gpt4 book ai didi

c++ - OPENCV - 直接使用指针访问过滤图像并使用内核矩阵过滤

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:22 24 4
gpt4 key购买 nike

我正在阅读 OpenCV 2 Computer Vision Application Programming Cookbook 这本书。我已经执行了两个函数“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);
}
}

最佳答案

看起来您在锐化函数中没有处理不同的深度,因此,这可能是预期的结果。您可能想阅读有关 OpenCV 如何存储 image 的信息。在内存中。

关于c++ - OPENCV - 直接使用指针访问过滤图像并使用内核矩阵过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20228533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com