regarding cropping a polygon out of an image

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

 Accepted Answer

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

clear all;
close all;
clc
I=dicomread('100.ima');
[x,y,I2,rect]=imcrop(I,[]);
figure,imtool(I,[]);
figure,imtool(I2,[]);
m=143;
imstack=zeros(512,512,m);
for z=1:m;
n=dicomread([num2str(z),'.ima']);
n=imcrop(n,rect);
M=shrinkWrap(n,'objthresh',15000,'biggest');
Ipore=n<400&M;
imstack(:,:,z)=Ipore;
end
y=double(imstack);
D = squeeze(y);
h = vol3d('cdata',D,'texture','2D');
view(3);
Update view since 'texture' = '2D'
vol3d(h);
axis tight; daspect([1 1 .4])
alphamap('rampup');
alphamap(.06 .* alphamap);
Thanks a bunch for your help.This is the complete code that is working..the code is asking only once for cropping and then applying those dimensions to all the 143 images and popping the output..
Does this mean, that you accept the answer?

Sign in to comment.

More Answers (2)

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

thanks for the reply..BW created the mask i required...but img is showing the straight line...how to superimpose the created mask over the original image?
Did you try the to apply the mask by "img(~BW) = 255;
" or "img(~cat(3, BW, BW, BW)) = NaN;"? This should disable all not-masked pixels.
it's displaying only the thin strip of line..
What is "it" and what is "the thin strip of line"? When I use the above code, I can cut out the rectangle around an arbitrary polygone and all pixels outside the polygone are white. Please post, what you have done exactly - using one of the example pictures shipped with Matlab.
What is "it" and what is "the thin strip of line"? Please let me not guess the details, but post, what you have done exactly - using one of the example pictures shipped with Matlab. When I use the above code, I can cut out the rectangle around an arbitrary polygone and all pixels outside the polygone are white.
sorry for not being clear...i mean the code line img(~BW) is throwing the output of thin strip of line...
clear all;
x=dicomread('100.ima');
imshow(x,[]);
h=impoly;
position=wait(h);
x1=min(position(:,1));
X2=max(position(:,2));
y1=min(position(:,1));
y2=max(position(:,2));
bw=createMask(h);
imtool(bw);
x(~bw)=255;
imshow(x(~bw),[]);
this is the code i used.the output of imtool(BW) is a white rectangular box with black background.this is the mask.....now i want to apply this mask on the original image to get the cropped output image...next thing is to apply this to 140 images in a loop...
clear all;
close all;
clc
I=dicomread('100.ima');
[x,y,I2,rect]=imcrop(I,[]);
figure,imshow(I,[]);
figure,imshow(I2,[]);
m=143;
imstack=zeros(512,512,m);
for z=1:m;
n=dicomread([num2str(z),'.ima'];
[x,y,n,rect]=imcrop(n,[]);
M=shrinkWrap(n,'objthresh',15000,'biggest');
Ipore=n<400&M;
imstack(:,:,Z)=Ipore;
end
this line is able to do the rectangular cropping i need poly cropping instead. [x,y,I2,rect]=imcrop(I,[]);
but i am making changes to above code as well bcoz its asking for dimension for the second time in the loop..i jst want to take the dimensions for the first time and apply it to everything.
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?
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.

Sign in to comment.

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

I have got two questions regarding your code Alex
1. How can we do the same thing but for colored images because I have tried running your code for colored ones and it outputs red colored polygon of an image(probably which is the red channel of an image).
2.What if I want to do further processing only on image pixels within the polygon of the same image including the boundary.How should I select those pixel values only?
It will work with this adaptation:
BW = roipoly
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(BW,class(rgbImage)));
% Display it.
imshow(maskedRgbImage);
Thank you Image Analyst and What about my second question
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
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?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!