-
Laplace-Gaussian滤波算子在指纹增强中的应用
2021-01-27 17:31:58本文介绍了传统的滤波方法的分类,在此基础了引出了本文的研究内容。通过分析Laplace-Gaussian算子边缘检测的原理,然后分析了Laplace-Gaussian滤波器对指纹图像增强的处理,对滤波器的掩模尺寸和参数选择进行了详细... -
数字图像处理编程基础:Python语言不调用OpenCV函数实现Laplace算子锐化图像
2019-07-07 10:49:26Python语言不调用OpenCV函数实现Laplace算子锐化图像编译环境前言离散Laplace滤波模板不带对角项的滤波模板带有对角项的扩展模板Lpalace算子的数学定义实例分析代码展示运行结果结果对比结论 编译环境 编程语言:...Python语言不调用OpenCV函数实现Laplace算子锐化图像
编译环境
编程语言:Python
IDE:PyCharm2017前言
锐化处理的主要目的是突出灰度的过渡部分。图像锐化的用途多种多样,应用范围从电子印刷和医学成像到工业检测和军事系统的制导等。由之前的博客了解到,图像模糊可通过在空间域用像素领域平均法实现。因为均值处理和积分类似,在逻辑上,我们可以得出锐化处理可由空间微分来实现这一结论。基本上,微分算子的响应强度与图像在用算子操作的这一点的突变成都成正比,这样图像微风会增强边缘和其他突变(如噪声),削弱灰度变换缓慢的区域。
离散Laplace滤波模板
不带对角项的滤波模板
w=
0 1 0
1 -4 1
0 1 0带有对角项的扩展模板
w=
1 1 1
1 -8 1
1 1 1Lpalace算子的数学定义
如果使用的模板中心系数为正时,c=1,如果中心系数为负时,c= -1实例分析
下图是月球北极的一副略显模糊的图像,要对其进行空间锐化处理。
代码展示
import cv2 from pylab import * import numpy as np def Image_cal(Image,k): # Image是输入图像,k是标定图像灰度最大值 im=array(Image) img=[] fm=im-np.min(im) maxfm=np.max(fm) for i in range(im.shape[0]): for j in range(im.shape[1]): img[i,j]=k * (fm[i,j] / maxfm) return img def Laplace(Image,a): #输入图像Image,模板a im=array(Image) img=array(Image) dim=math.sqrt(len(a)) # 模板的维度dim w=im.shape[0] # 计算输入图像的宽高 h=im.shape[1] b=[] # 待处理的与模板等大小的图像块,分BGR通道 g=[] r=[] if sum(a)==0: # 判断模板a的和是否为0 A=1 else: A=sum(a) if size(im.shape)==3: # 判断图像为灰度图像还是RGB图像 for i in range(int(dim/2),w-int(dim/2)): for j in range(int(dim/2),h-int(dim/2)): for m in range(-int(dim/2),-int(dim/2)+int(dim)): for n in range(-int(dim / 2), -int(dim / 2) + int(dim)): b.append(im[i+m,j+n,0]) g.append(im[i+m,j+n,1]) r.append(im[i+m,j+n,2]) img[i,j,0]=sum(np.multiply(np.array(a),np.array(b)))/A img[i, j, 1] =sum(np.multiply(np.array(a),np.array(g)))/A img[i, j, 2] =sum(np.multiply(np.array(a),np.array(r)))/A b=[];g=[];r=[] else: for i in range(int(dim/2),w-int(dim/2)): for j in range(int(dim/2),h-int(dim/2)): for m in range(-int(dim/2),-int(dim/2)+int(dim)): for n in range(-int(dim / 2), -int(dim / 2) + int(dim)): b.append(im[i+m,j+n]) img[i,j]=sum(np.multiply(np.array(a),np.array(b)))/A b=[] return img img=cv2.imread('book\\0338.tif') a=[0,-1,0,-1,5,-1,0,-1,0] im=Laplace(img,a) cv2.imshow('Origin',img) cv2.imshow('Laplace',im) cv2.waitKey() cv2.destroyAllWindows()
运行结果
结果对比
可以看出处理之后的图像轮廓更清晰结论
拉普拉斯算子是锐化数字图像的一种重要工具,能够有效的应用于各个领域。
-
【数字图像处理实验】中值滤波、均值滤波、灰度图像的锐化处理(包括Sobel、Prewitt、Roberts、Laplace、...
2020-08-03 12:36:12目录1 原理1.1 中值滤波1.2 均值滤波1.3 图像锐化1.4 边缘检测2 实现源代码(MATLAB)2.1 中值滤波2.2 均值滤波2.3 锐化处理2.3.0 说明2.3.1 Laplace算子2.3.2 Sobel算子2.3.3 Prewitt算子2.3.4 Roberts算子2.4 边缘...目录
1 原理
1.1 中值滤波
主要用来处理椒盐噪声。椒盐噪声的出现使得该点像素比周围的像素亮(暗)很多,而如果在某个模板中对像素由小到大进行重新排列,那么最亮的或者最暗的点一定被排在两侧。这时取模板中排在中间位置上的像素的灰度值替代待处理像素的值,就可以达到滤除噪声的目的。
- 将模板中心与像素位置重合
- 读取模板下各对应像素的灰度值
- 将这些像素从小到大排成1列
- 找出这些值里排在中间的1个
- 将这个中间值赋给模板中心位置像素。
1.2 均值滤波
采用邻域平均法。基本思想是用几个像素灰度的平均值来代替每个像素的灰度。如使用一个3*3大小的模板对图像进行滤波,每次移动模板后,对模板范围内的所有像素灰度值相加再除以系数9,四舍五入后作为模板中心像素的灰度值。
1.3 图像锐化
图像锐化的目的是为了突出图像的边缘信息,加强图像的轮廓特征,便于人眼或者机器的识别。图像中边缘和轮廓往往出现于图像中灰度图片的地方,而检查这些图片可以用微分实现,因而图像锐化主要是通过微分方法进行的。
由于处理的对象是数字图像,其像素是离散的,对微分形式进行一些近似处理,得到离散的模板矩阵,称为算子。常见的算子诸如Laplacians、Roberts、Sobel等。用这些算子作为模板对图像进行处理,就能得到图像的轮廓图。将轮廓图与原图像相加即可得到原图像的锐化结果。1.4 边缘检测
在图像锐化得到的轮廓图的基础上,根据轮廓图的灰度直方图设立阈值(一般在两峰一谷之间),对其进行二值化处理即可。
2 实现源代码(MATLAB)
2.1 中值滤波
function res = medianfilter(mat, width, height) % res = medianfilter(img, width, height) % -- res the result of the median filter process % -- img the input image matrix(should be 2 dimensions) % -- width the width of template % -- height the height of template % e.g.: % filename='noise.jpg'; % img=imread(filename); % width=3;height=3; % res = medianfilter(img, width, height); % imshow(res); % mkdir('results/medianfilter'); % imwrite(res,['./results/medianfilter/',filename]); res=im2uint8(zeros(size(mat))); [rows,cols]=size(mat); for i = 1:(rows-height + 1) for j = 1:(cols-width + 1) temp = mat(i:i+height-1, j:j+width-1); temp = sort(temp(:)); res((i+i+height-1)/2, (j+j+width-1)/2) = temp((length(temp)+1)/2); end end
2.2 均值滤波
function res = meanfilter(mat, width, height) % res = medianfilter(img, width, height) % -- res the result of the median filter process % -- img the input image matrix(should be 2 dimensions) % -- width the width of template % -- height the height of template % e.g.: % filename='noise.jpg'; % img=imread(filename); % width=3;height=3; % res = meanfilter(img, width, height); % imshow(res); % mkdir('results/meanfilter'); % imwrite(res,['./results/meanfilter/',filename]); res=im2uint8(zeros(size(mat))); [rows,cols]=size(mat); for i = 1:(rows-height + 1) for j = 1:(cols-width + 1) temp = mat(i:i+height-1, j:j+width-1); res((i+i+height-1)/2, (j+j+width-1)/2) = round(mean(temp(:))); end end
2.3 锐化处理
2.3.0 说明
在分别实现使用Sobel、Prewitt、Roberts、Laplace算子的滤波函数后,希望实现一个“万能滤波函数”。
搜索得知MATLAB内置的滤波函数为imfilter,支持输入图像、模板等参数,输出滤波后结果。其对“最外面一圈像素”的处理方式是在最外层“加一圈0像素”,并对全图像进行处理。
仿照其功能实现myfilter函数,代码如下:function res=myfilter(I,H) % function res=myfilter(I,H) % -- res the result of the filter process % -- I the input image % -- H the input template matrix(should be 2 dimensions) % e.g.: % filename='锐化及边缘检测用途.jpg'; % I=imread(filename); % H=[1,1,1;1,-8,1;1,1,1]; % res=myfilter(I,H); % imshow(res); % mkdir('results/myfilter'); % imwrite(res,['./results/myfilter/',filename]); I=im2uint8(I); H=int16(H); [rows,cols,channels]=size(I); res=uint8(zeros(rows,cols,channels)); [height,width]=size(H); hh=(height+1)/2; hw=(width+1)/2; for k=1:channels chan=int16(zeros(rows+hh,cols+hw)); chan(hh:rows+hh-1,hw:cols+hw-1)=I(:,:,k); for i=1:rows for j=1:cols temp=chan(i:i+hh,j:j+hw); res(i,j,k)=sum(sum(H.*temp)); end end end
通过如下代码实现两者效果对比:
I=imread('锐化及边缘检测用途.jpg'); H=[1,1,1;1,-8,1;1,1,1]; imres=imfilter(I,H); myres=myfilter(I,H); cmp=(imres==myres); length(cmp(cmp==0))
得到结果为:
ans = 0
于是复用myfilter函数,以另一种方式实现了Sobel、Prewitt、Roberts、Laplace算子滤波函数,在原命名后添加了“2”以区分。
需要注意的是,myfilter最多算是imfilter的一个弱化版本,运行速度远不及imfilter。另外,经查阅,imfilter默认使用的是’corr’参数,而不是’conv’,即默认使用的不是卷积运算。但本实验遇到的算子(Sobel、Prewitt、Laplace)模板都可以认为是对称的,实验得知本实验中对这三类算子在’conv’和’corr’两种参数下结果是完全一致的,故myfilter也没有提供诸如’corr’和’conv’之类参数的支持(即不支持模板矩阵不对称的卷积运算,仅支持相关运算)。2.3.1 Laplace算子
采用的laplacian operator为[1,1,1;1,-8,1;1,1,1],故若要得到锐化图像,应该使用原图像减去该函数计算结果再输出。
function laplacian=laplacianfilter(image) % function laplacian=laplacianfilter(image) % -- laplacian the result of the laplacian operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=laplacianfilter(img); % imshow(res); % mkdir('results/laplacianfilter'); % imwrite(res,['./results/laplacianfilter/',filename]); image=im2uint8(image); [rows,cols,channels]=size(image); laplacian=uint8(zeros(rows,cols,channels)); for k=1:channels chan=int16(zeros(rows+2,cols+2)); chan(2:rows+1,2:cols+1)=image(:,:,k); for i=1:rows for j=1:cols temp=chan(i:i+2, j:j+2); laplacian(i,j,k)=sum(temp(:))-9*chan(i+1,j+1); end end end
复用myfilter函数实现:
function laplacian=laplacianfilter2(image) % function laplacian=laplacianfilter2(image) % -- laplacian the result of the laplacian operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=laplacianfilter2(img); % imshow(res); % mkdir('results/laplacianfilter'); % imwrite(res,['./results/laplacianfilter/',filename]); laplacianoperator=[1,1,1;1,-8,1;1,1,1]; laplacian=myfilter(image,laplacianoperator);
2.3.2 Sobel算子
function sobel=sobelfilter(image) % function sobel=sobelfilter(image) % -- sobel the result of the sobel operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=sobelfilter(img); % imshow(res); % mkdir('results/sobelfilter'); % imwrite(res,['./results/sobelfilter/',filename]); sobeloperator1=int16([-1,0,1;-2,0,2;-1,0,1]); sobeloperator2=int16([-1,-2,-1;0,0,0;1,2,1]); image=im2uint8(image); [rows,cols,channels]=size(image); sobel1=uint8(zeros(rows,cols,channels)); sobel2=uint8(zeros(rows,cols,channels)); for k=1:channels chan=int16(zeros(rows+2,cols+2)); chan(2:rows+1,2:cols+1)=image(:,:,k); for i=1:rows for j=1:cols temp=chan(i:i+2,j:j+2); sobel1(i,j,k)=sum(sum(sobeloperator1.*temp)); sobel2(i,j,k)=sum(sum(sobeloperator2.*temp)); end end end sobel=abs(sobel1)+abs(sobel2);
复用myfilter函数实现:
function sobel=sobelfilter2(image) % function sobel=sobelfilter2(image) % -- sobel the result of the sobel operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=sobelfilter2(img); % imshow(res); % mkdir('results/sobelfilter'); % imwrite(res,['./results/sobelfilter/',filename]); sobeloperator1=[-1,0,1;-2,0,2;-1,0,1]; sobeloperator2=[-1,-2,-1;0,0,0;1,2,1]; sobel=myfilter(image,sobeloperator1)+myfilter(image,sobeloperator2);
2.3.3 Prewitt算子
function prewitt=prewittfilter(image) % function prewitt=prewittfilter(image) % -- prewitt the result of the prewitt operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=prewittfilter(img); % imshow(res); % mkdir('results/prewittfilter'); % imwrite(res,['./results/prewittfilter/',filename]); image=im2uint8(image); [rows,cols,channels]=size(image); prewitt1=uint8(zeros(rows,cols,channels)); prewitt2=uint8(zeros(rows,cols,channels)); for k=1:channels chan=int16(zeros(rows+2,cols+2)); chan(2:rows+1,2:cols+1)=image(:,:,k); for i=1:rows for j=1:cols temp=chan(i+2,j:j+2)-chan(i,j:j+2); prewitt1(i,j,k)=sum(temp(:)); temp=chan(i:i+2,j+2)-chan(i:i+2,j); prewitt2(i,j,k)=sum(temp(:)); end end end prewitt=prewitt1+prewitt2;
复用myfilter函数实现:
function prewitt=prewittfilter2(image) % function prewitt=prewittfilter2(image) % -- prewitt the result of the prewitt operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=prewittfilter2(img); % imshow(res); % mkdir('results/prewittfilter'); % imwrite(res,['./results/prewittfilter/',filename]); prewittoperator1=[-1,-1,-1;0,0,0;1,1,1]; prewittoperator2=[-1,0,1;-1,0,1;-1,0,1]; prewitt=myfilter(image,prewittoperator1)+myfilter(image,prewittoperator2);
2.3.4 Roberts算子
为方便计算,将Roberts算子修改为3*3矩阵,复用myfilter函数实现:
function roberts=robertsfilter2(image) % function roberts=robertsfilter2(image) % -- roberts the result of the laplacian operator filter process % -- image the input image % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % res=robertsfilter2(img); % imshow(res); % mkdir('results/robertsfilter2'); % imwrite(res,['./results/robertsfilter2/',filename]); robertsoperator1=[0,0,0;0,-1,0;0,0,1]; robertsoperator2=[0,0,0;0,0,-1;0,1,0]; roberts=myfilter(image,robertsoperator1)+myfilter(image,robertsoperator2);
2.4 边缘检测
同样地,参考MATLAB内置边缘检测函数edge实现了其一个简化版本myedge:支持直接输入模板矩阵,或者输入字符串以调用对应模板矩阵;没有自动阈值选择,需要手动输入)。代码如下:
function res=myedge(img,operator,threshold) % function res=myedge(img,operator,threshold) % -- res the edge result of the input image % -- image the input image % -- operator the input operator(should be 2 dimensional odd-order matrix) or string like 'laplacian'/'sobel'/'prewitt'/'roberts' % -- threshold threshold to decide whther a pixel on the edge should be black or white % e.g.: % filename='锐化及边缘检测用途.jpg'; % img=imread(filename); % operator='laplacian'; % threshold=233; % res=myedge(img,operator,threshold); % imshow(res); % mkdir(['results/myedge/',operator]); % imwrite(res,['./results/myedge/',operator,'/',filename]); if ischar(operator) switch operator case 'laplacian' res=laplacianfilter2(img); case 'sobel' res=sobelfilter2(img); case 'prewitt' res=prewittfilter2(img); case 'roberts' res=robertsfilter2(img); end else res=myfilter(img,operator); end res(res<threshold)=0;res(res>=threshold)=255;
3 实验结果
3.1 中值滤波(使用3*3大小模板)
编写了通用函数,可以在输入参数中指定模板大小。
3.2 均值滤波(使用3*3大小模板)
编写了通用函数,可以在输入参数中指定模板大小。
3.3 锐化处理(注:锐化效果未叠加到原图,展示图为轮廓图)
3.3.0原图
3.3.1 Laplace算子
3.3.2 Sobel算子
3.3.3 Prewitt算子
3.3.4 Roberts算子
3.4 边缘检测
以laplacian operator为模板矩阵,阈值为233。其余结果类似,可参照help myedge中的例子,调用myedge函数生成。
注:遇到的一个问题是,边缘检测应当是只有0和255两种灰度的图像,在MATLAB中生成以后使用imshow查看没有问题,但保存为.jpg格式后出现了其他灰色。再次读取该.jpg图片,数值上与原来MATLAB生成图像并不一致,猜测可能是压缩编码为.jpg格式时出现了损失。
-
数字图像处理实验(2)--中值滤波/均值滤波.7z
2020-04-07 14:47:39滤波模板的大小自定(可为3×3、5×5、7×7、15×15等)。实验图像可从提供的实验图像集中的噪声图像中选取。 思考题:(选做) 编程实现灰度图像的均值滤波平滑处理;也可尝试实现灰度图像的锐化处理,包括Sobel、... -
数字图像处理实验(2)——中值滤波/均值滤波
2020-04-07 14:38:46滤波模板的大小自定(可为3×3、5×5、7×7、15×15等)。实验图像可从提供的实验图像集中的噪声图像中选取。 思考题:(选做) 编程实现灰度图像的均值滤波平滑处理;也可尝试实现灰度图像的锐化处理,包括Sobel、...实验题目:
编程实现灰度图像的中值滤波平滑处理。滤波模板的大小自定(可为3×3、5×5、7×7、15×15等)。实验图像可从提供的实验图像集中的噪声图像中选取。
思考题:(选做)
编程实现灰度图像的均值滤波平滑处理;也可尝试实现灰度图像的锐化处理,包括Sobel、Prewitt、Roberts、Laplace、Canny边缘检测等。源码:
均值滤波
function ava(n) imNew=im2double(imread('noise.jpg')); len=floor(n/2); a(1:n,1:n)=1; %a即n×n模板,元素全是1 %对原始图像进行扩展,复制边界的值 imNew_pad=padarray(imNew,[len,len],'symmetric'); [M,N]=size(imNew_pad); New=zeros(size(imNew)); for i=1+len:M-len for j=1+len:N-len c=imNew_pad(i-(n-1)/2:i+(n-1)/2,j-(n-1)/2:j+(n-1)/2).*a; %取出x1中从(i,j)开始的n行n列元素与模板相乘 s=sum(sum(c)); %求c矩阵中各元素之和 New(i-(n-1)/2,j-(n-1)/2)=s/(n*n); %将与模板运算后的各元素的均值赋给模板中心位 end end figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(New); title(['灰度图像的均值滤波平滑处理',num2str(n),'X',num2str(n),'图像']); end
中值滤波
// 中值函数 function mid=mid(A) len=length(A); for i = 1:len for j = 1:len-i if A(j) > A(j+1) temp = A(j); %核心代码 A(j) = A(j+1); A(j+1) = temp; end end end mid=A(floor(len/2)+1); end //中值滤波 function mido(bianSize) imNew=im2double(imread('noise.jpg')); %扩展区域的行列数,floor取整 len=floor(bianSize/2); %对原始图像进行扩展,没有参数默认0填充 imNew_pad=padarray(imNew,[len,len]); [M,N]=size(imNew_pad); New=zeros(size(imNew)); for i=1+len:M-len for j=1+len:N-len %从扩展图像中,取出局部图像 Block=imNew_pad(i-len:i+len,j-len:j+len); %将多维矩阵转换为一维数组 Block=Block(:); %取这组数的中值,赋值给输出图像 New(i-len,j-len)=mid(Block); end end figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(New); title(['灰度图像的中值滤波平滑处理',num2str(bianSize),'X',num2str(bianSize),'图像']); end
Prewitt算子
function Prewitt imNew=im2double(imread('锐化及边缘检测用途.jpg')); [M , N, R]=size(imNew); for i=2:M-1 for j=2:N-1 Dx=[imNew(i+1,j-1)-imNew(i-1,j-1)]+[imNew(i+1,j)-imNew(i-1,j)]+[imNew(i+1,j+1)-imNew(i-1,j+1)]; Dy=[imNew(i-1,j+1)-imNew(i-1,j-1)]+[imNew(i,j+1)-imNew(i,j-1)]+[imNew(i+1,j+1)-imNew(i+1,j-1)]; P(i,j)=sqrt(Dx^2+Dy^2); end end for i=1:M-1 for j=1:N-1 if (P(i,j)<0.5) P(i,j)=1; else P(i,j)=0; end end end figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(P,[]); title('Prewitt边缘检测'); %画出边缘检测后的图像 end
Roberts算子
function Roberts imNew=im2double(imread('锐化及边缘检测用途.jpg')); [M,N]=size(imNew); newimNew=imNew;%为保留图像的边缘一个像素 for j=1:M-1 %进行边界提取 for k=1:N-1 robertsNum = abs(imNew(j,k)-imNew(j+1,k+1)) + abs(imNew(j+1,k)-imNew(j,k+1)); newimNew(j,k)=robertsNum; end end figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(newimNew); title('Roberts边缘检测'); %画出边缘检测后的图像 end
Sobel算子
function Sobel imNew=im2double(imread('锐化及边缘检测用途.jpg')); [M,N] = size(imNew); % 获得图像的高度和宽度 F2 = double(imNew); U = double(imNew); uSobel = imNew; for i = 2:M - 1 %sobel边缘检测 for j = 2:N - 1 Gx = (U(i+1,j-1) + 2*U(i+1,j) + F2(i+1,j+1)) - (U(i-1,j-1) + 2*U(i-1,j) + F2(i-1,j+1)); Gy = (U(i-1,j+1) + 2*U(i,j+1) + F2(i+1,j+1)) - (U(i-1,j-1) + 2*U(i,j-1) + F2(i+1,j-1)); uSobel(i,j) = sqrt(Gx^2 + Gy^2); end end figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(im2uint8(uSobel)); title('Sobel边缘检测'); %画出边缘检测后的图像 end
交互面板
function varargout = mmm2(varargin) % MMM2 MATLAB code for mmm2.fig % MMM2, by itself, creates a new MMM2 or raises the existing % singleton*. % % H = MMM2 returns the handle to a new MMM2 or the handle to % the existing singleton*. % % MMM2('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in MMM2.M with the given input arguments. % % MMM2('Property','Value',...) creates a new MMM2 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before mmm2_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to mmm2_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help mmm2 % Last Modified by GUIDE v2.5 23-Mar-2020 23:58:07 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @mmm2_OpeningFcn, ... 'gui_OutputFcn', @mmm2_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before mmm2 is made visible. function mmm2_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to mmm2 (see VARARGIN) % Choose default command line output for mmm2 handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes mmm2 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = mmm2_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) clear; mido(3) % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) clear; mido(5) % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) clear; mido(7); % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) clear; mido(9) % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) clear; ava(3) % --- Executes on button press in pushbutton8. function pushbutton8_Callback(hObject, eventdata, handles) clear; ava(5) % --- Executes on button press in pushbutton9. function pushbutton9_Callback(hObject, eventdata, handles) clear; ava(7) % --- Executes on button press in pushbutton10. function pushbutton10_Callback(hObject, eventdata, handles) clear; ava(9) % --- Executes on button press in pushbutton12. function pushbutton12_Callback(hObject, eventdata, handles) clear; Sobel(); % --- Executes on button press in pushbutton13. function pushbutton13_Callback(hObject, eventdata, handles) clear; Prewitt(); % --- Executes on button press in pushbutton14. function pushbutton14_Callback(hObject, eventdata, handles) clear; Roberts(); % --- Executes on button press in pushbutton16. function pushbutton16_Callback(hObject, eventdata, handles) clear; imNew=im2double(imread('锐化及边缘检测用途.jpg')); figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(imNew); title('原图2'); % --- Executes on button press in pushbutton17. function pushbutton17_Callback(hObject, eventdata, handles) clear; imNew=im2double(imread('noise.jpg')); figure('toolbar','none','menubar','none'); set (gca,'position',[0.05,0.03,0.90,0.90] ); imshow(imNew); title('原图1');
实验效果
原图
第一排是中值滤波,第二排是均值滤波,
从左到右是3X3,5X5,7X7,9X9模板大小
从左到右分别是Sobel,Prewitt,Roberts算子
-
图像分割之(五)边缘检测Laplace详解
2017-12-18 21:06:05LaplaceLaplace ... 使用二阶微分算子的基本方法是定义一种二阶微分的离散形式,然后根据这个形式生成一个滤波模板,与图像卷积。 对于二维图像f(x,y),二阶微分最简单的定义(拉普拉斯算子):∇2I=∂Laplace
数学基础
拉普拉斯算子,二阶微分线性算子,与一阶微分相比,二阶微分的边缘定位能力更强,锐化效果更好。
使用二阶微分算子的基本方法是定义一种二阶微分的离散形式,然后根据这个形式生成一个滤波模板,与图像卷积。
对于二维图像f(x,y),二阶微分最简单的定义(拉普拉斯算子):∇2I=∂2I∂x2+∂2I∂y2对于任意阶微分算子都是线性算子,所以二阶微分算子可以用生成模板然后卷积的方式得出结果。
我们在像素点(i,j)的3×3的邻域内,可以有如下的近似:∂2I∂x2=I(i,j+1)−2I(i,j)+I(i,j−1)∂2I∂y2=I(i+1,j)−2I(i,j)+I(i−1,j)
根据上面的定义,与拉普拉斯算子的定义相结合,得到:∇2I=–4I(i,j)+I(i,j+1)+I(i,j−1)+I(i+1,j)+I(i−1,j)
对应的二阶微分卷积核为:m=⎡⎣⎢0101−41010⎤⎦⎥
也就是一个点的拉普拉斯的算子计算结果是上下左右的灰度的和减去本身灰度的四倍。同样,可以根据二阶微分的不同定义,所有符号相反,也就是上式所有灰度值全加上负号,就是-1,-1,-1,-1,4。但要注意,符号改变,锐化的时候与原图的加或减应当相对变化。上面是四邻接的拉普拉斯算子,将这个算子旋转45°后与原算子相加,就变成八邻域的算子了,也就是一个像素周围一圈8个像素的和与中间像素8倍的差,作为拉普拉斯计算结果。计算方法及优化
所以二阶微分检测边缘的方法就分两步:
1)用上面的Laplace核与图像进行卷积;
2)对卷积后的图像,取得那些卷积结果为0的点。虽然上述使用二阶微分检测边缘的方法简单,但它的缺点是对噪声十分敏感,同时也没有能够提供边缘的方向信息。为了实现对噪声的抑制,Marr等提出了LOG的方法。
为了减少噪声对边缘的影响,首先图像要进行低通滤波,LOG采用了高斯函数作为低通滤波器。高斯函数为:
G(x,y)=12πσ2e−x2+y22σ2
上面的公式中σ决定了对图像的平滑程度。高斯函数生成的滤波模板尺寸一般设定为6σ+1(加1是会了使滤波器的尺寸为奇数)。使用高斯函数对图像进行滤波并对图像滤波结果进行二阶微分运算的过程,可以转换为先对高斯函数进行二阶微分,再利用高斯函数的二阶微分结果对图像进行卷积运算:∇2[G(x,y)∗f(x,y)]=∇2[G(x,y)]∗f(x,y)∇2G(x,y)=−12πσ4[1−x2+y2σ2]⋅exp(−x2+y22σ2)模板核推导过程
拉普拉斯是用二阶差分计算边缘的(在一阶微分图中极大值或极小值处,认为是边缘。在二阶微分图中极大值和极小值之间的过 0 点,被认为是边缘。)
拉普拉斯算子推导:
一阶差分:f ‘(x) = f(x) - f(x - 1)
二阶差分:f ‘(x) = (f(x + 1) - f(x)) - (f(x) - f(x - 1))
化简后:f ‘(x) = f(x - 1) - 2 f(x)) + f(x + 1)
提取前面的系数:[1, -2, 1]二维的情况下,同理可得
f ‘(x, y) = -4 f(x, y) + f(x-1, y) + f(x+1, y) + f(x, y-1) + f(x, y+1)拉普拉斯算子还可以表示成模板的形式,以便更好编程需要
图1(a)表示离散拉普拉斯算子的模板,
图1(b)表示其扩展模板,
图1(c)则分别表示其他两种拉普拉斯的实现模板。从模板形式容易看出,如果在图像中一个较暗的区域中出现了一个亮点,那么用拉普拉斯运算就会使这个亮点变得更亮。因为图像中的边缘就是那些灰度发生跳变的区域,所以拉普拉斯锐化模板在边缘检测中很有用。一般增强技术对于陡峭的边缘和缓慢变化的边缘很难确定其边缘线的位置。但此算子却可用二次微分正峰和负峰之间的过零点来确定,对孤立点或端点更为敏感,因此特别适用于以突出图像中的孤立点、孤立线或线端点为目的的场合。同梯度算子一样,拉普拉斯算子也会增强图像中的噪声,有时用拉普拉斯算子进行边缘检测时,可将图像先进行平滑处理。
图像锐化
图像锐化处理的作用是使灰度反差增强,从而使模糊图像变得更加清晰。
图像模糊的实质就是图像受到平均运算或积分运算,因此可以对图像进行逆运算,如微分运算能够突出图像细节,使图像变得更为清晰。由于拉普拉斯是一种微分算子,它的应用可增强图像中灰度突变的区域,减弱灰度的缓慢变化区域。因此,锐化处理可选择拉普拉斯算子对原图像进行处理,产生描述灰度突变的图像,再将拉普拉斯图像与原始图像叠加而产生锐化图像。拉普拉斯锐化的基本方法可以由下式表示:
这种简单的锐化方法既可以产生拉普拉斯锐化处理的效果,同时又能保留背景信息,将原始图像叠加到拉普拉斯变换的处理结果中去,可以使图像中的各灰度值得到保留,使灰度突变处的对比度得到增强,最终结果是在保留图像背景的前提下,突现出图像中小的细节信息。但其缺点是对图像中的某些边缘产生双重响应。API
void Laplacian(InputArray src,OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, intborderType=BORDER_DEFAULT );
第一个参数,InputArray类型的image,输入图像,即源图像,填Mat类的对象即可,且需为单通道8位图像。
第二个参数,OutputArray类型的edges,输出的边缘图,需要和源图片有一样的尺寸和通道数。
第三个参数,int类型的ddept,目标图像的深度。
第四个参数,int类型的ksize,用于计算二阶导数的滤波器的孔径尺寸,大小必须为正奇数,且有默认值1。
第五个参数,double类型的scale,计算拉普拉斯值的时候可选的比例因子,有默认值1。
第六个参数,double类型的delta,表示在结果存入目标图(第二个参数dst)之前可选的delta值,有默认值0。
第七个参数, int类型的borderType,边界模式,默认值为BORDER_DEFAULT。这个参数可以在官方文档中borderInterpolate()处得到更详细的信息。步骤
1.高斯模糊-GaussianBlur
2.灰度转换-cvtColor
3.拉普拉斯计算二阶导数
4.取绝对值
5.输出样例
#include<opencv2\core\core.hpp> #include<opencv2\highgui\highgui.hpp> #include<opencv2\imgproc\imgproc.hpp> using namespace std; using namespace cv; int main() { Mat src, dst; src = imread("D:/程序文件/Opencv/Opencv/lena.jpg"); if (!src.data) { return -1; } /* * 1.高斯模糊 * 2.转换为灰度图像 * 3.laplace算子找边缘 * 4.对于边缘像素值求绝对值,以保证像素没有被舍去 * 5.附加操作,可以通过阈值处理来加强结果。 */ char inputTitle[] = "input image"; char outputTitle[] = "output image"; namedWindow(inputTitle, CV_WINDOW_NORMAL); imshow(inputTitle, src); Mat gray_src, edge_image; GaussianBlur(src, dst, Size(3, 3), 0, 0); cvtColor(dst, gray_src, CV_BGR2GRAY); Laplacian(gray_src, edge_image, CV_16S, 3); convertScaleAbs(edge_image, edge_image); threshold(edge_image, edge_image, 0, 255, THRESH_OTSU | THRESH_BINARY); namedWindow(outputTitle, CV_WINDOW_NORMAL); imshow(outputTitle, edge_image); waitKey(0); return 0; }
-
数字视频处理(三)——空域模板操作
2020-04-10 23:12:54对灰度图进行平滑滤波(采用邻域平均模板 ...文章目录(一)所用图片格式说明(二)实验代码main.cppfiltering.hfiltering.cpp(三)实验结果1 邻域平均模板2 高斯平均模板3 Laplace锐化滤波4 Sobel滤波 ... -
图像分割-基本边缘检测roberts,prewitt,sobel,canny,laplace
2020-02-11 14:06:05执行边缘检测的三个基本步骤: 1、为降噪对图像进行平滑处理。(导数对噪声具有敏感性。图像的正负分量检测困难) 2、边缘点的检测。...常用绝对值来近似梯度幅值,保持灰度级的相对变化,代价是导致滤波... -
用python画函数的梯度图_【图像处理】——Python图像分割边缘检测算法之二阶梯度算子(laplace、log、dog...
2020-12-08 14:18:56目录一、二阶算子简介二、laplace(拉普拉斯算子)1、什么是拉普拉斯算子(1)连续二维函数的二阶导(2)离散二维函数的二阶导数2、常用算子模块及代码3、结果三、log算子1、什么是log算子2、计算流程3、log算子模板4、... -
OpenCV2邻域和模板操作
2015-08-05 14:06:16 ...至于模板操作是实现空间滤波的基础,通常是使用一个模板(一个的矩形)滑过整幅图像产生新的像素。下面介绍通过使用OpenCV2实现Laplace算子锐化图像,来介绍OpenCV2中对邻域和模板的操作。 -
基于模板匹配的前视红外目标识别方法
2019-02-15 12:33:38由高程数据和正射影像等卫星数据生成目标区参考图和基准图 ,在基准图中根据归一化 Laplace 响应确 定目标区特征尺度作为目标检测的先验知识 ,对实时图及灰度反转实时图进行匹配滤波 ,检测出候选区域 ,再 对候选区域... -
C#ImageEnhance图像增强
2016-10-13 16:30:17空域增强、频域增强、图像锐化 支持阈值滤波、均值滤波、中值滤波 Kirsch算子、Laplace算子、Prewitt算子、Roberts算子、Sobel算子、 Butterworth高通滤波、Butterworth低通滤波、模板滤波等算法 -
matlab图像增强程序(平滑和锐化)
2016-10-26 21:13:06利用matlab编写的图像平滑锐化程序,包括:'均值滤波','中值滤波','罗伯特梯度','Prewitt算法','Sobel梯度','Laplace算子','Laplace算子扩展模板','方向算子' -
VS2010多文档图像处理所有基础程序
2015-03-18 09:03:41利用VS2010编写的多文档的图像处理基础程序,包括灰度变换 直方图均衡 局部平均平滑 中值滤波 理想低通滤波 Butterworth低通滤波 图象锐化 理想高通滤波 Butterworth...Canny算子 边界跟踪 区域生长 图象识别 模板匹配 -
特征提取算法(4)——LoG特征提取算法
2019-10-01 10:08:33目录 1、介绍 2、LoG原理 3、数学原理 4、模板性质 ...LoG(DoG是一阶边缘提取)是二阶...于是,首先对图像进行高斯卷积滤波进行降噪处理,再采用Laplace算子进行边缘检测,就可以提高算子对噪声抗干扰能力, 这... -
matlab线性锐化滤波器(图像工程第三章)
2020-10-13 14:41:15Laplace模板 x=imread('cat1.gif'); f=[-1 -1 -1;-1 8 -1;-1 -1 -1]; x1=imfilter(x,f); x2=x+x1; subplot(131);imshow(x);title('原始图'); subplot(132);imshow(x1);title('拉普拉斯锐化掩膜'); subplot(133);i -
数字图像处理(9):拉普拉斯算子增强
2020-06-26 23:04:04存在噪声情况下,使用Laplacian算子检测边缘之前需要先进行低通滤波。所以,通常的分割算法都是把Laplacian算子和平滑算子结合起来生成一个新的模板。 拉普拉斯算子,二阶微分线性算子。与一阶微分相比,二
-
第3章-11 字符串排序 (20 分)
-
【Zookeeper】集群模式:架构设计猜想
-
关于pytorch语义分割二分类问题的两种做法
-
Arduino舵机风扇.zip
-
_this2.$xxxis not a function
-
鸿蒙系统Harmonyos源码架构分析-第1期第2课
-
后端out.write 输出客户端页面出现乱码
-
MHA 高可用 MySQL 架构与 Altas 读写分离
-
《ChinaTeXMathFAQ_V1.1》.pdf
-
股票投资了解
-
Codeforces Global Round 13 ABC题解
-
com.termux_102.apk
-
Unity RUST 逆向安全开发
-
在 Linux 上构建企业级 DNS 域名解析服务
-
第一章 工作原理、语言标准和规范(1.4~1.6)
-
python课件.rar
-
客户端向日葵SunloginEnterprise_3.0.0.27372.exe
-
Http通信协议
-
MySQL Router 实现高可用、负载均衡、读写分离
-
MaxScale 实现 MySQL 读写分离与负载均衡