数字图像处理:如何通过背景相减提取物体轮廓?(附MATLAB实现代码)

数字图像处理:如何通过背景相减提取物体轮廓?

因为在数学建模比赛中需要用到数字图像处理,从0开始学习,自己把期间做的笔记整理下来。不积跬步无以至千里。

一、理论

1、图像的数字化采集

图像采样就是按照图像空间的坐标测量该位置上像素的灰度值,对连续图像f(x,y)进行等间隔采样:

公式1
f ( x , y ) = [ f ( 0 , 0 ) f ( 0 , 1 ) … f ( 0 , N − 1 ) f ( 1 , 0 ) f ( 1 , 1 ) … f ( 1 , N − 1 ) ⋯ f ( M − 1 , 0 ) f ( M − 1 , 1 ) … f ( M − 1 , N − 1 ) ] f(x, y)=\left[\begin{array}{lll}{f(0,0)} & {f(0,1)} & {\dots} & {f(0, N-1)} \\ {f(1,0)} & {f(1,1)} & {\dots} & {f(1, N-1)} \\ {\cdots} & {} & {} \\ {f(M-1,0)} & {f(M-1,1)} & {\dots} & {f(M-1, N-1)}\end{array}\right] f(x,y)= f(0,0)f(1,0)f(M1,0)f(0,1)f(1,1)f(M1,1)f(0,N1)f(1,N1)f(M1,N1)

2、 高斯降噪

高斯降噪的原理是利用高斯函数作为模板,与原始图像进行卷积达到降噪的目的。

二维的高斯函数如下:

公式2
f ( x , y ) = ( 1 / 2 π δ 2 ) ∗ e − x 2 + y 2 2 δ 2 \mathrm{f}(\mathrm{x}, \quad \mathrm{y})=\left(1 / 2 \pi \delta^{2}\right) * \mathrm{e}^{-\frac{\mathrm{x}^{2}+\mathrm{y}^{2}}{2 \delta^{2}}} f(x,y)=(1/2πδ2)e2δ2x2+y2

3、图片灰度化

RGB颜色模式并不能反映图像的形态特征,只是从光学的原理上进行颜色的调配。图像灰度化处理可以作为图像处理的预处理步骤:

将RGB分量以不同的权值进行加权平均,得到较合理的灰度图像:

公式3
Gray ⁡ ( i , j ) = 0.299 ∗ R ( i , j ) + 0.578 ∗ G ( i , j ) + 0.114 ∗ B ( i , j ) \operatorname{Gray}(i, j)=0.299 * R(i, j)+0.578 * G(i, j)+0.114 * B(i, j) Gray(i,j)=0.299R(i,j)+0.578G(i,j)+0.114B(i,j)

4、直方图均衡化

直方图均衡化是将原图像通过某种变换,得到一幅灰度直方图为均匀分布的新图像的方法,从而达到清晰图像的目的。

https://blog.csdn.net/spongebob1234/article/details/77778709
M为图像行数, 为图像列数, 为灰度值, 为灰度级的个数

5、伽马变换

在图像处理中,将漂白(相机过曝)的图片或者过暗(曝光不足)的图片,通过拉伸和压缩灰度级进行图片的灰度变换。
r e s u l t ( i , j ) = c × i m g ( i , j ) γ result(i, j) = c{\times}img(i, j)^{\gamma} result(i,j)=c×img(i,j)γ

6、背景相减

result ⁡ ( i , j ) = { 0 , dis ⁡ { [ ( i , j ) , ( x 0 , y 0 ) ] } > 270 1 , i m g ( i , j ) − i m g 2 ( i , j ) > 10 0 ,  others  \operatorname{result}(i, j)=\left\{\begin{array}{lr}{0,} & {\operatorname{dis}\left\{\left[(i, j),\left(x_{0}, y_{0}\right)\right]\right\}>270} \\ {1,} & {i m g(i, j)-i m g 2(i, j)>10} \\ {0,} & {\text { others }}\end{array}\right. result(i,j)= 0,1,0,dis{ [(i,j),(x0,y0)]}>270img(i,j)img2(i,j)>10 others 

result表示结果矩阵, 为点 到点 的距离
img表示前景图像矩阵
img2表示背景图像矩阵

7、连通性

八连通或者四连通

二、代码实现

1、图片预处理

%imagePreprocessing.mlx 图片预处理文件
%路径变量
imgPath = "2019 APMCM Problem A Attachment";        % 图像库路径
imgPath2 = "colorGray"
imgOutPutPath = "gray"
histOutPutPath = "process_image"
%处理灰色图

imgDir  = dir(imgPath); % 遍历所有bmp格式文件
for i = 1 : length(imgDir) %遍历结构体
    if imgDir(i).name == "." || imgDir(i).name == ".."
        continue;
    end
%导入图片
    name = imgDir(i).name
    imgPathName = strcat(imgPath, "\", name)
    img = imread(imgPathName); %读取每张图片
    
%高斯降噪
    h_gaosi1=fspecial('gaussian',3,1);
    imageAfter=imfilter(img,h_gaosi1);
    imshow(imageAfter)
%灰度处理
grayImg = rgb2gray(img)  
grayImg = im2double(grayImg);
result = 1 * (grayImg .^ 2); 
saveImgName = strcat(imgOutPutPath, "\", "gray_", name)
imwrite(result, saveImgName);
imhist(result);
ylabel('出现频数');
end


%处理彩图
imgDir2  = dir(imgPath2); % 遍历所有bmp格式文件
for i = 1 : length(imgDir2) %遍历结构体
    if imgDir2(i).name == "." || imgDir2(i).name == ".."
        continue;
    end
%导入图片
    name = imgDir2(i).name
    imgPathName = strcat(imgPath2, "\", name)
    img = imread(imgPathName); %读取每张图片
    
%高斯降噪
    h_gaosi1=fspecial('gaussian',3,1);
    imageAfter=imfilter(img,h_gaosi1);
    imshow(imageAfter)
%灰度处理
grayImg = rgb2gray(img)
result = grayImg
saveImgName = strcat(imgOutPutPath, "\", "gray_", name)
imwrite(result, saveImgName);
imhist(result);
ylabel('出现频数');
end

2、背景相减

%ACPC.mxl 背景减除及二值化
clear;
clc;
% i1=imread('D:\MATLAB\ACPCM\IMG_BW\516_bw.bmp');
% i2=imread('D:\MATLAB\ACPCM\IMG_BW\610_bw.bmp');
% i1=rgb2gray(i1);
% i2=rgb2gray(i2);
i1=imread('D:\MATLAB\ACPCM\gray\gray_0502.bmp');
i2=imread('D:\MATLAB\ACPCM\gray\gray_0610.bmp');
[m,n]=size(i1);
im1=double(i1);
im2=double(i2);
i3=zeros(size(i1));
for i=1:m
    for j=1:n
        if (((i-561)*(i-561))+((j-1047)*(j-1047))) > 270*270%研究区域半径占270个像素点
              i3(i,j)=0;
        elseif abs((im1(i,j))-(im2(i,j))) > 8%最佳阈值在8~30之间
              i3(i,j)=1;
        else 
              i3(i,j)=0;
        end
    end
end
for i=10:m-10
    for j=10:n-10
        if i3(i-1,j-1)==1 && i3(i,j-1)==1 && i3(i-1,j)==1 && i3(i+1,j+1)==1  && i3(i-1,j+1)==1 && i3(i+1,j)==1 && i3(i,j+1)==1 && i3(i+1,j-1)==1 
            i3(i,j)=1;
        elseif i3(i-1,j-1)==0 && i3(i,j-1)==0 && i3(i-1,j)==0 && i3(i+1,j+1)==0  && i3(i-1,j+1)==0 && i3(i+1,j)==0 && i3(i,j+1)==0 && i3(i+1,j-1)==0 
            i3(i,j)=0; 
        end
    end
end
figure,imshow(i3);title('the result of 0502')
imfill(i3, 'hole');
contour = bwperim(i3);
figure,imshow(contour);title('the contour of 0502')

猜你喜欢

转载自blog.csdn.net/He_r_o/article/details/103355788