Matlab中的米粒分割

问题描述 投票:0回答:1

我有一个以jpg格式的多个米粒的图像。我需要将单独的米粒分成单独的图像。输入图像如下:

enter image description here

生成的图像应包含单个大米,如下所示:

enter image description here

当前我正在使用以下代码:

close all;

BW = imread('img11_Inp','jpg');
L = bwlabel(BW)

figure
    imshow(BW);
CC = bwconncomp(L);
stats = regionprops(L,'Image');
stats

%Display the first component as an image
Image1 = stats(2).Image
figure
    imshow(Image1);

此代码有两个问题。首先,它仅显示“状态”结构中的两个米粒图像,其次,还显示了一些噪音。请让我知道此代码有什么问题

image matlab image-segmentation
1个回答
0
投票

查看此代码

close all;
clear

I = imread('rice.jpg'); % jpg add noise. TODO: use png format
level = graythresh(I); % Global image threshold using Otsu's method
BW1 = imbinarize(I,level); % need to threshold, jpg add low level noise.
BW2 = imfill(BW1,'holes'); %fill holes inside grains

[L,n] = bwlabel(BW2); % label connected components
B = labeloverlay(I,L); 
figure('Name',sprintf('NLables=%d',n)); imshow(B);

stats = regionprops(L,{'Centroid','Area'});
for i=1:length(stats)
    x=stats(i).Centroid(1);
    y=stats(i).Centroid(2);
    area=stats(i).Area;
    text(x,y,sprintf('Area=%d',area),'FontSize',8);
end
© www.soinside.com 2019 - 2024. All rights reserved.