フィルターのクリア

save coordinates by using impoly..

7 ビュー (過去 30 日間)
Khaled Al-Faleh
Khaled Al-Faleh 2017 年 5 月 9 日
コメント済み: Khaled Al-Faleh 2017 年 5 月 19 日
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 ...

採用された回答

Image Analyst
Image Analyst 2017 年 5 月 12 日
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 件のコメント
Guillaume
Guillaume 2017 年 5 月 18 日
@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.
Khaled Al-Faleh
Khaled Al-Faleh 2017 年 5 月 19 日
Ok million of thanks to u all and sorry I will not ask again any more :)

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
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 件のコメント
Image Analyst
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
Khaled Al-Faleh 2017 年 5 月 12 日
Thanks sir its clear now :) .

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by