opencv2学习- - - 像素操作

Posted QQ29850706

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv2学习- - - 像素操作相关的知识,希望对你有一定的参考价值。

/*
        像素访问
*/ 
 
#include <iostream>  
#include <cv.h>    
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>
 
using namespace std;
using namespace cv;  
 
int main(int argc, int* argv[])  
{  
    Mat image = imread("lena.bmp");
    Mat out1 = image.clone();

    long n_rows = out1.rows;
    long n_cols = out1.cols * out1.channels();

    /*
     *            行指针
     */
    uchar *p;
    for(long i=0; i<n_rows; i++)
    {
        p = out1.ptr<uchar>(i);
        for(long j=0; j<n_cols; j++)
        {
            p[j] = 255 - p[j];
        }
    }

    /*
     *            元素指针
     */
    Mat out2;
    out2.create(image.size(), image.type());  // reserve size and type but alloc new space

    n_rows = out2.rows;
    n_cols = out2.cols * out2.channels();

    p = out2.data;
    double c = 0.5;
    double b = 30;
    for (long i=0; i<n_rows*n_cols; i++)
    {
       *(p+i) = (uchar)(255 * 0.5 * ((double)(image.data[i]) / 255) + b);
    }     

   /*
     * Display
     */
    imshow("src_image", image);
    imshow("reverse_image", out1);
    imshow("mici_image", out2);
 
    waitKey(6000);  
    return 0;     
}  

 

以上是关于opencv2学习- - - 像素操作的主要内容,如果未能解决你的问题,请参考以下文章

学习Opencv 2.4.9 ---操作像素

opencv学习-像素重映射

OpenCV2.4+遍历读写像素方法总结及时间度量

垫子类型转换和访问单个像素的问题

OpenCV2.1,地图功能?访问每个像素?

OpenCV 2.x 中的像素访问错误