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
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
Deema am 29 Okt. 2023
Yes it’s find the number of objects in any image
Walter Roberson
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

DGM
DGM am 30 Okt. 2023

0 Stimmen

You can only barely segment that first image using rgb2gray(). The background and foreground regions differ by as little as 1LSB in luma. That's assuming you deal with the edges of the image.
Try to do it just using luma alone
inpict = imread('image.png');
% create mask in luma
yw = 2; % assuming input is always uint8
Y = im2gray(inpict);
ymode = mode(Y(:));
mask = Y<(ymode - yw) | Y>(ymode + yw); % good luck with that
% display the mask with an added border
% otherwise the edges are invisible against this awful white webpage
imshow(padarray(mask,[10 10],0,'both'),'border','tight')
% get rid of border effects
bordw = 7;
mask = mask(bordw+1:end-bordw,bordw+1:end-bordw);
mask = padarray(mask,[1 1]*bordw,'replicate','both');
% try to fill speckle holes
mask = imfill(mask,'holes');
% erode mask to help get rid of bridges
mask = imerode(mask,strel('disk',3));
imshow(padarray(mask,[10 10],0,'both'),'border','tight')
% check the number of objects found
CC = bwconncomp(mask);
CC.NumObjects
ans = 92
Instead, just try using hue or something where there's enough object contrast to actually separate things.
inpict = imread('image.png');
%inpict = jpegger(inpict,50);
% create mask in hue
hw = 0.2;
[H,~,~] = rgb2hsv(inpict);
hmode = mode(H(:));
mask = H<(hmode - hw) | H>(hmode + hw);
imshow(padarray(mask,[10 10],0,'both'),'border','tight')
% get rid of border effects
bordw = 7;
mask = mask(bordw+1:end-bordw,bordw+1:end-bordw);
mask = padarray(mask,[1 1]*bordw,'replicate','both');
imshow(padarray(mask,[10 10],0,'both'),'border','tight')
% check the number of objects found
CC = bwconncomp(mask);
CC.NumObjects
ans = 95
Is hue going to provide ideal object contrast in all images? No. Of course not. It wouldn't work for the second image, whereas Y would.
Good luck creating anything that can "count the number of objects in any image". It should be obvious that without constraints, that's a fool's errand. This image contains 25 objects (or is it 23? ... or is it 27? ... what about 29?). Even if we decide what an object is or isn't, global thresholds in any one color component aren't going to be much help here.

Weitere Antworten (1)

Image Analyst
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
  1. Convert the RGB images into gray scale using rgb2gray().
  2. Find the mode gray level using mode().
  3. 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)
  4. 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.
  5. 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
Deema am 29 Okt. 2023
1- it's from book with some edit not Ai chatbot
2- i try the code but the number for rice image is 100, thank you
Image Analyst
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
Deema am 30 Okt. 2023
1- not homework is just if we want (info.)
2- is there a one code for any image?
Image Analyst
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
Deema am 30 Okt. 2023
Thank you

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 29 Okt. 2023

Kommentiert:

am 30 Okt. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by