Drawing rectangles using matrices.

Hi, I need to draw a figure as shown in the picture using 256x256 matrix. There should be two rectangles and the rest of the matrix values should be zero. Can you please help ?

댓글 수: 5

Anvinder  Singh
Anvinder Singh 2016년 2월 28일
편집: Azzi Abdelmalek 2016년 2월 28일
So i have drawn this figure as attached here and have used this code :
xlim([0 250]);
ylim([0 200]);
x = rectangle('Position',[40 130 70 40])
y = rectangle('Position',[120 10 80 25])
imshow(y).
Now can you please suggect me how to make the same figure using a 256x256 matrix ? How can i make lines using the matrix at some particular pixels which will create the same figure as shown.
Azzi Abdelmalek
Azzi Abdelmalek 2016년 2월 28일
your question is not clear, illustrate with a 4x4 matrix for example
Anvinder  Singh
Anvinder Singh 2016년 2월 28일
Ok, Rephrasing the question: The image which is attached 'send.jpg' it has two rectangles. I have implemened ted using the function 'rectangle('position')'. Now i need to implement the same image with the two rectangles using matrices. In other words, how to show 2 rectangles using matrices ?
sudharsan V
sudharsan V 2018년 8월 1일
how to draw a triangle instead of rectangle?
Image Analyst
Image Analyst 2018년 8월 1일
Use plot() with 3 x values and 3 y values.

댓글을 달려면 로그인하십시오.

답변 (4개)

Stephen23
Stephen23 2016년 2월 28일
편집: Stephen23 2016년 2월 28일

1 개 추천

You can use MATLAB's array indexing quite effectively for this:
>> X = ones(256,256,3);
>> X(40:140,[40,140],:) = 0;
>> X([40,140],40:140,:) = 0;
and to view the rectangle:
>> image(X)
creates this:

댓글 수: 8

Anvinder  Singh
Anvinder Singh 2016년 2월 28일
Thanks Cobel, What function is this X(40:140,[40,140],:) = 0; ? Where can i get more info on this ? Also the Y axis is reversed in the image ?
Image Analyst
Image Analyst 2016년 2월 28일
편집: Image Analyst 2016년 2월 28일
The y axis is show normally for an image. The top row is y=1 and increases from there as it goes down. This is opposite from a graph where the origin is at the bottom left. But it matches your example of what you said you wanted. If you just want a graph, use the rectangle() function.
Stephen23
Stephen23 2016년 2월 28일
편집: Stephen23 2016년 2월 28일
@Anvinder Singh: "What function is this" the first line of my answer tells you what this is: it is called array indexing. Follow that link to learn more, or start here:
Anvinder  Singh
Anvinder Singh 2016년 2월 29일
One more question , is this a 3d array that you have used ? I am unable to see it on their website .
Stephen23
Stephen23 2016년 2월 29일
Yes, I used a 3d array because this represents a standard RGB image that can be displayed via image. You can find more info on multidimensional arrays here:
Anvinder  Singh
Anvinder Singh 2016년 2월 29일
Stephen, Is there any way you can help me with the 2d matrix ? I am trying but am not able to get the result ! I want the same thing with 2d matrix
Guillaume
Guillaume 2016년 2월 29일
My answer is the same as Stephen's but applied to a 2D matrix.
However, I think you should pause and learn the basics of matrix indexing. If you're not able to understand Stephen's or my code you won't go very far.
In matlab's documentation follow the getting started tutorial.
Anvinder  Singh
Anvinder Singh 2016년 2월 29일
Guillaume you are right. I thought the rectangle function you defined was independent of the matrix and was it's values just based on the values given to it so it had nothing to do with the matrix (256*256) so got a little confused.

댓글을 달려면 로그인하십시오.

Guillaume
Guillaume 2016년 2월 28일

0 개 추천

Considering your rectangles are aligned with the side of the image, all you have to do is fill two rows and two columns with 1:
function img = drawrectangle(img, xbounds, ybounds)
img(ybounds, xbounds(1):xbounds(2)) = 1;
img(ybounds(1):ybounds(2), xbounds) = 1;
end
And in your case, you use it like this:
img = zeros(256);
img = drawrectangle(img, [40, 140], [60, 80]); %you didn't show the y coordinates for the 1st rectangle
img = drawrectangle(img, [120, 200], [120, 200]);
imshow(img);
Image Analyst
Image Analyst 2016년 2월 28일

0 개 추천

If you want them on a graph, with the y increasing upwards, try this:
rectangle('Position', [40,40,100,100]);
hold on;
rectangle('Position', [120,120,80,80]);
axis([0,220, 0,220]);
Note, your rectangles will overlap/intersect given the coordinates you gave despite the fact that your picture incorrectly shows them not overlapping.

댓글 수: 3

Anvinder  Singh
Anvinder Singh 2016년 2월 28일
Thanks, The rectangle function is good and i am able to implement it but the main problem is i need to do this using matrices. Like X = ones(256,256,3); X = ones(256,256,3); X(40:140,[40,140],:) = 0; X([40,140],40:140,:) = 0; but i am not sure about this function 'X([40,140],40:140,:) = 0;' and how to interpret its values. Any help is appreciated.
Then do it the way the other two people showed you. If you want to reverse the y axis direction, you can with something like this
set(gca, 'ydir', 'reverse'); % or 'normal'
Anvinder  Singh
Anvinder Singh 2016년 2월 29일
how do i set a given points in a row of matrix as 1 ? example- set x coordinate 40 to 120 as 1 etc..

댓글을 달려면 로그인하십시오.

Anvinder  Singh
Anvinder Singh 2016년 3월 1일

0 개 추천

The y axis of the graph is still reversed. I have tried axis([0,280, 0,280]); and set(gca, 'ydir', 'reverse'); % or 'normal'

댓글 수: 1

Image Analyst
Image Analyst 2016년 3월 1일
Explain why 120 comes after 140 in your diagram.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2016년 2월 28일

댓글:

2018년 8월 1일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by