Ask Your Question
3

image data processing in cv::Mat

asked Sep 22 '12

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

Preview: (hide)

4 answers

Sort by » oldest newest most voted
10

answered Sep 22 '12

updated Nov 22 '15

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:

Preview: (hide)

Comments

1

thanks that is what I am looking for

Thomas_N gravatar imageThomas_N (Sep 23 '12)edit
-1

answered Sep 23 '12

Thomas_N gravatar image

HI ALL. Thanks for the suggestions. But according to this tutorial http://docs.opencv.org/trunk/doc/tutorials/ios/video_processing/video_processing.html#opencviosvideoprocessing If I set self.videoCamera.grayscale = NO; data output will be 32 bit BGRA. So i still need to skip 1 byte data. Thanks

Preview: (hide)

Comments

Do you keep your images in BGRA format?

Michael Burdinov gravatar imageMichael Burdinov (Sep 24 '12)edit
-1

answered Sep 23 '12

updated Sep 23 '12

This might help.

Mat img = imread("filename.jpg",CV_LOAD_IMAGE_COLOR);
unsigned char *input = (unsigned char*)(img.data);
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.

Preview: (hide)

Comments

3

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 (Sep 23 '12)edit

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

jayrambhia gravatar imagejayrambhia (Sep 24 '12)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 (Sep 24 '12)edit
-2

answered Sep 27 '13

jecs89 gravatar image

updated Oct 22 '13

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.

Preview: (hide)

Comments

1

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 (Oct 22 '13)edit

Question Tools

Stats

Asked: Sep 22 '12

Seen: 11,633 times

Last updated: Nov 22 '15