数字图像处理(下)

米粒图片处理

去除背景二值化

  1. 去除图像背景
I = imread('rice.png');
subplot(1,3,1); imshow(I);
BG = imopen(I, strel('disk', 15));
subplot(1,3,2); imshow(BG);
I2 = imsubtract(I, BG);
subplot(1,3,3); imshow(I2);

  1. 对图像二值化
I2 = imsubtract(I, BG); level=graythresh(I2);
bw2 = im2bw(I2, level); 

  1. 所用函数
    • graythresh() 使用最大类间方差法找到图片的一个合适的阈值(threshold)。
    • m2bw() 使用阈值(threshold)变换法把灰度图像(grayscale image)转换成二值图像。

计数

  • bwlable 计算连通区域,该函数使用了连通区域标记算法,将每个连通区域内的像素点赋值为同一个值.
  • 原理:


  • 代码实现:
I=imread('rice.png');
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); level=graythresh(I2);
BW=im2bw(I2, level);
[labeled, numObjects]=bwlabel(BW, 8);

彩色显示

  • 所用函数:lable2rgb()
  • 代码实现:
I=imread('rice.png');
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); level=graythresh(I2); 
BW=im2bw(I2, level); 
[labeled, numObjects]=bwlabel(BW, 8);
RGB_label=label2rgb(labeled); imshow(RGB_label);

结果分析

  • 所需函数:regionprops():可以将检测结果封装成结构体数组.
  • 代码实现
I=imread('rice.png'); 
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); level=graythresh(I2); 
BW=im2bw(I2, level); 
[labeled, numObjects]=bwlabel(BW, 8);
graindata = regionprops(labeled, 'basic');
graindata(51)
  • 运行结果:
       Area: 155
   Centroid: [112.4258 245.8645]
BoundingBox: [108.5000 234.5000 8 22]

鼠标交互

  • 所需函数:bwselect():鼠标选择连通区域.
  • 代码实现
I=imread('rice.png'); level=graythresh(I);
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); BW=im2bw(I2, graythresh(I2)); 
ObjI = bwselect(BW); imshow(ObjI);

原文地址:https://www.cnblogs.com/thrseven/p/15259632.html