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

Posted

技术标签:

【中文标题】OPENCV - 直接使用指针访问过滤图像并使用内核矩阵过滤【英文标题】:OPENCV - Filter an image directly with pointer acces and filter with kernel matrix 【发布时间】:2013-11-26 21:40:29 【问题描述】:

我正在阅读 OpenCV 2 Computer Vision Application Programming Cookbook 这本书。我已经执行了两个函数“锐化”和“锐化2D”,如果处理的图像是灰度的,结果是相同的,但如果图像是彩色的,结果是不同的。特别是在“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);


【问题讨论】:

【参考方案1】:

看起来您在锐化功能中处理的深度不同,因此,这可能是预期的结果。您可能想了解 OpenCV 如何在内存中存储 image。

【讨论】:

天哪,是的,在彩色情况下,我必须对所有 3 个通道进行锐化操作。

以上是关于OPENCV - 直接使用指针访问过滤图像并使用内核矩阵过滤的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 中使用 OpenCV 访问轮廓边界内的像素值

如果我需要访问底层数据,我应该为 Opencv 图像使用啥格式?

【OPENCV】cv::Mat像素遍历方法比较

浅析OpenCV中的BlobDetector

opencv4图像分割那些你不知道的API 代码可直接移植使用

opencv 访问图像像素的三种方式