![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176138/image.png)
save coordinates by using impoly..
7 views (last 30 days)
Show older comments
Khaled Al-Faleh
on 9 May 2017
Commented: Khaled Al-Faleh
on 19 May 2017
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 ...
0 Comments
Accepted Answer
Image Analyst
on 12 May 2017
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
sourceImage = imread('peppers.png');
subplot(2, 2, 1);
imshow(sourceImage);
title('Original Image', 'FontSize', 20);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
uiwait(msgbox('Draw a polygon. Double click to accept/finish it'));
hPoly = impoly;
polyPoints = hPoly.getPosition
mask = hPoly.createMask;
subplot(2, 2, 2);
imshow(mask);
title('Mask', 'FontSize', 20);
croppedImage = sourceImage; % Initialize
% Set outside of polygon to black
croppedImage(~repmat(mask, 1, 1, 3)) = 0;
x1 = round(min(polyPoints(:, 1)))
x2 = round(max(polyPoints(:, 1)))
y1 = round(min(polyPoints(:, 2)))
y2 = round(max(polyPoints(:, 2)))
% Do a rectangular crop
croppedImage = croppedImage(y1:y2, x1:x2, :);
subplot(2, 2, 3);
imshow(croppedImage);
title('Cropped Image', 'FontSize', 20);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176138/image.png)
9 Comments
Guillaume
on 18 May 2017
@Khaled,
reading all your questions in this thread, it's clear that you haven't spend the time to understand what the code does in any of our answers. Everything you need was given in my original answer and if you'd try to understand what it did (that is what (min(polypoints(:, 2)) and co. represent), you would have had no need to ask any of the subsequent questions.
It's incredibly frustrating to answerers, when the person asking the question do not try to understand the answer they're given.
More Answers (1)
Guillaume
on 9 May 2017
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 Comments
Image Analyst
on 12 May 2017
Edited: Image Analyst
on 12 May 2017
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!