http://www.ihalcon.com/read-12670.html
#include #include //头文件 using namespace cv; //包含cv命名空间 using namespace std; int main() { //读入图片 Mat img_1 = imread("F:\\图标\\222222.jpg"); //Mat img_2 = imread("F:\\图标\\2.jpg"); if (img_1.empty()) { cout << "打开图片失败,请检查" << endl; return -1; } imshow("src_img", img_1); //图像增强算法 直方图均衡化-------------------------------------------------------------------1 Mat imageRGB[3]; split(img_1, imageRGB); for (int i = 0; i < 3; i++) { equalizeHist(imageRGB, imageRGB); } merge(imageRGB, 3, img_1); imshow("直方图均衡化图像增强效果", img_1); //拉普拉斯算子可以增强局部的图像对比度---------------------------------------------------------2 Mat imageEnhance; Mat kernel = (Mat_(3, 3) << 0, -1, 0, 0, 5, 0, 0, -1, 0); filter2D(img_1, imageEnhance, CV_8UC3, kernel);//利用内核实现对图像的卷积运算 imshow("拉普拉斯算子图像增强效果", imageEnhance); //基于对数Log变换的图像增强,达到了扩展和增强低灰度部分,压缩高灰度部分的值的功能-------------3 Mat imageLog(img_1.size(), CV_32FC3); for (int i = 0; i < img_1.rows; i++) { for (int j = 0; j < img_1.cols; j++) { imageLog.at(i, j)[0] = log(1 + img_1.at(i, j)[0]); imageLog.at(i, j)[1] = log(1 + img_1.at(i, j)[1]); imageLog.at(i, j)[2] = log(1 + img_1.at(i, j)[2]); } } //归一化到0~255 normalize(imageLog, imageLog, 0, 255, CV_MINMAX); //转换成8bit图像显示 convertScaleAbs(imageLog, imageLog); imshow("after", imageLog); //基于伽马变换的图像增强,---------------------------------------------------------------------4 //伽马变换对于图像对比度偏低,并且整体亮度值偏高(对于于相机过曝)情况下的图像增强效果明显。 Mat imageGamma(img_1.size(), CV_32FC3); for (int i = 0; i < img_1.rows; i++) { for (int j = 0; j < img_1.cols; j++) { imageGamma.at(i, j)[0] = (img_1.at(i, j)[0])*(img_1.at(i, j)[0])*(img_1.at(i, j)[0]); imageGamma.at(i, j)[1] = (img_1.at(i, j)[1])*(img_1.at(i, j)[1])*(img_1.at(i, j)[1]); imageGamma.at(i, j)[2] = (img_1.at(i, j)[2])*(img_1.at(i, j)[2])*(img_1.at(i, j)[2]); } } //归一化到0~255 normalize(imageGamma, imageGamma, 0, 255, CV_MINMAX); //转换成8bit图像显示 convertScaleAbs(imageGamma, imageGamma); imshow("伽马变换图像增强效果", imageGamma); //imshow("【图2】", img_2); cout << "图像1的长宽:" << img_1.rows <<"*"<< img_1.cols << endl; //cout << "图像2的长宽:" << img_2.rows <<"*"<< img_2.cols << endl; //转为灰度图并去燥 Mat gray_img; cvtColor(imageGamma, gray_img, CV_RGB2GRAY); blur(gray_img,gray_img,Size(5,5)); //canny边缘检测 Mat canny_img; Canny(gray_img,canny_img,10.0,60.0); vector compression_params; //向compression_params里面添加值 compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); compression_params.push_back(100); imwrite("F:\\图标\\222222增强.jpg", imageGamma, compression_params); imwrite("F:\\图标\\222222素描.jpg", canny_img, compression_params); cout << "素描图像保存成功!"<<endl; cout << "增强图像保存成功!"<<endl; imshow("gray_image", gray_img); imshow("canny_img", canny_img); waitkey(0); return 0; }