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 ...

 Accepted Answer

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);

9 Comments

Thanks sir :).
You're welcome. Actually to make sure you capture the complete image, don't use round() - use ceil() and floor() instead:
x1 = floor(min(polyPoints(:, 1)))
x2 = ceil(max(polyPoints(:, 1)))
y1 = floor(min(polyPoints(:, 2)))
y2 = ceil(max(polyPoints(:, 2)))
Sir I want to put a red rectangular box to what we cut I used this but not working will
rectangle('Position',[x1 y1 x2 y2],'EdgeColor','r','LineWidth',2);
The Position property takes [xLeftColumn, yTopLine, xWidth, yHeight], not [x1 y1 x2 y2].
Ok sir but in our code before how I get [xLeftColumn, yTopLine, xWidth, yHeight]
rectangle('Position',[x1 y1 x2 y2],'EdgeColor','r','LineWidth',2);
Well, isn't the width equal to x2-x1? And the height y2-y1??????
So, did you try
rectangle('Position',[x1, y1, abs(x2-x1), abs(y2-y1)],'EdgeColor','r','LineWidth',2);
@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.
Ok million of thanks to u all and sorry I will not ask again any more :)

Sign in to comment.

More Answers (1)

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

so sir there is no way to do what I want ??
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.
Ok sir thanks for what u did but one more question please in your code the last croppedimage how I can get its coordinates the x,y,w and h of it ?
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?
sir see the image its about parking and I select a spot by impoly then I want to save its coordinates then use it to show it by original image because I will select many spots. I see the code sir I wish its clear.
sourceimage = imread('A.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)
x = min(polypoints(:, 2))
w = max(polypoints(:, 2))
y = min(polypoints(:, 1))
h = max(polypoints(:, 1))
II = sourceimage(x:x+h,y:y+w);
figure,imshow(II);
II = sourceimage(x:x+h,y:y+w);
figure,imshow(II);
here the problem does not show the same that I select..
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.
Thanks sir its clear now :) .

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!