ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Accessing pixel pointer

asked 2014-07-03 08:32:01 -0600

ROSkinect gravatar image

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?

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++)

Thank you.

edit retag flag offensive close merge delete


ptr_img_B seems to be missing here.

dornhege gravatar image dornhege  ( 2014-07-03 18:59:45 -0600 )edit

how so ?..

ROSkinect gravatar image ROSkinect  ( 2014-07-04 02:38:10 -0600 )edit

It is neither declared nor advanced ever.

dornhege gravatar image dornhege  ( 2014-07-04 05:12:12 -0600 )edit

Ya I just pick up this part of code just to understand the accessing pixel

ROSkinect gravatar image ROSkinect  ( 2014-07-04 05:28:36 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-07-03 11:39:12 -0600

ahendrix gravatar image

updated 2014-07-04 03:50:01 -0600

This is fairly normal pointer arithmetic, usually explained in a second or third computer science/C++ course.

This page explains it fairly well:

The OpenCV Mat Documentation describes how to access data in OpenCV images.

edit flag offensive delete link more


I don't want courses in pointer, my question was how we can use pointer to access to pixel. Anyway thank you

ROSkinect gravatar image ROSkinect  ( 2014-07-04 02:34:25 -0600 )edit

The sample you posed uses pointer arithmetic to access the value of each pixel in row-major order.

ahendrix gravatar image ahendrix  ( 2014-07-04 03:01:27 -0600 )edit

and what about the cols ? because as I know the image is like a two dimension table we can access to each pixel via i,j.. but using pointer I can't see clearly how we do that !

ROSkinect gravatar image ROSkinect  ( 2014-07-04 03:24:23 -0600 )edit

Same as in the question you asked here ptr_img_A = cv_ptr->image.ptr<float>(i); Gives you a pointer to the first element in the i'th row. ptr_img_A++ changes ptr_img_A from pointing to one column to pointing the next column...

Wolf gravatar image Wolf  ( 2014-07-04 04:20:22 -0600 )edit

Question Tools

1 follower


Asked: 2014-07-03 08:32:01 -0600

Seen: 388 times

Last updated: Jul 04 '14