/* 图像平滑,锐化 图像金字塔 形态学运算 查找表 */ #include<opencv2\opencv.hpp> #include<iostream> using namespace cv; using namespace std; int main() { Mat img_org = imread("D://图片//4.jpg"); if (img_org.empty()) { cout << "图片加载失败!" << endl; return -1; } Mat img1, img2, img3, img5; //平滑操作 boxFilter(img_org, img1, -1, Size(9, 9)); //盒式滤波 GaussianBlur(img_org, img2, Size(9, 9), 0, 0); //高斯滤波 medianBlur(img_org, img3, 9); //中值滤波 //bilateralFilter(img_org, img5, 5, 8, 8); //与高斯滤波相似 Mat im1, im2, im3; //锐化操作 Sobel(img_org, im1, -1, 1, 1); Laplacian(img_org, im3, CV_8UC3); Mat ig1, ig2, ig3; //金字塔 pyrDown(img_org, ig1); //pyrUp是pyrDown的反运算,但是不能获取原始图像 pyrDown(ig1, ig2); pyrUp(ig2, ig3); Mat tem, tem1, tem2; inRange(img_org, Scalar(0, 0, 0), Scalar(255, 150, 255), tem); //保留颜色范围 Mat element = getStructuringElement(MORPH_ELLIPSE, Size(16, 18)); dilate(tem, tem1, element); //膨胀 erode(tem1, tem2, element); //腐蚀 uchar *M = new uchar[256]; //查找表 for (int i = 0; i < 256; i++) { M[i] = i*0.5; } Mat lut(1, 256, CV_8UC3, M); Mat result; LUT(img_org, lut, result); imshow("img1", img1); imshow("img2", img2); imshow("img3", img3); //imshow("img5", img5); imshow("im1", im1); imshow("im3", im3); imshow("ig1", ig1); imshow("ig2", ig2); imshow("ig3", ig3); imshow("tem1", tem1); imshow("tem2", tem2); imshow("result", result); delete[]M; waitKey(0); destroyAllWindows; return 0; }
运行结果: