How to automatically crop region of an object in an image with a bounding box

9 ビュー (過去 30 日間)
Zoe
Zoe 2023 年 1 月 9 日
コメント済み: DGM 2023 年 1 月 10 日
Hi, I have a set of images (as the attached) in which a small round filter (the one in the middle) is imaged. The locations of the round filter varied between images. Is there any way to automatically apply a bounding box to crop the region of the round filter?

採用された回答

DGM
DGM 2023 年 1 月 10 日
I don't know what parts of the image may vary between frames. Particularly, whether the object always appears with a similar size, and whether the object always has a slightly blue margin with a slightly yellowish interior. I am going to ignore the interior colored region and its relationship to object size.
% an image
inpict = imread('D1-MOSL-0919-R2A-9.jpg');
% generate a mask selecting the blue object margin
limits = [0.498 0.794;
0.066 1.000;
0.212 0.965];
hsvpict = rgb2hsv(inpict);
limits = permute(limits,[2 3 1]);
mask = all(hsvpict>=limits(1,:,:) & hsvpict<=limits(2,:,:),3);
% find the edge of the dish
% remove that part from the mask
dish = mask & ~imclearborder(mask);
dish = imdilate(dish,strel('disk',50));
mask = mask & ~dish;
% clean up the mask and fill it in
mask = bwareafilt(mask,1);
mask = imfill(mask,'holes');
mask = imopen(mask,strel('disk',50));
At this point, the mask can be applied however one chooses. To crop the image to the extent of the mask:
rows = any(mask,2);
cols = any(mask,1);
outpict = inpict(rows,cols,:);
Alternatively, maybe the lighting conditions and adjacent markings/dish make it difficult to isolate the object edges depending on the image. Maybe you could simply rely on the interior color being relatively unique. The problem is that if the edges of the object are difficult to isolate, and only the interior is known, you might have to assume the object's size based on a subset of its apparent geometry:
% an image
inpict = imread('D1-MOSL-0919-R2A-9.jpg');
% generate a mask selecting the blue object margin
limits = [0.919 0.257;
0.066 1.000;
0.212 1.000];
hsvpict = rgb2hsv(inpict);
limits = permute(limits,[2 3 1]);
Hmask = hsvpict(:,:,1)>=limits(1,:,1) | hsvpict(:,:,1)<=limits(2,:,1);
SVmask = all(hsvpict(:,:,2:3)>=limits(1,:,2:3) & hsvpict(:,:,2:3)<=limits(2,:,2:3),3);
mask = Hmask & SVmask;
% clean up the mask and fill it in
mask = bwareaopen(mask,50);
mask = imclose(mask,strel('disk',50));
mask = bwareafilt(mask,1);
% grow the mask by some presumed amount
mask = bwconvhull(mask); % might not be necessary for a box crop
mask = imdilate(mask,strel('disk',40)); % grow by 40px
... and again, the mask can be applied the same as before.
  4 件のコメント
Zoe
Zoe 2023 年 1 月 10 日
編集済み: Zoe 2023 年 1 月 10 日
is there any easy way to determine the limit or threshold? Also, you mentioned that if the edges of the object are difficult to isolate, and only the interior is known, I might have to assume the object's size based on a subset of its apparent geometry. I am just wondering what it means by 'assume the object's size based on a subset of its apparent geometry'?
DGM
DGM 2023 年 1 月 10 日
"is there any easy way to determine the limit or threshold?"
The Color Thresholder App provides at least a graphical means to select those. The specific values can be found by using the Export tab and selecting Export as function.
"what it means by 'assume the object's size based on a subset of its apparent geometry'"
In the second example, the mask selects the tan area, which is smaller than the object. In the example, I simply grew the mask by a fixed 40px so as to include the rest of the object. If the size of the object is variable, but (for instance) the object radius is always 8% larger than the radius of the tan area, then that's another piece of information that could be used to grow the mask to fit.
In any case, if the colors or object/image properties vary significantly between images, then it's important to know what information can be relied upon to be most consistent.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by