How to select and crop a particular region from the image?

조회 수: 118 (최근 30일)
Jonathan
Jonathan 2015년 8월 18일
답변: Josep Llobet 2021년 7월 23일
After some filtering, I manage to get this image,
My question is, how do you select and hopefully crop the region of interest? So I can get something like this.
Many thanks,

채택된 답변

blaat
blaat 2015년 8월 18일
편집: blaat 2015년 8월 18일
If you have the Image Processing Toolbox, you can use the function imrect() to select a rectangular area from your image. For example:
figure;
imshow(img);
h_rect = imrect();
% Rectangle position is given as [xmin, ymin, width, height]
pos_rect = h_rect.getPosition();
% Round off so the coordinates can be used as indices
pos_rect = round(pos_rect);
% Select part of the image
img_cropped = img(pos_rect(2) + (0:pos_rect(4)), pos_rect(1) + (0:pos_rect(3)));
  댓글 수: 2
piku mandal
piku mandal 2018년 10월 1일
but how to use it for live images that is come from camera?
Jessica Avellaneda
Jessica Avellaneda 2021년 5월 10일
how to use it for a movie?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Josep Llobet
Josep Llobet 2021년 7월 23일
You can select just the regions of the matrix that define your image, as follows:
img_cropped = img(300:450, 500:600);
imshow(img_cropped)
In the example, you will select the image in the regions between 300 and 450 of the x axis, and the 500 to 600 in the y axis.
If you want to know the size of the matrix which define your image, you can use size()
size(img)
Hope this will be useful!!

Community Treasure Hunt

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

Start Hunting!

Translated by