ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

accessing pixel image c++

asked 2014-06-18 07:33:09 -0500

ROSkinect gravatar image

Hi there,

    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..
                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 the different between cv_depthptr->image.ptr<float>(i) and Di[j] ?

Thank you so much

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-06-19 02:16:52 -0500

Wolf gravatar image

What is your use case?

The image.ptr<float>(i) gives you a pointer to the first element (element at position zero) in the i'th row.

The call Di[j] gives you a read/write reference to the element j positions "behind" where Di is pointing.

Basically this is how element access in OpenCV could be done with pointers. However, OpenCV2 provides you more convienient way to access your elements such as the cv::Mat::at<T>() method (slow but convenient if you just need to modifiy a couple of pixels) or the cv::MatIterator<T> (the proper way to iterate all elements of your matrix)...

See OpenCV Doc:

edit flag offensive delete link more


OK I don't care about the use of the program I want to understand how the stuffs work because I just start learning Opencv. Thank you so much

ROSkinect gravatar image ROSkinect  ( 2014-06-19 02:53:05 -0500 )edit

You should ask this question here: > or here > or check the documentation of OpenCV as suggested Wolf.

Mehdi. gravatar image Mehdi.  ( 2014-06-19 05:18:46 -0500 )edit

Well, it slightly depends on your use case whether it is suitable to use pointers, Mat::at or MatIterators to access the pixels.... The example you gave just requests a pointer to the 1. element of each row and then applies simple pointer arithmetic to get the following elements of the row...

Wolf gravatar image Wolf  ( 2014-06-19 07:34:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-06-18 07:33:09 -0500

Seen: 2,072 times

Last updated: Jun 19 '14