已知如下一张雪花的图片,怎么提取特定半径雪花呢?
matlab官方demo:http://cn.mathworks.com/help/images/examples/granulometry-of-snowflakes.html
- 读取图片并增强对比度
%雪花粒度测量法
%http://cn.mathworks.com/help/images/examples/granulometry-of-snowflakes.html
clc,clear
I = imread('snowflakes.png');
figure
imshow(I)
% Enhance Contrast 增强对比度
claheI = adapthisteq(I,'NumTiles',[10 10]);%将图像a进行直方图均衡化
figure,imshow(claheI)
claheI = imadjust(claheI);%调节灰度图像的亮度或彩色图像的颜色矩阵。
figure,imshow(claheI)
图片前后变化:
2. 进行开运算并统计保留下来的像素数量
radius_range = 0:22;
intensity_area = zeros(size(radius_range));
for counter = radius_range
% strel('disk', counter)表示创建一个圆盘用来消除半径小于counter个像素的元素
%效果为直接开运算
remain = imopen(claheI, strel('disk', counter));
%remain中半径小于counter个像素的元素已经被删除
%有强烈对比的图片中留下来的像素个数(非零的),共23个结果
intensity_area(counter +1) = sum(remain(:)); %counter +1是因为下标索引必须为正整数类型或逻辑类型。
end
figure
plot(intensity_area, 'm - *')
grid on
title('图像保留下来的像素数量')
xlabel('打开时使用的构造器半径大小(像素)')
ylabel('打开图片的像素数量(高对比的)')
其中创建了23个结构元素进行腐蚀和膨胀,得到23个结果。imopen为开运算
- 统计像素值差异
intensity_area_prime = diff(intensity_area); %即intensity_area中像素个数差异,差异绝对值小说明差的不多
figure
plot(intensity_area_prime, 'm - *')
grid on
title('雪花粒度大小分布')
ax = gca;
ax.XTick = [0 2 4 6 8 10 12 14 16 18 20 22];
xlabel('打开时使用的构造器半径大小(像素)')
ylabel('此半径下的像素值差异')
4.提取5像素半径的雪花
%提取5像素半径的雪花
open5 = imopen(claheI,strel('disk',5));%5,6时的像素差异最大
figure,imshow(open5),title('open5')
open6 = imopen(claheI,strel('disk',6));
figure,imshow(open6),title('open6')
%open5 - open6,不是反过来,如果反过来图像就没有了(根据形态学腐蚀的道理:半径大的结构元素进行腐蚀,留下来的图像就小)
rad5 = imsubtract(open5,open6);
figure,imshow(rad5,[])
open5和open6:
提取出来的雪花: