我正在阅读OpenCV 2计算机视觉应用程序设计手册。我已经执行了两个函数“sharpen”和“sharpen2D”,如果处理后的图像是灰度,则结果相同,但如果图像是彩色则结果不同。特别是对于“sharpen2D”功能而言,结果似乎也是正确的,并且对于“锐化”功能而言也是不可理解的。为什么?结果应该完全一样,还是我错了?
using namespace cv;
void sharpen(const Mat &image, Mat &result) {
// allocate if necessary
result.create(image.size(), image.type());
for (int j= 1; j<image.rows-1; j++) { // for all rows
// (except first and last)
const uchar* previous = image.ptr<const uchar>(j-1); // previous row
const uchar* current = image.ptr<const uchar>(j);
// current row
const uchar* next = image.ptr<const uchar>(j+1); // next row
uchar* output= result.ptr<uchar>(j); // output row
for (int i=1; i<image.cols-1; i++) {
*output++= saturate_cast<uchar>(
5*current[i]-current[i-1]
-current[i+1]-previous[i]-next[i]);
}
}
// Set the unprocess pixels to 0
result.row(0).setTo(Scalar(0));
result.row(result.rows-1).setTo(Scalar(0));
result.col(0).setTo(Scalar(0));
result.col(result.cols-1).setTo(Scalar(0));
}
void sharpen2D(const Mat &image, Mat &result) {
//kernel=matrice convoluta con l'immagine, stesso effetto della sharpen
// Construct kernel (all entries initialized to 0)
Mat kernel(3,3,CV_32F,Scalar(0));
// assigns kernel values
kernel.at<float>(1,1)= 5.0;
kernel.at<float>(0,1)= -1.0;
kernel.at<float>(2,1)= -1.0;
kernel.at<float>(1,0)= -1.0;
kernel.at<float>(1,2)= -1.0;
//filter the image
filter2D(image,result,image.depth(),kernel);
}
int main( int argc, char** argv )
{
Mat image, result, result2;
image = imread("a.jpg");
cvtColor( image, image, CV_BGR2GRAY );
namedWindow( "Image", CV_WINDOW_AUTOSIZE );
namedWindow( "Result", CV_WINDOW_AUTOSIZE );
namedWindow( "Result2", CV_WINDOW_AUTOSIZE );
sharpen(image,result);
sharpen2D(image,result2);
imshow("Image",image);
imshow("Result",result);
imshow("Result2",result2);
waitKey(0);
return 0;
}
感谢回复,我理解了我的错误,并且我修改了我的锐化功能,但是图像结果是完全黑的,我错了?
void sharpen(const Mat &image, Mat &result) {
// allocate if necessary
result.create(image.size(), image.type());
if (image.channels()==1){
for (int j= 1; j<image.rows-1; j++) { // for all rows
// (except first and last)
const uchar* previous = image.ptr<const uchar>(j-1); // previous row
const uchar* current = image.ptr<const uchar>(j);
// current row
const uchar* next = image.ptr<const uchar>(j+1); // next row
uchar* output= result.ptr<uchar>(j); // output row
for (int i=1; i<image.cols-1; i++) {
*output++= saturate_cast<uchar>(
5*current[i]-current[i-1
-current[i+1]-previous[i]-next[i]);
}
}
// Set the unprocess pixels to 0
result.row(0).setTo(Scalar(0));
result.row(result.rows-1).setTo(Scalar(0));
result.col(0).setTo(Scalar(0));
result.col(result.cols-1).setTo(Scalar(0));
}
if (image.channels()==3)//color image
{
vector<Mat> planes;
vector<Mat> planes2;
Mat image1,temp;
split(image,planes);
for(int k=0; k<3; k++)
{
image1.create(planes[k].size(), planes[k].type());
for (int j= 1; j<planes[k].rows-1; j++)
{
// for all rows
// (except first and last)
const uchar* previous = planes[k].ptr<const uchar>(j-1);
const uchar* current = planes[k].ptr<const uchar>(j);
const uchar* next = planes[k].ptr<const uchar>(j+1);
uchar* output= image1.ptr<uchar>(j); // output row
for (int i=1; i<planes[k].cols-1; i++)
{
*output= saturate_cast<uchar>(
5*current[i]-current[i-1]
-current[i+1]-previous[i]-next[i]);
}
}
image1.row(0).setTo(Scalar(0));
image1.row(image1.rows-1).setTo(Scalar(0));
image1.col(0).setTo(Scalar(0));
image1.col(image1.cols-1).setTo(Scalar(0));
planes[k]=image1;
}
merge(planes,result);
}
}