c++ - 锐化图像 - 访问邻近像素

标签 c++ visual-studio-2010 image opencv image-processing

我正在编写以下代码:

#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

void Sharpen(Mat &,Mat);

int main()
{
    Mat image,result;

    try
    {
        image = imread("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");

        if(!image.data)
        {
            throw 1;
        }
    }
    catch(int i)
    {
        cout << "Image is unable to reae" << endl;
    }

    Sharpen(image,result);
    waitKey(0);
}

void Sharpen(Mat &image,Mat result)
{
    //result = image.clone();
    result.create(image.size(), image.type());

    //For all rows except first and last
    for(int i=1;i<image.rows-1;i++)
    {
        const uchar *previous = image.ptr<const uchar>(i-1);
        const uchar *next = image.ptr<const uchar>(i+1);
        const uchar *current = image.ptr<const uchar>(i);

        uchar *output = result.ptr<uchar>(i);

        //For all columns except first and last
        for(int a=1;a<image.cols-1;a++)
        {
            *output++ = cv::saturate_cast<uchar>(5*current[a]-current[a-1]-current[a+1]-previous[a]-next[a]);
        }

    }

    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));

    namedWindow("Original");
    imshow("Original",image);

    namedWindow("Duplicate");
    imshow("Duplicate",result);
}

在这里,我想做的是锐化图像。以下是图像锐化的公式。

resultPixel = 5*currentPixel-previousPixel-nextPixel-upperPixel-belowPixel

无论如何,以下是我得到的输出

enter image description here

如您所见,我没有得到预期的结果。做错了什么?

最佳答案

您的第一个问题是每个像素有 3 个字节,因此您仅迭代图像的三分之一以上。您的另一个问题是您无法执行 *output++,因为您想跳过第一个像素。将这两件事放在一起会产生以下代码,它给出您正在寻找的结果。

#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

void Sharpen(Mat &image,Mat& result)
{
    result.create(image.size(), image.type());

    //For all rows except first and last
    for(int i=1;i<image.rows-1;i++)
    {
        const uchar *previous = image.ptr<const uchar>(i-1);
        const uchar *next = image.ptr<const uchar>(i+1);
        const uchar *current = image.ptr<const uchar>(i);

        uchar *output = result.ptr<uchar>(i);

        //For all columns except first and last
        for(int a=3;a<(image.cols-1)*3;a++)
        {
            output[a] = cv::saturate_cast<uchar>(5*current[a]-current[a-1]-current[a+1]-previous[a]-next[a]);
        }

    }

    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));

    namedWindow("Original");
    imshow("Original",image);

    namedWindow("Duplicate");
    imshow("Duplicate",result);

}

int main()
{
    Mat image,result;

    try
    {
        image = imread("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg");

        if(!image.data)
        {
            throw 1;
        }
    }
    catch(int i)
    {
        cout << "Image is unable to reae" << endl;
    }

    Sharpen(image,result);
    waitKey(0);
}

关于c++ - 锐化图像 - 访问邻近像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16117597/

相关文章:

php - 如何使用 PHP 在图像上显示热图?

c#命名管道双向通信

C++ getline 双重错误

c++ - 如何在编译时检查值和限制

.net - 在 VS2010 数据库项目中部署时设置 DefaultDataPath 和 DefaultLogPath

php - 由于某种原因,我的链接栏上的背景图片没有显示

c++ - 从 QTreeView 中删除项目时取消选择所有行

visual-studio-2010 - MVC 的 Visual Studio 和文件未找到警告

asp.net - 如何在 Visual Studio 2010 的输出窗口中读取 Console.WriteLine() 的结果

python - Pandas - 图像到 DataFrame