Serious problem with "createMask" function.
Ältere Kommentare anzeigen
Dear all,
I am trying to develop a tool that lets the user to correct manually the contour of an object. The idea is to convert each pixel in the contour and make it a vertex using "impoly" in following code:
Z = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
BW = im2bw(Z);
B = bwboundaries(BW);
b = B{1};
X = b(:, 1);
Y = b(:, 2);
figure, imshow(BW);
hpoly = impoly(gca,[Y, X]);
wait (hpoly)
maskImage = hpoly.createMask();
figure, imshow(maskImage);
figure, imshow(Z);
Now, if you didn't do any change to the vertex and just double clicked inside the square you can notice that "maskImage" and "Z" are not the same!
Any one can tell me why is that? and how can we solve it?
Any help will be appreciated.
Meshoo
Akzeptierte Antwort
Weitere Antworten (1)
Steve Eddins
am 25 Mär. 2014
1 Stimme
If you are clicking on the centers of the border pixels, then half of the border pixels lie inside the polygonal region you selected and half lie outside. You should click between adjacent pixels instead.
4 Kommentare
Image Analyst
am 25 Mär. 2014
He's clicking anywhere inside the square to "Accept" the mask. Try this code instead to better illustrate. I have to admit, I can understand his confusion.
Z = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
BW = im2bw(Z);
B = bwboundaries(BW);
b = B{1};
X = b(:, 1);
Y = b(:, 2);
subplot(1,2,1);
imshow(BW, 'InitialMagnification', 1600);
axis on;
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
uiwait(msgbox('Double click anywhere inside the square'));
hpoly = impoly(gca,[Y, X]);
wait (hpoly)
maskImage = hpoly.createMask()
subplot(1,2,1);
imshow(maskImage, 'InitialMagnification', 1600);
axis on;
grid on;
subplot(1,2,2);
imshow(Z, 'InitialMagnification', 1600);
axis on;
grid on;
Steve Eddins
am 27 Mär. 2014
I believe I did understand the question. I'm sorry that my answer cryptic. The X-Y coordinates that are being passed to impoly enclose a region that includes only half of most the pixels around the border of your mask, and only a quarter of the pixels on the corners. The mask "shift" you are observing is simply a consequence of the tie-breaking rules for determining the output value for a pixel that's half inside and half outside the polygon.
The createMask function for impoly and its cousins uses the Image Processing Toolbox poly2mask, and poly2mask has been designed to have a consistent geometry for pixels that have unit area, as opposed to pixels that are defined a zero-area point. Because the output from bwboundaries traces through pixel centers instead of along pixel edges, it doesn't play nicely with the poly2mask geometric conventions.
For than you probably want to know about poly2mask, see these blog postings:
To get output from bwboundaries that works consistently with poly2mask, try the following:
Upsample the binary by a factor of 3 (imresize(BW,3,'nearest')).
Call bwboundaries
Scale and shift the output of bwboundaries back into the original coordinate system.
I'll experiment with some code to do this.
Meshooo
am 28 Mär. 2014
Kategorien
Mehr zu Annotations finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

