MATLAB Answers

How to crop an image inside a boundary ?

37 ビュー (過去 30 日間)
Warid Islam
Warid Islam 2019 年 7 月 17 日
コメント済み: Divya Gaddipati 2019 年 7 月 26 日
I have an image from which I want to crop a certain portion of it. I drew the boundary at first. Below is the code:
img = imread('Intensity1.jpg');
figure, imshow(img);
% getting a segment
position = wait(h);
figure,imshow(region); title('Segmented Region');
% use bwperim rather than bwboundaries to get an Image of boundary.
boundImg = uint8(bwperim(rgb2gray(region),4));
boundImg(boundImg ~=0) = max(img(:));
figure, imshow(img + boundImg)
% save image without loosing information..
imwrite(img + boundImg, 'writeImg.jpg');
% check
read = imread('writeImg.jpg');
figure, imshow(read)
isequal(read, img + boundImg)
The image is below:
I want to crop the portion of the image specified by the boundary above. I tried the following line of code but there is no change in the result.
Any suggestions would be very much appreciated. Thank you.

  3 件のコメント

Kristen Meyer
Kristen Meyer 2019 年 7 月 19 日
So your image is read and you have a matrix discribing the selected area?
for i = 1:numCol
read(lowerRowIndexOfSelectionAtCol_i:end,i,:) = [];
read(1:upperRowIndexOfSelectionAtCol_i,i,:) = [];
Goes column by column and removes above and below selected area. Hopfully it's enough to get you where you need to go.
KALYAN ACHARJYA 2019 年 7 月 20 日
@Warid Please do clarify your job to segment the yellowish region or extracts marked ROI only?
Warid Islam
Warid Islam 2019 年 7 月 22 日
Hi Kalyan,
I want to extract the marked ROI only.

Sign in to comment.


Divya Gaddipati
Divya Gaddipati 2019 年 7 月 24 日
After getting the mask of the selected region from the above line in your code, you can use regionprops to obtain a rectangular box around the mask and then use imcrop to crop the image.
stats = regionprops(region, 'BoundingBox');
crop_region = stats.BoundingBox; % contains [top_left_x top_left_y width height]
cropped_img = imcrop(img, crop_region);
For more information on how to use regionprops and imcrop, refer to the following links:

  2 件のコメント

Warid Islam
Warid Islam 2019 年 7 月 24 日
Hi Divya,
Thank you for your response. I tried the above line of code but somehow I don't get any option to crop. I get the following error message.
Undefined function 'wait' for input arguments of type 'images.roi.Freehand'.
Error in fh4 (line 5)
position = wait(h);
Divya Gaddipati
Divya Gaddipati 2019 年 7 月 26 日
I assume you are using the wait command to obtain the positions of the selected boundary from.
The drawfreehand command returns an object which contains the positions of the boundary you selected. Hence, instead of using wait command, you can directly access the "Position" parameter of your drawfreehand object (i.e, h).
position = h.Position;
Try the above line in place of "position = wait(h)".
For more information, refer to the "Output Arguments" section in the drawfreehand documentation.

Sign in to comment.

その他の回答 (0 件)




Translated by