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

OPENCV - Filter an image directly with pointer acces and filter with kernel matrix

本文关键字:过滤 内核 图像 指针 访问 OPENCV-      更新时间:2023-10-16

我正在读《OpenCV 2计算机视觉应用程序编程食谱》一书。我执行了两个函数"锐化"answers"锐化2D",如果处理的图像是灰度级的,结果是一样的,但如果图像是彩色的,结果就不同了。特别是,对于"锐化2D"函数,在颜色情况下,结果似乎也是正确的,而对于"锐化"函数,结果是不可理解的。为什么?结果应该完全一样,还是我错了?

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如何在内存中存储图像。