1.自定义线性滤波
·拉普拉斯算子和掩膜的算子很像,一个中间是4,一个中间是5.但是结果会差很多。
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\types_c.h>
#include <opencv2\objdetect\objdetect_c.h>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
using namespace cv;
int main()
{
Mat src,dst,dstx,dsty;
src=imread("cat.jpg");
if(src.empty())
{
cout<<"!!???"<<endl;
return -1;
}
namedWindow("cat!",1);
imshow("cat!",src);
/*1.Robert
//Robert算子(梯度算子),X方向
namedWindow("R x",1);
namedWindow("R y",1);
Mat kernelx=(Mat_<int>(2,2)<<1,0,0,-1);
filter2D(src,dstx,-1,kernelx,Point(-1,-1),0.0);
imshow("R x",dstx);
//Robert算子,Y方向
Mat kernely=(Mat_<int>(2,2)<<0,1,-1,0);
filter2D(src,dsty,-1,kernely,Point(-1,-1),0.0);
imshow("R y",dsty);
*/
/*2.Sobel
//Sobel算子x和y方向
namedWindow("S x",1);
namedWindow("S y",1);
Mat sx,sy;
Mat kernelx=(Mat_<int>(3,3)<<-1,0,1,-2,0,2,-1,0,1);
filter2D(src,sx,-1,kernelx,Point(-1,-1),0.0);
imshow("S x",sx);
Mat kernely=(Mat_<int>(3,3)<<-1,-2,-1,0,0,0,1,2,1);
filter2D(src,sy,-1,kernely,Point(-1,-1),0.0);
imshow("S y",sy);
*/
/*
//3.拉普拉斯算子
namedWindow("Lap",1);
Mat lap;
Mat kernel=(Mat_<int>(3,3)<<0,-1,0,-1,4,-1,0,-1,0);
filter2D(src,lap,-1,kernel,Point(-1,-1),0.0);
imshow("Lap",lap);
*/
//自定义一个随着时间变化循环模糊~
int c=0;
int index=0;
int ksize=3;
while(true)
{
c=waitKey(500);
if((char)c==27)//ESC键
break;
ksize=4+(index%5)*2+1;
Mat kernel=Mat::ones(Size(ksize,ksize),CV_32F)/(float)(ksize*ksize);
filter2D(src,dst,-1,kernel,Point(-1,-1),0.0);
index++;
imshow("out",dst);
}
waitKey(0);
return 0;
}
2.处理边缘
扫描二维码关注公众号,回复:
1968334 查看本文章
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\types_c.h>
#include <opencv2\objdetect\objdetect_c.h>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
using namespace cv;
int main()
{
Mat src,dst;
src=imread("cat.jpg");
if(src.empty())
{
cout<<"!!???"<<endl;
return -1;
}
namedWindow("cat!",1);
imshow("cat!",src);
namedWindow("out",1);
int top=(int)(0.05*src.rows);
int bottom=(int)(0.05*src.rows);
int left=(int)(0.05*src.cols);
int right=(int)(0.05*src.cols);
RNG rng(12345);
int bordertype=BORDER_DEFAULT;
int c=0;
while(true)
{
c=waitKey(500);
if((char)c==27)
break;
if((char)c=='r')
{
bordertype=BORDER_REPLICATE;
}
else if((char)c=='w')
{
bordertype=BORDER_WRAP;
}
else if((char)c=='c')
{
bordertype=BORDER_CONSTANT;
}
else
{
bordertype=BORDER_DEFAULT;
}
Scalar color=Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255));
copyMakeBorder(src,dst,top,bottom,left,right,bordertype,color);
imshow("out",dst);
}
waitKey(0);
return 0;
}