Identify objects in images

image = imread(['pills_hard.jpg']);
imtool(image);
% gn(I);
gray_img = rgb2gray(image);
%imshow(image);
imshow(gray_img);
imtool(gray_img);
BW = imbinarize(gray_img,'adaptive','ForegroundPolarity','dark','Sensitivity',0.47);
figure;
imshow(BW);
% computes the compliment of the image
X = imcomplement(BW);
p4 = imfill(X, 'holes');
figure;
imshow(p4);
SE = strel('disk', 5);
p5 = imerode(p4, SE);
[L,num] = bwlabel(p5);
disp(num);
So I am provided a series of images with different difficulty levels, the aim is to count the pills, and with images containing different pills, identify and count them respectively. I made some progress with simpler images using imclose() and imsubtract(), or binarize grayscale image with different sensitivity level. But with shadows and connections of pills and reflection of omega-oil pills, I couldn't find a way to successfully produce outcomes for these harder images. I'm more than grateful if anyone can shed some light on this, thanks in advance. I have attached the code that I used.

답변 (1개)

Image Analyst
Image Analyst 2021년 11월 22일

0 개 추천

First of all, get a better background, like all white or something. Then background correct and threshold. Then measure features such as Area and MajorAxisLength to filter out the type of pills you want.
mask = correctedImage > someThreshold;
props = regionprops(mask, 'Area', 'MajorAxisLength', 'MinorAxisLength');
allAreas = [props.Area];
aspectRatios = [props.MajorAxisLength] ./ [props.MinorAxisLength]
smallPillIndexes = find(allAreas < someValue);
smallPillsMask = ismember(mask, smallPillIndexes);
bigPillsMask = mask & ~smallPillsMask;
Pretty easy but see my Image Segmentation Tuturial if you need help, or write back here.
You might also look into the Color Thresholder App on the Apps tab of the tool ribbon to get code to identify the colored background.

댓글 수: 8

U X
U X 2021년 11월 22일
Thank you for the reply! Unfortunately I'm only allowed to use these specific images. I have already downloaded your image segmentation tutorial, but I'm having so much trouble with establishing threshold because the shadow on the background and the light parts of oval pills (which are even brighter than the background itself)
Image Analyst
Image Analyst 2021년 11월 22일
That's fine. I didn't say anything about you having to use other images.
Did you try the Color Thresholder yet? If not, why not?
How much of your homework assignment am I allowed to do for you? The whole thing?
U X
U X 2021년 11월 23일
I tried the Color Thresholder. However, the mask produced was not ideal, and the function exported could not successfully mask other pictures with only the pills. And I can't really manually adjust all the pictures because the goal was to create a automatic process. Thank you for the suggestion, but I think I should go through the detailed implementation process by myself...I'm now considering using watershed, do you have any suggestion on this or other approaches?
Image Analyst
Image Analyst 2021년 11월 23일
편집: Image Analyst 2021년 11월 23일
Yes, to split apart the pills that are touching you'll have to use watershed. The color segmentation was only to get the pills mask first, upon which you'll feed into the watershed.
I'll see if I can get something, but you forgot to answer my question:
"How much of your homework assignment am I allowed to do for you? The whole thing?"
Can you do anything to help make your life easier, like put the pills well separated on a contrasting background like a green screen, or black velvet? Or use better lighting, like both broad overhead lighting, plus LED light strips around the edge, level with the pills, to eliminate shadows? Or at least have consistent lighting so the overall light level and pattern (where the hot spots is) doesn't vary all over the place. Improving the image capture setup can make the image analysis substantially easier.
Have you tried imfindcircles() for the circular pills?
U X
U X 2021년 11월 23일
I have to use the original images, sorry. I did try imfindcircles(), but I couldn't accurately locate the actual pills in hard images because of the shadows and lighting I think...
I'm afraid that I'm not allowed to get a full answer from you, as I said, I should go through the detailed implementation process by myself. But if you succeed with the images, perhaps you can let me know the general approaches you employed? Thank you so much.
Image Analyst
Image Analyst 2021년 11월 23일
Well what methods did they cover in your class?
U X
U X 2021년 11월 23일
In my class, we are not taught about specific methods in Matlab, and there's no limitations.
Image Analyst
Image Analyst 2021년 11월 23일
Strange they would give you such a difficult problem with no discussion of possible methods to solve it.
First I'd try to do a background correction by estimating the background, maybe by using the Color Thresholder. Then use scatteredInterpolant() to fill in the background everywhere. Then I'd divide your image by the background to compensate for the fact that there is lens shading and your light exposure is not the same everywhere. I attach a demo for that.
Next I'd try the Color Thresholder again on the background corrected image to see if you can get the pills without the shadows. Try HSV color space.
It's possible you might need a different masking function for each pill color, or you might be able to just get the background and invert it to get all the pills.
Since some of the pills are touching, after you get the mask you'll need to erode it a bit with imerode() to separate them. Or else use watershed:

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

카테고리

도움말 센터File Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

질문:

U X
2021년 11월 22일

댓글:

2021년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by