Matlab底层源代码实现图像开闭操作(与Halcon效果一致)

理论基础

图像灰度的开闭操作属于图像处理的基础,这里主要展示根据原理,来编写底层的函数代码。

源代码

首先实现Matlab对图像区域的腐蚀与膨胀函数

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像膨胀操作函数
%Author:Zhu
%时间:2022.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function image_out = Dilation(srcImage,maskWidth,maskHeight)
   image_out = srcImage;
   image_matrix=double(srcImage);
   [height,width,~]=size(srcImage);


    for i=(maskHeight+1)/2:height-(maskHeight+1)/2
        for j=(maskWidth+1)/2:width-(maskWidth+1)/2
            maxValue = 0;
            %更新最大值
            for k=1:maskHeight
                for p=1:maskWidth
                    currentPixel = image_matrix(i-(maskHeight+1)/2+k,j-(maskWidth+1)/2+p);
                    if(currentPixel>maxValue)
                        maxValue = currentPixel;
                    end
                end
            end
            %更新卷积核中心的数值
            image_out(i,j)=maxValue;
        end
    end

    %图像边缘像素修改
    for i=1:height
        for j=1:width
            if(i>(maskHeight+1)/2 && i<height-(maskHeight+1)/2 && j>(maskWidth+1)/2 && j<width-(maskWidth+1)/2)
                image_out(i,j)= image_out(i,j);
            else
                image_out(i,j)= image_matrix(i,j);
            end
        end
    end
end

图像的腐蚀函数

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像腐蚀操作函数
%Author:Zhu
%时间:2022.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function image_out = Erosion(srcImage,maskWidth,maskHeight)
   image_out = srcImage;
   image_matrix=double(srcImage);
   [height,width,~]=size(srcImage);
   
    for i=(maskHeight+1)/2:height-(maskHeight+1)/2
        for j=(maskWidth+1)/2:width-(maskWidth+1)/2
            minValue = 255;
            %更新最大值
            for k=1:maskHeight
                for p=1:maskWidth
                    currentPixel = image_matrix(i-(maskHeight+1)/2+k,j-(maskWidth+1)/2+p);
                    if(currentPixel<minValue)
                        minValue = currentPixel;
                    end
                end
            end
            %更新卷积核中心的数值
            image_out(i,j)=minValue;
        end
    end

    %图像边缘像素修改
    for i=1:height
        for j=1:width
            if(i>(maskHeight+1)/2 && i<height-(maskHeight+1)/2 && j>(maskWidth+1)/2 && j<width-(maskWidth+1)/2)
                image_out(i,j)= image_out(i,j);
            else
                image_out(i,j)= image_matrix(i,j);
            end
        end
    end 
end

图像的开运算

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像的开操作
%Author:Zhu
%时间:2022.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
image = imread("E:\1.jpg");
image_matrix=image(:,:,1);

%输入的卷积核大小
maskHeight=11;
maskWidth=11;

%先腐蚀操作,再膨胀操作
middle = Erosion(image_matrix,maskWidth,maskHeight);
image_out =  Dilation(middle,maskWidth,maskHeight);

%显示图像
image_out = uint8(image_out);
subplot(1,2,1);
imshow(image);
subplot(1,2,2);
imshow(image_out);

图像的闭运算

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%图像的闭操作
%Author:Zhu
%时间:2022.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
image = imread("E:\1.jpg");
image_matrix=image(:,:,1);

%输入的卷积核大小
maskHeight=11;
maskWidth=11;

%先膨胀操作,再腐蚀操作
middle =  Dilation(image_matrix,maskWidth,maskHeight);
image_out = Erosion(middle,maskWidth,maskHeight);

%显示图像
image_out = uint8(image_out);
subplot(1,2,1);
imshow(image);
subplot(1,2,2);
imshow(image_out);

Halcon算子以及实现效果

*图像闭操作
gray_closing_rect (Image, ImageClosing, 11, 11)
*图像开操作
gray_opening_rect (Image, ImageOpening, 11, 11)

图像效果比对

Matlab开操作
Matlab底层源代码实现图像开闭操作(与Halcon效果一致)_第1张图片
Halcon开操作
Matlab底层源代码实现图像开闭操作(与Halcon效果一致)_第2张图片
Matlab闭操作
Matlab底层源代码实现图像开闭操作(与Halcon效果一致)_第3张图片
Halcon闭操作
Matlab底层源代码实现图像开闭操作(与Halcon效果一致)_第4张图片
效果一致

你可能感兴趣的:(matlab,matlab,图像处理,计算机视觉)