OpenCV实现图象翻转、滤波、锐化

注:以下代码,使用opencv库函数实现了对图片的翻转、灰度图转换、各种滤波、各种锐化。

库函数相关参数及说明参阅OpenCV中文站=》opencv教程(cn)

 #include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp> using namespace std;
using namespace cv; //全局变量
const int slider_max = ;
int slider;
Mat img;
Mat resImg; Mat src, res[]; //翻转图片
void turn(string src)
{
Mat srcMat = imread(src);
if (!srcMat.data)
{
cout << "源图象获取失败!" << endl;
return;
}
//原始图像
imshow("原图片", srcMat);
//水平翻转
flip(srcMat, res[], );
imshow("水平翻转", res[]);
imwrite("D:\\trashBox\\testIMG\\flip1.jpg", res[]);
//垂直翻转
flip(srcMat, res[], );
imshow("垂直翻转", res[]);
imwrite("D:\\trashBox\\testIMG\\flip0.jpg", res[]);
} //图像平滑处理(滤波)
void blur(string src)
{
Mat srcMat = imread(src);
if (!srcMat.data)
{
cout << "源图象获取失败!" << endl;
return;
}
//源图片
imshow("源图片", srcMat);
//中值滤波
medianBlur(srcMat, res[], );
imshow("中值滤波", res[]);
imwrite("D:\\trashBox\\testIMG\\medianBlur.jpg", res[]);
//均值滤波
blur(srcMat, res[], Size(, ), Point(-, -));
imshow("均值滤波", res[]);
imwrite("D:\\trashBox\\testIMG\\averBlur.jpg", res[]);
//高斯滤波
GaussianBlur(srcMat, res[], Size(, ), , );
imshow("高斯滤波", res[]);
imwrite("D:\\trashBox\\testIMG\\gsBlur.jpeg", res[]);
} //图象锐化:高通滤波
void sharpen(string src)
{
Mat srcMat = imread(src);
if (!srcMat.data)
{
cout << "源图象获取失败!" << endl;
return;
}
//原图
imshow("源图片", srcMat);
//灰度图
cvtColor(srcMat, res[], CV_BGR2GRAY);
imshow("灰度图象", res[]);
imwrite("D:\\trashBox\\testIMG\\grey.jpg", res[]);
//sobel算子,基于梯度
Sobel(res[], res[], res[].depth(), , , , , , BORDER_DEFAULT);
imshow("X水平Sobel", res[]);
imwrite("D:\\trashBox\\testIMG\\sobelX.jpg", res[]);
Sobel(res[], res[], res[].depth(), , , , , , BORDER_DEFAULT);
imshow("Y方向Sobel", res[]);
imwrite("D:\\trashBox\\testIMG\\sobelY.jpg", res[]);
//sobel叠加
convertScaleAbs(res[], res[]);
convertScaleAbs(res[], res[]);
addWeighted(res[], 0.5, res[], 0.5, , res[]);
imshow("叠加Sobel", res[]);
imwrite("D:\\trashBox\\testIMG\\sobelXY.jpg", res[]); //laplace算子, 基于二阶微分
Laplacian(res[], res[], res[].depth(), , , , BORDER_DEFAULT);
convertScaleAbs(res[], res[]);
imshow("Laplace算子边缘检测", res[]);
imwrite("D:\\trashBox\\testIMG\\laplace.jpg", res[]);
} //直方图
void hist(string src)
{
Mat srcMat = imread(src);
if (!srcMat.data)
{
cout << "源图象获取失败!" << endl;
return;
}
//直方图均衡化
cvtColor(srcMat, res[], CV_BGR2GRAY);//转灰度图像
imshow("灰度图", res[]);
equalizeHist(res[], res[]);
imshow("直方图均衡化", res[]);
imwrite("D:\\trashBox\\equalHist.jpg", res[]);
} //颜色减半
void colorReduce(Mat& input, Mat& output, int div);
void on_trackbar(int pos, void *); //迭代器实现颜色减半
void colorReduce(Mat& input, Mat& output, int div)
{
output = input.clone(); Mat_<Vec3b>::iterator it = input.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = input.end<Vec3b>(); Mat_<Vec3b> cimage = output;
Mat_<Vec3b>::iterator itout = cimage.begin();
Mat_<Vec3b>::iterator itoutend = cimage.end(); for (; it != itend; it++, itout++)
{
for(int i=; i<; i++)
(*itout)[i] = (*it)[i] / div * div + div / ;//200-249->0-29压缩灰度级,用中间值代替
}
//output = cimage;
//imshow("源图片", input);
//imshow("颜色减半图片", output);
} //trackBar 回调函数
void on_trackbar(int pos, void *)
{
if (pos <= )
{
resImg = img;
}
else
{
colorReduce(img, resImg, pos);
}
imshow("显示结果", resImg);
} int main()
{
     //**************此部分代码调用翻转、滤波、锐化函数*******************//
//string lena = "D:\\trashBox\\testIMG\\lena.bmp";
//string tiger = "D:\\trashBox\\testIMG\\tiger.jpg";
//string xian = "D:\\trashBox\\testIMG\\xian.jpg";
//turn(lena);
//blur(tiger);
//blur(lena);
//sharpen(xian);
//sharpen(xian);
//hist(lena);
     //**************************************************************// //********************此部分代码实现图象的颜色减半******************//
Mat src, res;
img = imread("D:\\trashBox\\photo.jpg");
if (!img.data)
{
cout << "源图象获取失败!" << endl;
return ;
}
namedWindow("原图像");
namedWindow("显示结果"); slider = ;
createTrackbar("ColorReduce", "显示结果", &slider, slider_max, on_trackbar); imshow("原图像", img);
imshow("显示结果", img); waitKey();
}
05-14 06:40