OpenCV实现图象翻转、滤波、锐化
注:以下代码,使用opencv库函数实现了对图片的翻转、灰度图转换、各种滤波、各种锐化。
库函数相关参数及说明参阅:OpenCV中文站=》opencv教程(cn)
1 #include <iostream> 2 #include <stdio.h> 3 #include <Windows.h> 4 #include <opencv2/highgui/highgui.hpp> 5 #include <opencv2/imgproc/imgproc.hpp> 6 #include <opencv2/imgproc/types_c.h> 7 #include <opencv2/core/core.hpp> 8 #include <opencv2/opencv.hpp> 9 10 using namespace std; 11 using namespace cv; 12 13 //全局变量 14 const int slider_max = 64; 15 int slider; 16 Mat img; 17 Mat resImg; 18 21 Mat src, res[20]; 22 23 //翻转图片 24 void turn(string src) 25 { 26 Mat srcMat = imread(src); 27 if (!srcMat.data) 28 { 29 cout << "源图象获取失败!" << endl; 30 return; 31 } 32 //原始图像 33 imshow("原图片", srcMat); 34 //水平翻转 35 flip(srcMat, res[0], 1); 36 imshow("水平翻转", res[0]); 37 imwrite("D:\trashBox\testIMG\flip1.jpg", res[0]); 38 //垂直翻转 39 flip(srcMat, res[1], 0); 40 imshow("垂直翻转", res[1]); 41 imwrite("D:\trashBox\testIMG\flip0.jpg", res[1]); 42 } 43 44 //图像平滑处理(滤波) 45 void blur(string src) 46 { 47 Mat srcMat = imread(src); 48 if (!srcMat.data) 49 { 50 cout << "源图象获取失败!" << endl; 51 return; 52 } 53 //源图片 54 imshow("源图片", srcMat); 55 //中值滤波 56 medianBlur(srcMat, res[4], 5); 57 imshow("中值滤波", res[4]); 58 imwrite("D:\trashBox\testIMG\medianBlur.jpg", res[4]); 59 //均值滤波 60 blur(srcMat, res[5], Size(2, 3), Point(-1, -1)); 61 imshow("均值滤波", res[5]); 62 imwrite("D:\trashBox\testIMG\averBlur.jpg", res[5]); 63 //高斯滤波 64 GaussianBlur(srcMat, res[6], Size(3, 3), 0, 0); 65 imshow("高斯滤波", res[6]); 66 imwrite("D:\trashBox\testIMG\gsBlur.jpeg", res[6]); 67 } 68 69 //图象锐化:高通滤波 70 void sharpen(string src) 71 { 72 Mat srcMat = imread(src); 73 if (!srcMat.data) 74 { 75 cout << "源图象获取失败!" << endl; 76 return; 77 } 78 //原图 79 imshow("源图片", srcMat); 80 //灰度图 81 cvtColor(srcMat, res[3], CV_BGR2GRAY); 82 imshow("灰度图象", res[3]); 83 imwrite("D:\trashBox\testIMG\grey.jpg", res[3]); 84 //sobel算子,基于梯度 85 Sobel(res[3], res[7], res[3].depth(), 1, 0, 3, 1, 0, BORDER_DEFAULT); 86 imshow("X水平Sobel", res[7]); 87 imwrite("D:\trashBox\testIMG\sobelX.jpg", res[7]); 88 Sobel(res[3], res[8], res[3].depth(), 0, 1, 3, 1, 0, BORDER_DEFAULT); 89 imshow("Y方向Sobel", res[8]); 90 imwrite("D:\trashBox\testIMG\sobelY.jpg", res[8]); 91 //sobel叠加 92 convertScaleAbs(res[7], res[7]); 93 convertScaleAbs(res[8], res[8]); 94 addWeighted(res[7], 0.5, res[8], 0.5, 0, res[9]); 95 imshow("叠加Sobel", res[9]); 96 imwrite("D:\trashBox\testIMG\sobelXY.jpg", res[9]); 97 98 //laplace算子, 基于二阶微分 99 Laplacian(res[3], res[10], res[3].depth(), 3, 1, 0, BORDER_DEFAULT); 100 convertScaleAbs(res[10], res[10]); 101 imshow("Laplace算子边缘检测", res[10]); 102 imwrite("D:\trashBox\testIMG\laplace.jpg", res[10]); 103 } 104 105 //直方图 106 void hist(string src) 107 { 108 Mat srcMat = imread(src); 109 if (!srcMat.data) 110 { 111 cout << "源图象获取失败!" << endl; 112 return; 113 } 114 //直方图均衡化 115 cvtColor(srcMat, res[3], CV_BGR2GRAY);//转灰度图像 116 imshow("灰度图", res[3]); 117 equalizeHist(res[3], res[11]); 118 imshow("直方图均衡化", res[11]); 119 imwrite("D:\trashBox\equalHist.jpg", res[11]); 120 } 121 122 //颜色减半 123 void colorReduce(Mat& input, Mat& output, int div); 124 void on_trackbar(int pos, void *); 125 126 //迭代器实现颜色减半 127 void colorReduce(Mat& input, Mat& output, int div) 128 { 129 output = input.clone(); 130 131 Mat_<Vec3b>::iterator it = input.begin<Vec3b>(); 132 Mat_<Vec3b>::iterator itend = input.end<Vec3b>(); 133 134 Mat_<Vec3b> cimage = output; 135 Mat_<Vec3b>::iterator itout = cimage.begin(); 136 Mat_<Vec3b>::iterator itoutend = cimage.end(); 137 138 for (; it != itend; it++, itout++) 139 { 140 for(int i=0; i<3; i++) 141 (*itout)[i] = (*it)[i] / div * div + div / 2;//200-249->0-29压缩灰度级,用中间值代替 142 } 143 //output = cimage; 144 //imshow("源图片", input); 145 //imshow("颜色减半图片", output); 146 } 147 148 //trackBar 回调函数 149 void on_trackbar(int pos, void *) 150 { 151 if (pos <= 0) 152 { 153 resImg = img; 154 } 155 else 156 { 157 colorReduce(img, resImg, pos); 158 } 159 imshow("显示结果", resImg); 160 } 161 162 int main() 163 {
//**************此部分代码调用翻转、滤波、锐化函数*******************// 164 //string lena = "D:\trashBox\testIMG\lena.bmp"; 165 //string tiger = "D:\trashBox\testIMG\tiger.jpg"; 166 //string xian = "D:\trashBox\testIMG\xian.jpg"; 167 //turn(lena); 168 //blur(tiger); 169 //blur(lena); 170 //sharpen(xian); 171 //sharpen(xian); 172 //hist(lena);
//**************************************************************//
//********************此部分代码实现图象的颜色减半******************// 173 Mat src, res; 174 img = imread("D:\trashBox\photo.jpg"); 175 if (!img.data) 176 { 177 cout << "源图象获取失败!" << endl; 178 return 0; 179 } 180 namedWindow("原图像"); 181 namedWindow("显示结果"); 182 183 slider = 0; 184 createTrackbar("ColorReduce", "显示结果", &slider, slider_max, on_trackbar); 185 186 imshow("原图像", img); 187 imshow("显示结果", img); 188 189 waitKey(0); 190 }