【图像检测】基于形态学算法实现水果蔬菜缺陷检测matlab代码

268 阅读2分钟

1 简介

机器视觉是指利用图像建立一个现实世界中的物体的虚拟的模型。图像是将现实世界中的三维物体转换成二维信息,在这个过程中,许多有用的三维的信息被模糊掉,而实际应用中,很多时候需要用到这些被模糊掉的信息。机器视觉正是完成这个多对一映射的信息恢复的手段,其目的就是利用电子化设备来感知进而理解图像,达到复制人类视觉的效果。图像是图像处理的对象,因此待处理图像的质量好坏直接影响结果。待处理图像的质量取决于两方面:图像采集装置与采集后的预处理。

2 部分代码

   imshow(maskedrgbImage);
   title('Result: Good');
end  

%=================================================================================%

%img = imread('b1.jpg');
img = imread('b2.jpg');
[L, Centers] = imsegkmeans(img, 3);
B = labeloverlay(img,L);
rgbImage = B;
% Display the original image.
subplot(4, 5, 9);
imshow(rgbImage);
title('Original Image(Banana)');
% Split the original image into color bands.
redBand = rgbImage(:,:, 1);
greenBand = rgbImage(:,:, 2);
blueBand = rgbImage(:,:, 3);
% Threshold each color band.
redthreshold = 100;
greenThreshold = 140;
blueThreshold = 170;
redMask = (redBand < redthreshold);
greenMask = (greenBand > greenThreshold);
blueMask = (blueBand < blueThreshold);
% Combine the masks to find where all 3 are 'true.'
damagedAreasMask = uint8(redMask & greenMask & blueMask);
maskedrgbImage = uint8(zeros(size(damagedAreasMask))); % Initialize
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* damagedAreasMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* damagedAreasMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* damagedAreasMask;
%imtool(maskedrgbImage);
reqInfo = maskedrgbImage(:, :, 1) > 0 & maskedrgbImage(:, :, 2) > 0 & maskedrgbImage(:, :, 3) > 0;
res = sum(reqInfo(:));
disp(res)
t = 10000;
if type=='b'
   t = 5000;
end
if res>t
   subplot(4, 5, 10);
   imshow(maskedrgbImage);
   title('Result: Infected');
else
   subplot(4, 5, 10);
   imshow(maskedrgbImage);
   title('Result: Good');
end  

%=================================================================================%
% display the Original Image
subplot(4,5,12);
imshow(ai)
title('Internal Image(Apple)')
%%=================================================================================================
n=imhist(ai); % Compute the histogram
N=sum(n); % sum the values of all the histogram values
max=0; %initialize maximum to zero
%%================================================================================================
for i=1:256
   P(i)=n(i)/N; %Computing the probability of each intensity level
end
%%================================================================================================
for T=2:255      % step through all thresholds from 2 to 255
   w0=sum(P(1:T)); % Probability of class 1 (separated by threshold)
   w1=sum(P(T+1:256)); %probability of class2 (separated by threshold)
   u0=dot([0:T-1],P(1:T))/w0; % class mean u0
   u1=dot([T:255],P(T+1:256))/w1; % class mean u1
   sigma=w0*w1*((u1-u0)^2); % compute sigma i.e variance(between class)
   if sigma>max % compare sigma with maximum 
       max=sigma; % update the value of max i.e max=sigma
       threshold=T-1; % desired threshold corresponds to maximum variance of between class
   end
end
%%====================================================================================================
bw=im2bw(ai,threshold/255); % Convert to Binary Image
subplot(4,5,13);
imshow(bw);
title('Otsu threshold');
imgc = uint8(imcomplement(bw));
%imshow(imgc); % Display the Binary Image

finalImage = uint8(zeros(size(ai)));
finalImage(:,:,1) = ai(:,:,1) .* imgc;
finalImage(:,:,2) = ai(:,:,2) .* imgc;
finalImage(:,:,3) = ai(:,:,3) .* imgc;
%imshow(finalImage);
reqInfo = finalImage(:, :, 1) > 0 & finalImage(:, :, 2) > 0 & finalImage(:, :, 3) > 0;
res = sum(reqInfo(:));
disp(res)

3 仿真结果

4 参考文献

[1]朱培逸, 刘红晴. 基于数学形态学图像分割算法在水果分级中的应用[J]. 科学技术与工程, 2013(34):6.

部分理论引用网络文献,若有侵权联系博主删除。

5 MATLAB代码与数据下载地址

见博客主页