图像锐化:
#include <iostream> #include "opencv2/opencv.hpp" using namespace std; /*图像锐化 *方法1 *对图片的每个像素进行如下处理: *current像素=5*current-up-down-left-right; */ void sharpen(const cv::Mat &Image,cv::Mat &result) { int rows =Image.rows; int cols =Image.cols*Image.channels(); result.create(Image.size(),Image.type()); cout<<"channels:"<<Image.channels(); if(Image.channels() ==1) { for(int i=1;i<rows-1;i++) { const uchar *current =Image.ptr<uchar>(i); const uchar *up =Image.ptr<uchar>(i-1); const uchar *down =Image.ptr<uchar>(i+1); uchar *output =result.ptr<uchar>(i); for(int j=1;j<cols-1;j++) { output[j] =cv::saturate_cast<uchar>(5*current[j]-up[j]-down[j]-current[j-1]-current[j+1]); } } } result.row(0).setTo(cv::Scalar(0)); result.row(result.rows-1).setTo(cv::Scalar(0)); result.col(0).setTo(cv::Scalar(0)); result.col(result.cols-1).setTo(cv::Scalar(0)); } /*图像锐化 *方法2 *调用filter2D函数 */ void sharpen2D(const cv::Mat &image,cv::Mat &result) { cv::Mat kernel(3,3,CV_32F,cv::Scalar(0)); kernel.at<float>(0,1) =-1; kernel.at<float>(1,0) =-1; kernel.at<float>(1,1) =5.0; kernel.at<float>(1,2) =-1; kernel.at<float>(2,1) =-1; cv::filter2D(image,result,image.depth(),kernel); } int main() { cv::Mat matImage = cv::imread("E:/QtMaterial/images/boldt.jpg",0); cv::Mat matOutImage; cv::imshow("Orignal",matImage); // sharpen(matImage,matOutImage); sharpen2D(matImage,matOutImage); cv::imshow("Process",matOutImage); cv::waitKey(); return 0; }