필터 지우기
필터 지우기

Create a binary image from a 2-dimensional binary [0 1] matrix

조회 수: 40 (최근 30일)
Nicholas
Nicholas 2022년 11월 21일
댓글: Nicholas 2022년 11월 24일
I've been trying for a while to draw shapes in the binary image function and it hasnt worked, I do not understand how to plot the coordinates of the shapes. Does anyone have any useful links or tips i can utilise?
  댓글 수: 2
DGM
DGM 2022년 11월 21일
Someone asked this recently. I'm assuming you also need to count the pixels as well?
Nicholas
Nicholas 2022년 11월 21일
Yes, I do need to count the pixels as well.

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

채택된 답변

Image Analyst
Image Analyst 2022년 11월 21일
Pretty trivial. First define the number of rows and columns in the image. Then define the x and y coordinates for one rectangle.
mask = false(rows, columns);
Then for one triangle, do
oneTriangle = mask & poly2mask(x, y, rows, columns);
Do that for all triangles. When done creating the mask, get the areas like this
props = regionprops(mask, 'Area');
allAreas = [props.Area]
I guess you haven't seen my tutorial yet. It's one of the most downloaded in the File Exchange and goes over basic stuff like this.
  댓글 수: 3
Image Analyst
Image Analyst 2022년 11월 22일
OK, seems like you're having trouble. Here's another way to do it:
rows = 100;
columns = 100;
mask = false(rows, columns);
% Create a triangle in the upper left
for col = 1 : 50
bottomRow = 50 - col + 1;
mask(1 : bottomRow, col) = true;
end
% Create square at bottom middle
mask(76:100, 26:76) = 1;
imshow(~mask);
% Compute areas of all shapes.
props = regionprops(mask, 'Area');
allAreas = [props.Area]
allAreas = 1×2
1275 1275
Extend it for the other 3 shapes.
Nicholas
Nicholas 2022년 11월 24일
thank you for the help, I greatly appreciate it

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

추가 답변 (1개)

Jan
Jan 2022년 11월 21일
What have you tried?
M = false(100, 100);
M(26:50, 76:100) = true; % The black rectangle
Instead of setting all pixels to black by a single true on the righ side, you can insert a triangle shape:
M(1:5, 1:5) = tril(true(5));
See also: triu, rot90, transpose.
  댓글 수: 4
Nicholas
Nicholas 2022년 11월 21일
This is all I've managed to do so far. I tried using a for loop
DGM
DGM 2022년 11월 21일
편집: DGM 2022년 11월 21일
So from this we can tell that the image is to be black-on-white and numeric. We can also tell that none of these answers will meet the requirements. The assignment instructs to do it row-wise using a loop (as maddening as that sounds). The prior question I answered on this assignment used a loop. I'll leave it to you to find it.
As an aside, when given an assignment that explicitly tells me to do things the dumb inefficient way, I'd tend to do it the way I'm told, and then also do it in a better way just to point out the absurdity of teaching students how to shoot themselves in the foot. Of course, the TA grader didn't write the assignment, but it's the thought that counts -- and usually the TA knows it's BS too.

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by