Remove specific objects from an image
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I want to remove some objects from a binary image based upon length to width ratio of the rectangular labeled each object, such that when a particular threshold is set the components satisfied then it removes t hose objects and show a new image.
Anybody has any idea?
댓글 수: 1
Can you share the sample image?
채택된 답변
Following similar logic to this answer, we'll use regionprops to get the length and width of the bounding box rectangles. Then we can compute the length:width ratio, set a threshold, and determine which boxes are greater than that threshold.
Then you can plot accepted objects with a red rectangle and rejected objects with a yellow rectangle.
See below for a second method.
% Read image as RGB
I = imread('bwimage.png'); %including the pull path is better
% Convert to binary
BW = imbinarize(rgb2gray(I));
% Get rid of white border
BWConv = bwconvhull(~BW);
BW = BW & BWConv;
% get region centers and major axis, and orientation
stats = regionprops('table',BW,'BoundingBox');
% Compute length/width ratio
stats.LenWdRatio = stats.BoundingBox(:,3) ./ stats.BoundingBox(:,4);
% Set threshold and determine which objects
% are greater than the threshold.
thresh = 2.0;
stats.isGreater = stats.LenWdRatio > thresh;
% Show bounding box for objects that met the threshold
figure()
h = imshow(BW);
axis on
hold on
% Plot red rectangles around accepted objects
arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','r'), find(stats.isGreater));
% Plot yellow rectangles around rejected objects
arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','y'), find(~stats.isGreater));

Second method: using major:minor axis length
Instead of using the horizontal and vertical extent of the bounding box, you could use the ratio of the MajorAxisLength to the MinorAxisLength. Here's an example why this method might be better. Suppose you have an long object that stretchs along a diagonal with slope=1. The width and height of the bounding box will be equal even though it's a very long and narrow object. If you used the ratio of the Major:Minor axis lengths, the object will be detected as long and narrow even through the bounding box is square.
To use this method, make the following changes.
% get region centers and major axis, and orientation
stats = regionprops('table',BW,'BoundingBox','MajorAxisLength','MinorAxisLength');
% Compute length/width ratio
stats.LenWdRatio = stats.MajorAxisLength ./ stats.MinorAxisLength;
[Update]
I forgot to show how to remove the objects.
When you call regionprop, add the SubarrayIdx property to the list.
stats = regionprops('table',BW,'BoundingBox','MajorAxisLength','MinorAxisLength','SubarrayIdx');
Then you can loop through each rejected object and replace its values in BW with false (black).
% Remove rejected objects (fill with black)
objRemoveIdx = find(~stats.isGreater);
for i = find(~stats.isGreater).'
BW(stats.SubarrayIdx{i,1},stats.SubarrayIdx{i,2}) = false;
end
% Show same figure with objects removed (yellow rectangles show where they were)
figure()
h = imshow(BW);
axis on
hold on
% Plot red rectangles around accepted objects
arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','r'), find(stats.isGreater));
% Plot yellow rectangles around rejected objects
arrayfun(@(i)rectangle('Position',stats.BoundingBox(i,:),'EdgeColor','y'), find(~stats.isGreater));
댓글 수: 8
I think the second method is better. That's what I'd use. Bounding box is often deceptive, like when it's at an angle as you explained.
Adam Danz
2019년 10월 18일
Thanks, @Image Analyst. The only reason I listed it as 2nd was because I thought of it and added it to the answer after the first method was posted.
Hi, I tried using this code but the line 'objRemoveIdx = find(~stats.isGreater). Returns an error " Requested array exceeds the maximum possible variable size." What could be the problem?
It looks like you're working with an image with lots of objects captured by regionprops, too many for Matlab to handle. Perhaps you could break up your image into smaller sub-images, if this is indeed the problem.
@Mustapha Seidu What is this stats structure? Is it something returned from regionprops()? I don't see any isGreater property that is measured by regionprops(). If you still have trouble, read this:
See my Image Segmentation Tutorial here:
Knowing what the rest of the error message says might also be helpful here.
I can run Adam's code out to 25 million objects before running into array size errors, and even then, they're not "maximum possible array size" errors; they're "maximum array size preferences" errors (I don't have much memory).
I question the likelihood that someone is ntentionally trying to segment images with tens of millions of objects in them. Any nontrivial images would need to be ridiculously large to even hold that many objects without them being connected.
@Adam Danz How can I use this code to select the large circles and worm shape objects in my figure in yellow?
@Laura the image you shared may be a bit difficult for this method but I would start by removing anything that isn't yellow-ish by looking at the color data and thresholding the colors. But since many of the yellow blobs do not appear to be discrete objects this method may fall short in which case asking a new question may get you more targetted help.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Color Segmentation에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
