regarding cropping a polygon out of an image
6 views (last 30 days)
Show older comments
hi folks can anybody help me with cropping a polygon out of the image?
i used the following code
I=dicomread('100.ima');
figure,imshow(I,[]);
h=impoly;
position=wait(h);
its providing the option for configuring the required polygon.i have also said it to copy figure.
how can i see only the cropped image?
can anyone help with imcrop()to see the output
0 Comments
Accepted Answer
Jan
on 24 Feb 2011
Do not use
imshow(x(~bw), []);
but
imshow(x, []);
"x(~bw)" is a vector and IMSHOW must fail.
What is the problem with the 2nd part of your program? If you just want to apply the IMCROP once, use something like "if z==1".
2 Comments
More Answers (2)
Jan
on 22 Feb 2011
Do you want to crop a rectangle from the image? Or mask a polygone?
figure
img = imread('pout.tif');
imshow(img);
h = impoly;
position = wait(h);
x1 = min(position(:, 1));
x2 = max(position(:, 1));
y1 = min(position(:, 2));
y2 = max(position(:, 2));
BW = createMask(h);
img(~BW) = 255;
img = img(x1:x2, y1:y2);
% Or if img is a 3D RGB array:
% img(~cat(3, BW, BW, BW)) = NaN;
% img = img(x1:x2, y1:y2, :);
figure;
image(img);
8 Comments
manoj sambasivam
on 16 May 2012
Hi Jan simon,i have small doubt sir, regarding the program.after i select the polygon , what should i do to crop the polygon out of image and save it separetely?
Image Analyst
on 16 May 2012
You do know that images have to be rectangular, don't you? You do know that you can't have a polygonal image, right? (Looks like your answer would be "no") Even if the information is in a polygon shape, you still have to have something (like zeros) to make it a rectangular image.
Alex Taylor
on 16 May 2012
Also, if you DO intend to apply a polygon mask to an image, there are easier ways of doing it. The function roipoly is designed for interactively creating a mask of an arbitrary polygon:
doc roipoly
For example:
I = imread('pout.tif');
imshow(I);
BW = roipoly
Imasked = zeros(size(I),class(I));
Imasked(BW) = I(BW);
imshow(Imasked)
5 Comments
vidya
on 10 Sep 2012
Maria,
I am looking for an answer to your second question, were you able to find a solution, if yes can you share, if not can someone help me too. I also am trying to find a method that can help process only selected image pixels within a polygon for colored images, -Many thanks for your help
Image Analyst
on 10 Sep 2012
It depends on what kind of process you want to do: area processes and point processes would be done in different ways. Area processes (like blurring) need masking while point processes don't necessarily need extra masking steps (for example taking the average = mean(grayImage(maskImage))). Why don't you start a new thread and upload your image and tell us what you want to measure?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!