#include "stdafx.h"
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
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]);
}
}
//将未处理的像素设置为0
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 _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image = cv::imread("boldt.jpg");
cv::Mat myImage;
sharpen(image,myImage);
cv::namedWindow("Image");
cv::imshow("Image", image);
cv::imwrite("output.jpg", image);
cvWaitKey();
return 0;
}