设为首页 加入收藏

TOP

OpenCV操作像素深入理解(二)
2018-11-24 22:08:37 】 浏览:479
Tags:OpenCV 操作 像素 深入 理解
bsp; data[j] = data[j]/div*div + div/2;
        }
    }
}


int main()
{
    cv::Mat image = cv::imread("boldt.jpg");


    reduceColor(image, 64);
    cv::namedWindow("image");
    cv::imshow("image", image);
    cv::waitKey();
    return 0;
}



当然刚刚的这个代码是用普通的遍历完成的,我们也可以用迭代器完成对像素的遍历


void reduceColorNew(cv::Mat image, int div=64) {


    // div must be a power of 2
    int n= static_cast<int>(log(static_cast<double>(div))/log(2.0) + 0.5);
    // mask used to round the pixel value
    uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0
    uchar div2 = div >> 1; // div2 = div/2


    // get iterators
    cv::Mat_<cv::Vec3b>::iterator it= image.begin<cv::Vec3b>();
    cv::Mat_<cv::Vec3b>::iterator itend= image.end<cv::Vec3b>();


    // scan all pixels
    for ( ; it!= itend; ++it) {


        // process each pixel ---------------------


        (*it)[0]&= mask;
        (*it)[0]+= div2;
        (*it)[1]&= mask;
        (*it)[1]+= div2;
        (*it)[2]&= mask;
        (*it)[2]+= div2;


        // end of pixel processing ----------------
    }
}


在图像处理中经常有这样的处理函数,它在计算每个像素的数值时,需要使用周边像素的值。 如果相邻像素在上一行或下一行,就需要同时扫描图像的多行。


我们将使用一个锐化图像的处理函数。它基于拉普拉斯算子。在图像处理领域有一个众所周知的结论:如果从图像中减去拉普拉斯算子部分,图像 的边缘就会放大,因而图像会变得更加尖锐。


实现思路:


#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>


void sharpen(const cv::Mat &image, cv::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


        for (int i=nchannels; i<(image.cols-1)*nchannels; i++) {


            // apply sharpening operator
            *output++= cv::saturate_cast<uchar>(5*current[i]-current[i-nchannels]-current[i+nchannels]-previous[i]-next[i]);
        }
    }


    // Set the unprocess pixels to 0
    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));
}



int main()
{
    cv::Mat image= cv::imread("boldt.jpg");
    if (!image.data)
        return 0;


    cv::Mat result;


    double time= static_cast<double

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇OpenCV颜色转换和皮肤检测 下一篇Shell编程基础进阶

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目