Ask Your Question
0

Accessing pixel pointer

asked 2014-07-03 08:38:54 -0600

ROSpioneer gravatar image

updated 2014-07-03 08:40:13 -0600

Hi guys

Can you explain to me how the accessing pixel has been done in the double loop ? how do they change rows and cols and pixel's value like what is the (1,1) pixel coordinates for example ?

for(int i=0;i<cv_ptr->image.rows;i++)
    {   
        float* ptr_img_A = cv_ptr->image.ptr<float>(i); 

        for(int j=0;j<cv_ptr->image.cols;j++)
        {                   
            *ptr_img_B=255*(*ptr_img_A)/3.5;
             ptr_img_A++;   
        }           
    }

Thank you.

edit retag flag offensive close merge delete

Comments

I have not met cv_ptr until now. Can you post also the definition of the cv_ptr?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-07-03 08:42:39 -0600 )edit

OK this the code where it comes from: cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::TYPE_32FC1);

ROSpioneer gravatar imageROSpioneer ( 2014-07-03 09:05:06 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2014-07-03 10:15:18 -0600

You should have a look at that page for an overview of pixels' access in OpenCV.

But for a short answer, you could do like this:

float* p;
float* q;
for( int i = 0; i < image.rows; ++i)
{
    p = image.ptr< float >( i );
    q = image2.ptr< float >( i ); // I guess image2 is the same size as image1
    // and both image are in float!
    for ( int j = 0; j < image.cols; ++j)
    {
        // Do whatever you want
        p[ j ] = 255.f * q[ j ] / 3.5f;
    }
}
edit flag offensive delete link more

Comments

Thank you so much merci beaucoup je vois bien les choses maintenant;)

ROSpioneer gravatar imageROSpioneer ( 2014-07-04 03:24:47 -0600 )edit
1

answered 2014-07-03 09:33:58 -0600

thdrksdfthmn gravatar image

ROS image is an image in the Robot Operating System. cv_ptr is a pointer to that image. For a faster access a float pointer is made (I would prefer to use cv::Ptr, std::unique_ptr, or std::auto, instead). Every image.ptr<float>(i) is a pointer to the i-th row of the matrix image. Every element of that row pointer is an element on a column (row i, col j). So by the pointer, the element is accessed; the pointer points to the address of that float (image pixel).

I am not sure what exactly ptr_img_B is doing, but some kind of operation on that pixel (because ptr_img_A is incremented)

edit flag offensive delete link more

Comments

Thank you so much It's clear

ROSpioneer gravatar imageROSpioneer ( 2014-07-04 03:25:05 -0600 )edit

Question Tools

Stats

Asked: 2014-07-03 08:38:54 -0600

Seen: 1,993 times

Last updated: Jul 03 '14