OPENCV学习笔记2-5_扫描图像并访问相邻像素

  To illustrate this recipe, we will apply a processing function that sharpens an image(锐化图像的处理函数). This time, the processing cannot be accomplished in-place. Users need to provide an output image. The image scanning(扫描)is done using three pointers, one for the current line, one for the line above, and another one for the line below.

  Since filtering is a common operation in image processing, OpenCV has defined a special function that performs this task: the cv::filter2D function. Note that it is particularly advantageous(特别有利) to use the filter2D function with a large kernel, in this case, a more efficient algorithm.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
void sharpen(const Mat &image, Mat &result) ;
void sharpen2D(const Mat &image, Mat &result);

int main()
{
    Mat image = imread("test.jpg");
    resize(image, image, Size(), 0.6, 0.6);
    Mat result1;
    Mat result2;

    namedWindow("YunFung Image");
    imshow("YunFung Image", image);

    sharpen(image, result1);
    namedWindow("Result1");
    imshow("Result1", result1);

    sharpen2D(image, result2);
    namedWindow("Result2");
    imshow("Result2", result2);

    waitKey();
    return 0;
}

void sharpen(const Mat &image, Mat &result) {
    result.create(image.size(), image.type());   // allocate if necessary
    int nchannels = image.channels();

    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 pointer
        //saturate_cast<uchar>  changing negative values to 0 and values over 255 to 255
        //sharpened_pixel = 5 * current - left - right - up - down;
        for (int i = nchannels; i < (image.cols - 1)*nchannels; i++) {
            *output++ = saturate_cast<uchar>(5 * current[i] - current[i - nchannels]
                                                                - current[i + nchannels] - previous[i] - next[i]);
        }
    }

    // Set the unprocessed pixels to 0   - left - right - up - down
    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) {

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

 

 

转载于:https://www.cnblogs.com/yunfung/p/7560811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值