Drawing rectangles using matrices.

21 visualizzazioni (ultimi 30 giorni)
Anvinder  Singh
Anvinder Singh il 28 Feb 2016
Commentato: Image Analyst il 1 Ago 2018
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 Commenti
sudharsan V
sudharsan V il 1 Ago 2018
how to draw a triangle instead of rectangle?
Image Analyst
Image Analyst il 1 Ago 2018
Use plot() with 3 x values and 3 y values.

Accedi per commentare.

Risposte (4)

Stephen23
Stephen23 il 28 Feb 2016
Modificato: Stephen23 il 28 Feb 2016
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 Commenti
Guillaume
Guillaume il 29 Feb 2016
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 il 29 Feb 2016
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.

Accedi per commentare.


Guillaume
Guillaume il 28 Feb 2016
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 il 28 Feb 2016
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 Commenti
Image Analyst
Image Analyst il 28 Feb 2016
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 il 29 Feb 2016
how do i set a given points in a row of matrix as 1 ? example- set x coordinate 40 to 120 as 1 etc..

Accedi per commentare.


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

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by