Ask Your Question
0

Accessing pixel image c++

asked 2014-06-19 05:54:39 -0500

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

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2014-06-19 08:31:36 -0500

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.

edit flag offensive delete link more
2

answered 2014-06-19 08:41:27 -0500

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-06-19 05:54:39 -0500

Seen: 1,136 times

Last updated: Jun 19 '14