影像分析

影响分析(上)

Read and Show An Image

  • imread()
  • imshow()
1
2
3
clear, close all
I = imread('pout.tif');
imshow(I);

immultiply()

1
2
3
4
I = imread('rice.png');
subplot(1,2,1); imshow(I);
J = immultiply(I, 1.5);
subplot(1,2,2); imshow(J); % J更亮了

imadd()

1
2
3
4
5
6
I = imread('rice.png');
J = imread('cameraman.tif');
K = imadd(I, J);
subplot(1,3,1); imshow(I);
subplot(1,3,2); imshow(K);
subplot(1,3,3); imshow(J);

两张图片相加会变亮,相加的条件是大小要相等

imhist()

1
imhist(I)

将每一个graylevel拿出来统计,得到一个统计图表,横坐标为0~255,纵坐标为frequency

histeq()

1
2
3
4
5
I = imread('pout.tif'); I2 = histeq(I);
subplot(1,4,1); imhist(I);
subplot(1,4,2); imshow(I);
subplot(1,4,3); imshow(I2);
subplot(1,4,4); imhist(I2);

对比度增加

imrotate()

1
2
3
4
5
6
I = imread('rice.png'); 
subplot(1,2,1);imshow(I);
J = imrotate(I, 35, 'bilinear');
subplot(1,2,2); imshow(J);
size(I)
size(J)

imwrite()

1
imwrite(I, 'pout2.png');

影像分析(下)

Problem Setup

对于下图,如何通过影像分析数到有多少颗米,米的大小有多大?

1

策略:

  1. 将该图二值化,即米变成白色,背景变成黑色
  2. 计算连在一起的白色的点数

Image Thresholding

1
I = imread('rice.png'); imhist(I);

A gray-level image can be turned into a binary image by using a threshold

通过观察直方图,选出一个值(threshold),对于每个像素,大于那个值的设置为白色,小于的设置为黑色

graythresh() and im2bw()

  • graythresh() find an optimal threshold level
  • im2bw() converts an images into binary image
1
2
3
4
5
6
7
I = imread('rice.png');
level = graythresh(I);
bw = im2bw(I, level);
subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imshow(bw);

由于部分亮的背景被误认为米粒而变白,所以二进制图里面会有一些闪光点,为了不让这些点影响计算米粒数量结果,需要想办法让背景更加纯洁,就需要割去原有的背景

Background Estimation

  • Estimation for the gray level of the background
1
2
3
I = imread('rice.png');
BG = imopen(I, strel('disk', 15));
imshow(BG);

BG就是该图的背景,要去掉背景,就将原图减去这个背景图

Background Subtraction

1
2
3
4
5
6
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); % I2 = I - BG
subplot(1,3,3); imshow(I2);

这样就能让背近更加纯净

Thresholding on Background Removed Image

1
2
3
4
5
6
7
I = imread('rice.png'); level = graythresh(I);
bw = im2bw(I, level);
subplot(1,2,1); imshow(bw);
BG = imopen(I, strel('desk', 15));
I2 = imsubtract(I, BG); level = graythresh(I2);
bw2 = im2bw(I2, level);
subplot(1,2,2); imshow(bw2);

可以见到由I2生成的bw2中没有了之前的闪点(sparkle)

这样让误差减少了很多

identify how many grains are there in the image

使用的算法:Connected-component Labeling

A procedure for assigning a unique label to each object

对于原来的binary image,每个像素要么1要么0;

对于每个区域的1,用不同的label来标记,1、2、3.。。

1
2
3
4
5
0 0 0 0 0 0 0
0 1 1 0 0 0 0
0 1 0 0 1 0 0
0 0 0 1 1 0 0
0 0 0 0 0 0 0

变成

1
2
3
4
5
0 0 0 0 0 0 0
0 1 1 0 0 0 0
0 1 0 0 2 0 0
0 0 0 2 2 0 0
0 0 0 0 0 0 0

Connected-component Labeling: bwlabel()

1
2
3
4
5
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);

labeled就是一个用不同lebal标记后的一个矩阵

看最大的label是多少就是米的数量有多少

  • What is the size of the largest grain?
  • what is the mean size of the grains?

Color-coding Objects: label2rgb()

  • Converts a label matrix into an RGB color image
  • Visualize the labeled regions
1
2
3
4
5
6
7
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);

label2rgb()就是添加不同颜色,无其他功能

Object Properties: regionprops()

  • Provedes a set of properties for each connected component

可以知道米的大小

1
2
3
4
5
6
7
8
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)
Donate? comment?