How to crop an image inside a boundary ?

7 visualizaciones (últimos 30 días)
Warid Islam
Warid Islam el 17 de Jul. de 2019
Respondida: Aytaç Tok el 12 de Mzo. de 2021
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
h=drawfreehand();
position = wait(h);
region=uint8(roipoly(img,position(:,1),position(:,2)));
region=region.*img;
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:
fh.jpg
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.
imcrop('Intensity1.jpg');
Any suggestions would be very much appreciated. Thank you.
  3 comentarios
KALYAN ACHARJYA
KALYAN ACHARJYA el 20 de Jul. de 2019
Editada: KALYAN ACHARJYA el 20 de Jul. de 2019
@Warid Please do clarify your job to segment the yellowish region or extracts marked ROI only?
Warid Islam
Warid Islam el 22 de Jul. de 2019
Hi Kalyan,
I want to extract the marked ROI only.

Iniciar sesión para comentar.

Respuesta aceptada

Divya Gaddipati
Divya Gaddipati el 24 de Jul. de 2019
region=uint8(roipoly(img,position(:,1),position(:,2)));
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 comentarios
Warid Islam
Warid Islam el 24 de Jul. de 2019
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 el 26 de Jul. de 2019
Hi,
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.

Iniciar sesión para comentar.

Más respuestas (1)

Aytaç Tok
Aytaç Tok el 12 de Mzo. de 2021
How can I get the object by auto cropping from a rgb picture. For example, there is a banana on the table and the background is black. I want to crop this picture and just take the banana and I want no background pixel at all. how can I do that

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by