视觉图像:卷积运算及代码实现

 时间:2024-10-21 19:36:58

绪:图像卷积操作即:原图像f(x),模板图像g(x),首先,将模板g(x)在原图像f(x)中移动;然后,每到一个位置,将f(x)与g(x)的定义域相交的元素进行乘积且求和,得出新的图像点;遍历所有像素点,得到卷积后的图像;模板图像又称为卷积核;

视觉图像:卷积运算及代码实现

方法/步骤

1、OpenCV卷积函数filter2D():形式:CV_EXPORTS_Wvoidfilter2D(InputArraysrc,OutputArraydst,intddepth,InputArraykernel,Pointanchor=Point(-1,-1),doubledelta=0,intborderType=BORDER_DEFAULT);功能:利用内核实现对图像的卷积运算;参数:InputArraysrc:输入图像;OutputArraydst:输出图像,和输入图像具有相同的尺寸和通道数量;intddepth:目标图像深度;原图像和目标图像支持的图像深度如下:src.depth()=CV_8U,ddepth=-1/CV_16S/CV_32F/CV_64Fsrc.depth()=CV_16U/CV_16S,ddepth=-1/CV_32F/CV_64Fsrc.depth()=CV_32F,ddepth=-1/CV_32F/CV_64Fsrc.depth()=CV_64F,ddepth=-1/CV_64F当ddepth输入值为-1时,目标图像和原图像深度保持一致;InputArraykernel:卷积核,一个单通道浮点型矩阵。如果想在图像不同的通道使用不同的kernel,可以先使用split()函数将图像通道事先分开。Pointanchor:内核的基准点(anchor),其默认值为(-1,-1)说明位于kernel的中心位置。基准点即kernel中与进行处理的像素点重合的点。doubledelta:在储存目标图像前可选的添加到像素的值,默认值为0intborderType:像素向外逼近的方法,默认值是BORDER_DEFAULT,即对全部边界进行计算。

2、filter2D()示例:#include<opencv2\opencv.hpp>usingnamespacestd;usingnamespacecv;intmain(intargc,char**argv){Matraw_img=imread("raw.jpg",1);if(!raw_img.data){return-1;}imshow("before",raw_img);Matmask_img=(Mat_<char>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);Matdst_img(raw_img.rows,raw_img.cols,CV_32FC3);Pointanchor=Point(-1,-1);filter2D(raw_img,dst_img,-1,mask_img,anchor,0,BORDER_DEFAULT);imshow("before",raw_img);imshow("after",dst_img);waitKey(0);return0;}

视觉图像:卷积运算及代码实现

视觉图像:卷积运算及代码实现

3、常见卷积模板:模板:矩阵方块,其数学含义是一种卷积运算;卷积运算:是加权求和的过程;所有乘积之和作为区域中心像素的新值;卷积示例:3*3的像素区域R与卷积核G的卷积运算(中心像素)R5=R1G1+R2G2+R3G3+R4G4+R5G5+R6G6+R7G7+R8G8+R9G9;常见模板如下:滤波是图像处理的基本操作,滤波去除图像中的噪声,提取感兴趣的特征,允许图像重采样。图像中的频域和空域:空间域指用图像的灰度值来描述一幅图像;而频域指用图像灰度值的变化来描述一幅图像。而低通滤波器和高通滤波器的概念就是在频域中产生的。低通滤波器:指去除图像中的高频成分,使边缘平滑;而高通滤波器:指去除图像中的低频成分,使边缘提取与增强;

视觉图像:卷积运算及代码实现

视觉图像:卷积运算及代码实现

视觉图像:卷积运算及代码实现

4、卷积边界问题:当处理图像边界像素时,卷积核与图像使用区域不能匹配,卷积核的中心与边界像素点对应,卷积将出现问题。处理方法:A.忽略边界像素,即处理后的图像将丢掉这些像素;B.保留原边界像素,即copy边界像素到处理后的图像;

视觉图像:卷积运算及代码实现

5、自定义卷积运算:#include<opencv2\opencv.hpp>掼鸿乡羰usingnamespacestd;usingn锾攒揉敫amespacecv;//kernel(-1,-2,1;4,-2,-1;4,-2,2);voidSharpen(Mat&myImage,Mat&Result){intnChannels=myImage.channels();Result.create(myImage.size(),myImage.type());for(intj=1;j<myImage.rows-1;++j)//hang{unsignedchar*previous=myImageNaNr<unsignedchar>(j-1);//上一行数据的指针unsignedchar*current=myImageNaNr<unsignedchar>(j);//当前行数据的指针unsignedchar*next=myImageNaNr<unsignedchar>(j+1);//下一行数据的指针unsignedchar*output=ResultNaNr<unsignedchar>(j);//输出图像当前列数据的指针for(inti=1;i<myImage.cols-1;++i)//lie{output[i]+=saturate_cast<uchar>(-1*previous[i-1]-2*previous[i]+1*previous[i+1]+4*current[i-1]-2*current[i]-1*current[i+1]+4*next[i-1]-2*next[i]+2*next[i+1]);}}//将边界设为0Result.row(0).setTo(Scalar(0));Result.row(Result.rows-1).setTo(Scalar(0));Result.col(0).setTo(Scalar(0));Result.col(Result.cols-1).setTo(Scalar(0));}intmain(){Matimg=imread("raw.jpg",0);Matsrc;Sharpen(img,src);imshow("img",img);imshow("src",src);waitKey(0);return0;}

6、示例:#include<opencv2\ope艘绒庳焰ncv.hpp>usingnamespacestd;using惺绅寨瞀namespacecv;MatGaussian_kernal(intkernel_size,floatsigma);Matz_Sharpen(Matraw_img,Matkernel_img);intmain(intargc,char**argv){Matraw_img=imread("raw.jpg",0);imshow("before",raw_img);intkernel_size=3;floatsigma=1.4;Matkernel_img=Gaussian_kernal(kernel_size,sigma);cout<<kernel_img<<endl;//floatmyArray[3][3]=//{//-1,-2,1,//4,-2,-1,//4,-2,2//};//Matkernel_img=Mat(3,3,CV_32FC1,myArray);//创建矩阵Matdst_img=z_Sharpen(raw_img,kernel_img);imshow("after",dst_img);waitKey(0);return0;}Matz_Sharpen(Matraw_img,Matkernel_img){intm_width=raw_img.cols;//kuanintm_height=raw_img.rows;//gaointkernel_size=kernel_img.cols;intk_size=(kernel_size-1)/2;Matdst_img(m_height,m_width,CV_8UC1);for(intj=k_size;j<m_width-k_size;j++)//lie{for(inti=k_size;i<m_height-k_size;i++)//hang{unsignedchar*current=dst_imgNaNr<unsignedchar>(i);//dangqianhangintm_J=-k_size;//liefor(intm=0;m<kernel_size;m++){float*k_data=kernel_imgNaNr<float>(m);intm_I=-k_size;//hangfor(intn=0;n<kernel_size;n++){unsignedchar*p_data=raw_imgNaNr<unsignedchar>(i+m_I);current[j]+=saturate_cast<unsignedchar>(p_data[j+m_J]*k_data[n]);m_I++;}m_J++;}}}//将边界设为0for(inti=0;i<k_size;i++){dst_img.row(i).setTo(Scalar(0));dst_img.col(i).setTo(Scalar(0));}for(inti=m_height-k_size;i<m_height;i++){dst_img.row(i).setTo(Scalar(0));}for(inti=m_width-k_size;i<m_width;i++){dst_img.col(i).setTo(Scalar(0));}returndst_img;}MatGaussian_kernal(intkernel_size,floatsigma){constdoublePI=3.14159265358979323846;intm=kernel_size/2;Matmask_temp(kernel_size,kernel_size,CV_32FC1);floats_2=2*sigma*sigma;for(inti=0;i<kernel_size;i++){for(intj=0;j<kernel_size;j++){intx=i-m;inty=j-m;mask_tempNaNr<float>(i)[j]=exp(-(x*x+y*y)/2*s_2)/(PI*s_2);}}returnmask_temp;}

视觉图像:卷积运算及代码实现

pycharm怎么安装第三方库 anaconda运行python程序教程 python如何安装Numpy模块? 如何在pycharm中安装功能库 python如何读取图片并显示?
热门搜索
好看的恋爱动漫 在线h动漫 动漫高清壁纸 透视装图片 动漫爱情