save coordinates by using impoly..
显示 更早的评论
Hi, I am using imrect to select what I need from image and save the coordinates and then show the image with that coordinates see the code
I = imread('gantrycrane.png');
imshow(I);
s = imrect;
P = getPosition(s)
x=P(1,1);
y=P(1,2);
w=P(1,3);
h=P(1,4);
II = I(y:y+h,x:x+w);
imshow(II);
now I want to use impoly to do same thing like what I did with imrect I want to select what I need by using impoly and save the coordinates and show the image with that coordinates ... I wish my question is clear ...
采纳的回答
更多回答(1 个)
Guillaume
2017-5-9
Well, you can't crop an image to an arbitrary polygon since an image must be rectangular. Guessing, maybe you want the image crop to the bounding box of the polygon, with all pixels outside the polygon set to black:
sourceimage = imread('gantrycrane.png');
imshow(sourceimage);
hpoly = impoly;
polypoints = hpoly.getPosition;
croppedimage = sourceimage;
croppedimage(~repmat(hpoly.createMask, 1, 1, 3)) = 0; %set outside of polygon to black
croppedimage = croppedimage(min(polypoints(:, 2)):max(polypoints(:, 2)), min(polypoints(:, 1)):max(polypoints(:, 1)), :);
imshow(croppedimage)
8 个评论
Khaled Al-Faleh
2017-5-11
Guillaume
2017-5-12
Well, considering you haven't told us what it is you want in enough detail, I'd say my answer does what you want.
If not, please explain what it is you want exactly. Preferably by showing examples.
Khaled Al-Faleh
2017-5-12
What do you think
min(polypoints(:, 2)):max(polypoints(:, 2))
min(polypoints(:, 1)):max(polypoints(:, 1))
is in the line that performs the crop, if not the bounds of the cropped image?
Khaled Al-Faleh
2017-5-12
Khaled Al-Faleh
2017-5-12
Image Analyst
2017-5-12
编辑:Image Analyst
2017-5-12
Why do you think images are indexed (x,y), they ARE NOT! Images take row and column as the indexes in that order, so you need to put y first, NOT x
II = sourceimage(y:y+h,x:x+w);
Also, your equations for x, y, w, and h are wrong.
Khaled Al-Faleh
2017-5-12
类别
在 帮助中心 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
