number of objects in images
Ältere Kommentare anzeigen
this the question:

i try two code for this image, i want it one code but doesn't work for two images correctly
the first code is gode for rice count:
% Read image
I=imread('rice.png');
% Estimate background using morphological opening operation
% Morphological opening is an erosion followed by a dilation
% The opening operation has the effect of removing objects.
% strel('disk',15): disk-shaped structuring element with radius 15
background=imopen(I,strel('disk',15));
% Subtract background image from original image
I2=I - background;
% Increase image contrast, because background is too dark, so we using imadjust
I3=imadjust(I2);
% Threshold image to create binary version of image in order to count the number of grains of rice
% graythresh computes threshold for conversion from gray
level=graythresh(I3);
% im2bw converts grayscale image to binary image using thresholding
bw=im2bw(I3,level);
% bwareaopen removes background noise
bw=bwareaopen(bw,50);
% bwconncomp finds all connected components (objects) in binary image
cc=bwconncomp(bw,4);
num= cc.NumObjects
% view all objects
% create a label matrix and display it as a pseudo-color indexed image
% labelmatrix creates a label matrix from output of bwconncomp
labeled=labelmatrix(cc);
% label2rgb creates colormap, choose background color, and maps objects in
% labelmatrix map to colors in colormap
RGB_label=label2rgb(labeled,@spring,'c','shuffle');
figure; imshow(RGB_label);
title(['Number of objects detected: ', num2str(num)]);
and the second code is good for the another image:
close all
clear all
clc% Clear command window.
% Read the input image and display
Image = imread('objj.png');
% Convert the image to grayscale
Imag = rgb2gray(Image);
% Convert to binary image to remove small objects
bw = Imag>210;
s = strel('disk',8);
% Morphologically open image
o = imopen (bw,s);
% Morphologically close image
o = imclose (o,s);
% Number of objects
[Imag , num] = bwlabel (~o,4);
disp ("Number of objects detected")
disp (num)
subplot(2,2,1)
imshow(Image)
title(['Number of objects detected: ', num2str(num)]);
3 Kommentare
Walter Roberson
am 29 Okt. 2023
Near the center top of the rice image, there is a single yellow blob that has two connected parts at different angles. If the task is to find the number of objects in the image, then that blob should be counted as a single image. If the task is to find the number of rice grains in the image, then logic about the shape of rice should be applied to separate that single blob into two grains. Therefore the number of expected objects depends on how the task is defined.
Deema
am 29 Okt. 2023
Walter Roberson
am 29 Okt. 2023
The general task of finding "objects" in any image has no reason to know that blobs of a single color are composed of multiple objects.
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 29 Okt. 2023
0 Stimmen
Yeah, that's because you're using some AI chatbot to write your code instead of trying to understand basical fundamental image processing methods. There's a lot of useless (like imadjust) or wrong stuff in the code you posted, though it is nice to put in lots of comments. Here's what I'd do. Give it a try
- Convert the RGB images into gray scale using rgb2gray().
- Find the mode gray level using mode().
- Create a mask that has everything except the mode gray level. If the images are noisy (like from a JPG file) then use a small range around the mode, like 2 gray levels or something. mask = (grayImage < modeGL-2) | (grayImage > modeGL+2)
- If some of the rice blobs are touching, then call imerode to erode away a layer or two of pixels. It shouldn't be needed with the shapes image but probably won't hurt.
- Call bwlabel to count the blobs [~, blobCount] = bwlabel(mask)
It should work for both images. If it doesn't, look at the gray scale image. It might be possible that some of the colored objects are the same gray level as the mode once they've been converted to gray level. In that case we'll have to do color segmentation where we can get the most common color and mask for just that particular color.
5 Kommentare
Deema
am 29 Okt. 2023
Image Analyst
am 30 Okt. 2023
I didn't post any code because it looks like a homework assignment you need to turn in and be graded on. I can help you with your code. What code did you try? You forgot to post it.
Deema
am 30 Okt. 2023
Image Analyst
am 30 Okt. 2023
You can try detect but in general, no, there is no single function that can detect all objects in all images since the definition of objects and backgrounds varies so much and is subjective.
Deema
am 30 Okt. 2023
Kategorien
Mehr zu Region and Image Properties finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






