OpenCV three ways to access image pixels

Source: Internet
Author: User

OpenCV's access image pixel three ways to access pixels

① pointer access: fastest

② iterator iterator: Slow, very secure, pointer access may be out of bounds

③ Dynamic Address calculation: slower, through at () implementation. Applies to the pixels of a row i, column J, and not the traversal pixel

mat is stored in memory form

The storage form of grayscale graph

    

The storage format of RGB

  

In general, the mat is continuously stored and connected by rows. The iscontinuous () function can be used to determine whether the matrix is continuously stored, and if it returns true continuously.

three ways to access pixels

1. Pointer access

void Visitimgbypointer (Mat &inputimg, Mat &dstimg)
{
    dstimg = Inputimg.clone ();
    int rows = dstimg.rows;
    int cols = Dstimg.cols * Dstimg.channels ();

    for (int i = 0; i < rows; i++)
    {
        uchar* data = dstimg.ptr<uchar> (i);
        for (int j = 0; J < cols; J + +)
        {
            data[j] = 0;  Process every pixel
            //add code
        }}
    } 

When the mat is continuously stored by row, all data can be accessed directly with the pointer.

void Visitcontinueimgbypointer (Mat &inputimg, Mat &dstimg)
{
    dstimg = Inputimg.clone ();
    int rows = dstimg.rows;
    int cols = Dstimg.cols;
    int channels = Dstimg.channels ();

    if (dstimg.iscontinuous ())
    {
        cols *= rows;
        rows = 1;
        cout << "is continuous" << Endl;
    }

    for (int i = 0; i < rows; i++)
    {
        uchar* data = dstimg.ptr<uchar> (i);
        for (int j = 0; J < cols * Channels; j + +)
        {
            data[j] = 155;  Process each pixel
            //add code
        }
    } 
    //If the store is contiguous, the equivalent of the following code
    //uchar* data = dstimg.data;
    for (int i = 0; i < cols * rows * channels; i++)
    //    data[i] = 155;    Processing per pixel

}
2. Iterator Access

void Visitimgbyiterator (Mat &inputimg, Mat &dstimg)
{
    dstimg = Inputimg.clone ();
    const INT channels = Dstimg.channels ();
  
    switch (Channels)
    {case
    1:
        {
            mat_<uchar>::iterator it= dstimg.begin<uchar> ();
            Mat_<uchar>::iterator itend= dstimg.end<uchar> ();
            for (; it!= itend; it++)//processing each pixel
            {
                *it =;
            }
            break;
        }
    Case 3:
        {
            mat_<vec3b>::iterator it3= dstimg.begin<vec3b> ();
            Mat_<vec3b>::iterator itend3= dstimg.end<vec3b> ();
            for (; it3!= itend3; it3++)//processing per pixel
            { 
                (*it3) [0]= 255;
                (*IT3) [1]= 0;
                (*IT3) [2]= 0;
            }
            Break;}}}

3. Dynamic Address access

void Visitimgbyat (Mat &inputimg, Mat &dstimg)
{
    dstimg = Inputimg.clone ();
    int rows = dstimg.rows;
    int cols = Dstimg.cols;
    int channels = Dstimg.channels ();

    switch (Channels)
    {case
    1:
        {for
            (int i = 0, i < rows; i++) for
                (int j = 0; J < cols; J + +)
                    D Stimg.at<uchar> (i,j) =;
            break;
        }
    Case 3:
        {for
            (int i = 0, i < rows; i++) for
                (int j = 0; J < cols; J + +)
                {
                    dstimg.at<vec3b& gt; (I,J) [0] =  0;
                    Dstimg.at<vec3b> (I,J) [1] =  0;
                    Dstimg.at<vec3b> (I,J) [2] = 255;
                }
            Break;}}}

4. Test code

#include <iostream> #include <opencv2/opencv.hpp> using namespace CV;


using namespace Std;
void Visitimgbypointer (Mat &inputimg, Mat &dstimg);
void Visitcontinueimgbypointer (Mat &inputimg, Mat &dstimg);
void Visitimgbyiterator (Mat &inputimg, Mat &dstimg);


void Visitimgbyat (Mat &inputimg, Mat &dstimg);
    int main () {Mat srcimg = Imread ("Pig.png"), dstimg;
    Mat grayimg;
    Cvtcolor (srcimg, grayimg, Cv_bgr2gray);
    Visitimgbypointer (SRCIMG,DSTIMG);
    
    Visitcontinueimgbypointer (GRAYIMG,DSTIMG);
    Visitimgbyiterator (SRCIMG,DSTIMG);
    
    Visitimgbyiterator (GRAYIMG,DSTIMG);
    Visitimgbyat (SRCIMG,DSTIMG);


    Visitimgbyat (GRAYIMG,DSTIMG);
    Imshow ("Original figure", srcimg);
    Imshow ("Grayscale graph", grayimg);


    Imshow ("Generate Diagram", dstimg);
    Waitkey (0);
return 0;
    } void Visitimgbypointer (Mat &inputimg, Mat &dstimg) {dstimg = Inputimg.clone ();
    int rows = dstimg.rows; int cols = Dstimg.cols * DstimG.channels ();
        for (int i = 0; i < rows; i++) {uchar* data = dstimg.ptr<uchar> (i);  for (int j = 0; J < cols; J + +) {Data[j] = 0; 
    Handle every pixel//add code}}} void Visitcontinueimgbypointer (Mat &inputimg, Mat &dstimg) {
    Dstimg = Inputimg.clone ();
    int rows = dstimg.rows;
    int cols = Dstimg.cols;


    int channels = Dstimg.channels ();
        if (dstimg.iscontinuous ()) {cols *= rows;
        rows = 1;
    cout << "is continuous" << Endl;
        } for (int i = 0; i < rows; i++) {uchar* data = dstimg.ptr<uchar> (i);  for (int j = 0; J < cols * Channels; j + +) {Data[j] = 155;
    Process each pixel//add code}}//If the store is contiguous, the equivalent of code//uchar* data = Dstimg.data;    for (int i = 0; i < cols * rows * channels; i++)//data[i] = 155; Processing per pixel} void Visitimgbyiterator (Mat &inPutimg, Mat &dstimg) {dstimg = Inputimg.clone ();
  
    const INT channels = Dstimg.channels ();
            Switch (Channels) {Case 1: {mat_<uchar>::iterator it= dstimg.begin<uchar> ();
            Mat_<uchar>::iterator itend= dstimg.end<uchar> ();
            for (; it!= itend; it++)//processing per pixel {*it = 150;
        } break;
            } Case 3: {mat_<vec3b>::iterator it3= dstimg.begin<vec3b> ();
            Mat_<vec3b>::iterator itend3= dstimg.end<vec3b> ();
                for (; it3!= itend3; it3++)//processing per pixel {(*it3) [0]= 255; (*IT3)
                [1]= 0; (*IT3)
            [2]= 0;
        } break;
    }}} void Visitimgbyat (Mat &inputimg, Mat &dstimg) {dstimg = Inputimg.clone ();
    int rows = dstimg.rows;
    int cols = Dstimg.cols;


  int channels = Dstimg.channels ();  Switch (Channels) {Case 1: {for (int i = 0, i < rows; i++) for (int j = 0; J < cols;
            J + +) dstimg.at<uchar> (i,j) = 150;
        Break
                } Case 3: {for (int i = 0, i < rows; i++) for (int j = 0; J < cols; J + +)
                    {dstimg.at<vec3b> (I,J) [0] = 0;
                    Dstimg.at<vec3b> (I,J) [1] = 0;
                Dstimg.at<vec3b> (I,J) [2] = 255;
        } break;
 }
    }
}

Original link: http://www.cnblogs.com/kuotian/p/6389260.html?utm_source=itdadao&utm_medium=referral

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.