视觉图像:matlab纹理分割编程实例

2025-11-05 14:31:05

1、读取图像:

%Step 1: Read Image

I1 = imread('bag.png');

I = rgb2gray(I1);

figure

imshow(I);

视觉图像:matlab纹理分割编程实例

2、创建纹理图像

使用纹理函数entropyfilt()创建纹理图像,返回与输入图像大小相同,每个像素值是输入图像相应像素值邻域的熵值;

使用比例函数rescale()变成double图像;

%Step 2: Create Texture Image

E = entropyfilt(I);%

Eim = rescale(E);%

figure

imshow(Eim);

视觉图像:matlab纹理分割编程实例

3、为底层纹理创建二值掩膜图像

①分割后的图像目标区域显示为白色;

②与原图像相比,图像顶部的纹理被过度分割,而底部纹理则以一个整体被分割出来;

③使用bwareaopen函数提取图像的底部纹理;

④分割图像的边界并不光滑,并且含有很多孔洞,使用imclose函数对图像执行形态学关操作;使用imfill函数对图像中的孔洞进行填充;

%Step 3: Create Rough Mask for the Bottom Texture

BW1 = imbinarize(Eim,0.8);%阈值分割

figure

imshow(BW1);

imwrite(BW1,'thresholdimg1.png');

BWao = bwareaopen(BW1,2000);%开运算,连通域

imshow(BWao)

nhood = true(11);

closeBWao = imclose(BWao,nhood);%闭合孔洞

imshow(closeBWao)

roughMask = imfill(closeBWao,'holes');%孔洞填充

imshow(roughMask)

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

4、使用掩膜图像分割出上层图像

%Step 4: Use Rough Mask to Segment the Top Texture

I2 = I;

I2(roughMask) = 0;%掩膜操作

imshow(I2);

imwrite(I2,'segmentimg1.png');

E2 = entropyfilt(I2);%重计算纹理图像

E2im = rescale(E2);

imshow(E2im)

BW2 = imbinarize(E2im);%自适应阈值分割

imshow(BW2);

mask2 = bwareaopen(BW2,1000);

imshow(mask2);

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

5、提取底层、上层图像和绘制边界:

%Step 5: Display Segmentation Results

texture1 = I;

texture1(~mask2) = 0;%上层图像

imshow(texture1)

texture2 = I;

texture2(mask2) = 0;%底层图像

figure

imshow(texture2)

imwrite(texture2,'bottomimg.png');

boundary = bwperim(mask2);

segmentResults = I;

segmentResults(boundary) = 255;%绘制边界

imshow(segmentResults)

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

6、除了使用entropyfilt函数,还可以使用标准差stdfilt()和rangefilt()来达到类似的分离效果;

% Step 6:Using Other Texture Filters in Segmentation

S = stdfilt(I,nhood);

imshow(rescale(S));

imwrite(rescale(S),'stdfiltimg.png');

R = rangefilt(I,ones(5));

imshow(R);

imwrite(R,'rangefiltimg.png');

视觉图像:matlab纹理分割编程实例

视觉图像:matlab纹理分割编程实例

7、【注】:matlab rescale()函数:

function y = rescale(x,a,b) 

% rescale - rescale data in [a,b] 

%   y = rescale(x,a,b); 

%   Copyright (c) 2004 Gabriel Peyr? 

if nargin<2 

    a = 0; 

end 

if nargin<3 

    b = 1; 

end 

m = min(x(:)); 

M = max(x(:)); 

y = (b-a) * (x-m)/(M-m) + a;

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢