Ask Your Question

image data processing in cv::Mat

asked 2012-09-22 12:24:15 -0500

Thomas_N gravatar image

Dear All, i have image info in Z matrix, say cv:Mat Z;

If i use x, y index to access the data in the unsigned char data matrix inside Z, how can I access the green channel data value only for that particular x, y index. I like to implement most efficient way in terms of processing speed. Thanks, Thomas

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2012-09-22 13:11:19 -0500

updated 2015-11-22 04:01:22 -0500

There are great tutorials in the OpenCV documentation. Just have a look at the available tutorials for the core module. The Core Functionality. You want to read:

edit flag offensive delete link more



thanks that is what I am looking for

Thomas_N gravatar imageThomas_N ( 2012-09-22 20:02:47 -0500 )edit

answered 2012-09-23 03:46:40 -0500

Thomas_N gravatar image

HI ALL. Thanks for the suggestions. But according to this tutorial If I set self.videoCamera.grayscale = NO; data output will be 32 bit BGRA. So i still need to skip 1 byte data. Thanks

edit flag offensive delete link more


Do you keep your images in BGRA format?

Michael Burdinov gravatar imageMichael Burdinov ( 2012-09-24 07:03:45 -0500 )edit

answered 2012-09-22 23:43:23 -0500

updated 2012-09-22 23:45:49 -0500

This might help.

Mat img = imread("filename.jpg",CV_LOAD_IMAGE_COLOR);
unsigned char *input = (unsigned char*)(;
int i,j,r,g,b;
for(int i = 0;i < img.cols;i++){
    for(int j = 0;j < img.rows;j++){
         b = input[img.cols * j + i ] ;
         g = input[img.cols * j + i + 1];
         r = input[img.cols * j + i + 2];

Here, g will give the green color for Row - i, Column -j . For more info Read this.

edit flag offensive delete link more



There 2 issues with correcteness of this code and 2 issues with performance. Correctness 1: instead of 'i' there should be '3 * i', because every pixel takes 3 bytes. Correctness 2: instead of 'img.cols * j' there should be 'img.step * j', because offset between 2 rows is at least 3 * img.cols (and may be more if this is ROI of bigger image). Performance 1: order of 'for' loops should be reversed, because Mat stored in row major order not column major. So scaning it column by column incurs huge penalty on memory access, especially for big images. Performance 2: proper order of loops also reduce amount of computaton needed per pixel. To conclude all this: see tutorial mentioned by Philipp for correct way of accessing pixels.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-09-23 02:32:57 -0500 )edit

Oh! Thanks for that. I guess I have been doing it all wrong. Should I delete the answer?

jayrambhia gravatar imagejayrambhia ( 2012-09-24 01:34:55 -0500 )edit

I don't know. Remove it if you wish. On one hand it will keep answers cleaner, but on other hand negative answer is also an answer.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-09-24 07:01:40 -0500 )edit

answered 2013-09-27 10:43:58 -0500

jecs89 gravatar image

updated 2013-10-22 15:15:04 -0500

If you're using gray images, you need to change.'Cause you don't have three channels.

cout<<(int)input[lung_left.step * j + i];

I don't understand why do i have bad score. I wanted to help.

edit flag offensive delete link more



Its simple. Your answer does not fit to the question. Its of course nice to see that you try to help, but the rating system is there to rate answers(magic involved). So if its not helpful, you will most likely get downvotes.

Moster gravatar imageMoster ( 2013-10-22 15:47:12 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2012-09-22 12:24:15 -0500

Seen: 9,838 times

Last updated: Nov 22 '15