OpenCV實現圖象翻轉、濾波、銳化
阿新 • • 發佈:2019-05-10
flip 叠代器 names 直方圖 read space eat tiger input
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 }
OpenCV實現圖象翻轉、濾波、銳化