天天看點

周遊圖像和鄰域操作,圖像銳化

#include<opencv2\core\core.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<iostream>

using namespace std;
using namespace cv;

//銳化函數
void sharpen(const cv::Mat &image,cv::Mat &result)
{
	result.create(image.size(),image.type());
	for(int j=1;j<image.rows-1;j++)
	{
		const uchar* previous=image.ptr<const uchar>(j-1);
		const uchar* current=image.ptr<const uchar>(j);
		const uchar* next=image.ptr<const uchar>(j+1);
		uchar *output=result.ptr<uchar>(j);
		for(int i=1;i<image.cols-1;i++)
		{
			*output++=cv::saturate_cast<uchar>(5*current[i]-current[i-1]-current[i+1]-previous[i]-next[i]);
		}
	}
	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));
}
int main()
{
	cv::Mat image=cv::imread("d:\\test\\opencv\\img.jpg",0);//注意灰色圖像
	cv::Mat out;
	sharpen(image,out);
	cv::namedWindow("result");
	cv::imshow("result",out);
	cv::namedWindow("img");
	cv::imshow("img",image);
	waitKey(0);
	return 0;
}
           
周遊圖像和鄰域操作,圖像銳化