First time here? Check out the FAQ!

Ask Your Question
0

Accessing pixel image c++

asked Jun 19 '14

ROSpioneer gravatar image
cv::Mat depth_clean(cv_depthptr->image.rows, cv_depthptr->image.cols, CV_32FC1);
cv::Mat img(cv_depthptr->image.rows, cv_depthptr->image.cols, CV_8UC1);
for(int i = 0; i < cv_depthptr->image.rows; i++)
{
    float* Di = cv_depthptr->image.ptr<float>(i);
    float* Ii = depth_clean.ptr<float>(i);
    char* Ivi = img.ptr<char>(i);
        for(int j = 0; j < cv_depthptr->image.cols; j++)
        {   
           if(Di[j] > 0.0f){                        
            Ii[j] = Di[j];
            Ivi[j] = (char) (255*((Di[j])/(5.5))); // some suitable values..
           }
            else{
            Ii[j] = 0.0f;
            Ivi[j] = 0;
            }
        }   
}

Can anyone explain to me using that program how the accessing to the pixel has been done ? using pointer and the two loops and what's the rule: if you have any course or somthing like that?

Thank you so much

Preview: (hide)

2 answers

Sort by » oldest newest most voted
4

answered Jun 19 '14

I think it's very simple. You can access pixel (i,j) in the following way:

Mat ExampleMat(n,n,CV_32F1);
//assume you want to access the pixel at location row = i, column = j
float * rowP = ExampleMat.ptr<float>(i);
float pixelValue = rowP[j];

It's faster than the regular way:

float pixelValue = ExampleMat.at<float>(i,j);

In the code you provided, they go over the rows in the outer loop and over the columns in the inner loop.

Please let me know if you have any further question or if something is not clear.

Gil.

Preview: (hide)
3

answered Jun 19 '14

Michael Burdinov gravatar image

There quite good tutorial that explains and compares different ways of accessing pixel. Read it and you will have good understanding of your options.

Preview: (hide)

Question Tools

Stats

Asked: Jun 19 '14

Seen: 6,186 times

Last updated: Jun 19 '14