How can retrieve pixel value using CvMat The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceAccessing certain pixel RGB value in openCVOpenCV get pixel channel value from Mat imagexcode CVpixelBuffer shows negative valuesHow do you set, clear, and toggle a single bit?How do I iterate over the words of a string?How can I profile C++ code running on Linux?Why can templates only be implemented in the header file?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhat is the difference between 'typedef' and 'using' in C++11?Why is my program slow when looping over exactly 8192 elements?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations

Word for: a synonym with a positive connotation?

How to create a folder symlink that has a different name?

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

Do I have Disadvantage attacking with an off-hand weapon?

How did passengers keep warm on sail ships?

Why can I use a list index as an indexing variable in a for loop?

What force causes entropy to increase?

Solving overdetermined system by QR decomposition

Working through the single responsibility principle (SRP) in Python when calls are expensive

Pretty sure I'm over complicating my loops but unsure how to simplify

Define a list range inside a list

"is" operation returns false even though two objects have same id

Is an up-to-date browser secure on an out-of-date OS?

TDS update packages don't remove unneeded items

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Identify 80s or 90s comics with ripped creatures (not dwarves)

Was credit for the black hole image misappropriated?

What's the point in a preamp?

Do working physicists consider Newtonian mechanics to be "falsified"?

What information about me do stores get via my credit card?

Is there a way to generate uniformly distributed points on a sphere from a fixed amount of random real numbers per point?

Am I ethically obligated to go into work on an off day if the reason is sudden?

What can I do if neighbor is blocking my solar panels intentionally?

What other Star Trek series did the main TNG cast show up in?



How can retrieve pixel value using CvMat



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceAccessing certain pixel RGB value in openCVOpenCV get pixel channel value from Mat imagexcode CVpixelBuffer shows negative valuesHow do you set, clear, and toggle a single bit?How do I iterate over the words of a string?How can I profile C++ code running on Linux?Why can templates only be implemented in the header file?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhat is the difference between 'typedef' and 'using' in C++11?Why is my program slow when looping over exactly 8192 elements?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















img->data.ptr[i,j]=img1.data.ptr[(m_c*w_in)+n_c];


I tried this but it is showing me only one value.
Any help can be appreciated.










share|improve this question






























    0















    img->data.ptr[i,j]=img1.data.ptr[(m_c*w_in)+n_c];


    I tried this but it is showing me only one value.
    Any help can be appreciated.










    share|improve this question


























      0












      0








      0








      img->data.ptr[i,j]=img1.data.ptr[(m_c*w_in)+n_c];


      I tried this but it is showing me only one value.
      Any help can be appreciated.










      share|improve this question
















      img->data.ptr[i,j]=img1.data.ptr[(m_c*w_in)+n_c];


      I tried this but it is showing me only one value.
      Any help can be appreciated.







      c++ opencv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 19 '15 at 12:47









      Antonio

      11.9k644145




      11.9k644145










      asked Feb 19 '15 at 10:14









      Himanshu SharmaHimanshu Sharma

      41




      41






















          4 Answers
          4






          active

          oldest

          votes


















          1














          first of all why are you using the old interface. If you have new opencv then convert the CvMat to cv::Mat and then do the operations. Once you are done then you can convert the Mat back to CvMat.






          share|improve this answer






























            1














            First of all, switch to cv::Mat
            Then, you have several ways to access pixel x,y:



            cv::Mat img;
            int x,y;

            //[...] Initialize here x and y

            cv::Point p(x,y);
            int stride = img.step1();

            //All of these are valid ways to access pixel x,y
            img.at<uint8_t>(y,x); //Or, for example, cv::Vec3b in place of uint8_t in case of color images
            img.at<uint8_t>(p);
            //The following are valid only for grayscale 8-bit images, otherwise they have to be modified a bit
            img.ptr(y)[x];
            img.ptr()[y * stride + x];


            In fact, once you switch to cv::Mat you can find other extensive answers here OpenCV get pixel channel value from Mat image and here Accessing certain pixel RGB value in openCV






            share|improve this answer

























            • ^^^the ptr versions will need a type, too.

              – berak
              Feb 19 '15 at 12:34






            • 1





              @berak Not if you are using grayscale 8-bit images

              – Antonio
              Feb 19 '15 at 12:35












            • @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

              – Himanshu Sharma
              Feb 26 '15 at 10:09


















            0














            This is an old question, just for anybody who do not have the luxury to use the newer cv:mat format, and must use cvmat to access pixel. Tested using OpenCV 1.1.



            static unsigned long get_color(IplImage *img, CvPoint* pt, double *luma) 
            uchar blue, green, red;
            unsigned long color = 0;
            CvMat hdr;
            CvMat *mat = cvGetMat(img, &hdr);
            int col = mat->step / mat->cols;
            uchar *pix = mat->data.ptr + (pt->y * mat->step + pt->x * col);

            if (col == 1)
            // Grayscale
            color = *pix;
            blue = color * 11 / 100;
            green = color * 59 / 100;
            red = color * 30 / 100;
            else if (col == 3)
            // 3 channel RGB
            blue = *pix;
            green = *(pix + 1);
            red = *(pix + 2);
            color = red << 16 else
            printf("Unsupported number of channel %dn", col);
            return 0;


            if (luma)
            *luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

            printf("nnb=%x g=%x, r=%x color=%xn", blue, green, red, color);
            printf("cols=%d, step=%d, col=%d, x=%d, y=%d loc=%dn",
            mat->cols, mat->step, col, pt->x, pt->y,
            (pt->y * mat->step + pt->x * col));
            return color;



            Output:



            1. Output from a grayscaled 600x600 Red.jpeg file
            // Pixel (0,0)
            b=8 g=2c, r=16 color=4c
            cols=600, step=600, col=1, x=0, y=0 loc=0

            // Pixel (1,0)
            b=8 g=2c, r=16 color=4c
            cols=600, step=600, col=1, x=1, y=0 loc=1

            // Pixel (1,1)
            b=8 g=2c, r=16 color=4c
            cols=600, step=600, col=1, x=1, y=1 loc=601


            2. Output from a 3 channel rgb 600x600 Red.jpeg file
            // Pixel (0,0)
            b=0 g=0, r=fe color=fe0000
            cols=600, step=1800, col=3, x=0, y=0 loc=0

            // Pixel (1,0)
            b=0 g=0, r=fe color=fe0000
            cols=600, step=1800, col=3, x=1, y=0 loc=3

            // Pixel (1,1)
            cols=600, step=1800, col=3, x=1, y=1 loc=1803
            b=0 g=0, r=fe color=fe0000





            share|improve this answer






























              -1














              for accessing data using CvMat you have to use "img->data.ptr[x*col+y]" it can used to store data of uchar. CvMat also support double,float,string and integer type. So you can store data according to your convince.






              share|improve this answer























              • x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                – Antonio
                Feb 26 '15 at 10:41












              • The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                – Antonio
                Feb 26 '15 at 10:51












              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28603693%2fhow-can-retrieve-pixel-value-using-cvmat%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              first of all why are you using the old interface. If you have new opencv then convert the CvMat to cv::Mat and then do the operations. Once you are done then you can convert the Mat back to CvMat.






              share|improve this answer



























                1














                first of all why are you using the old interface. If you have new opencv then convert the CvMat to cv::Mat and then do the operations. Once you are done then you can convert the Mat back to CvMat.






                share|improve this answer

























                  1












                  1








                  1







                  first of all why are you using the old interface. If you have new opencv then convert the CvMat to cv::Mat and then do the operations. Once you are done then you can convert the Mat back to CvMat.






                  share|improve this answer













                  first of all why are you using the old interface. If you have new opencv then convert the CvMat to cv::Mat and then do the operations. Once you are done then you can convert the Mat back to CvMat.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 19 '15 at 10:36









                  Anubhav RohatgiAnubhav Rohatgi

                  386315




                  386315























                      1














                      First of all, switch to cv::Mat
                      Then, you have several ways to access pixel x,y:



                      cv::Mat img;
                      int x,y;

                      //[...] Initialize here x and y

                      cv::Point p(x,y);
                      int stride = img.step1();

                      //All of these are valid ways to access pixel x,y
                      img.at<uint8_t>(y,x); //Or, for example, cv::Vec3b in place of uint8_t in case of color images
                      img.at<uint8_t>(p);
                      //The following are valid only for grayscale 8-bit images, otherwise they have to be modified a bit
                      img.ptr(y)[x];
                      img.ptr()[y * stride + x];


                      In fact, once you switch to cv::Mat you can find other extensive answers here OpenCV get pixel channel value from Mat image and here Accessing certain pixel RGB value in openCV






                      share|improve this answer

























                      • ^^^the ptr versions will need a type, too.

                        – berak
                        Feb 19 '15 at 12:34






                      • 1





                        @berak Not if you are using grayscale 8-bit images

                        – Antonio
                        Feb 19 '15 at 12:35












                      • @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

                        – Himanshu Sharma
                        Feb 26 '15 at 10:09















                      1














                      First of all, switch to cv::Mat
                      Then, you have several ways to access pixel x,y:



                      cv::Mat img;
                      int x,y;

                      //[...] Initialize here x and y

                      cv::Point p(x,y);
                      int stride = img.step1();

                      //All of these are valid ways to access pixel x,y
                      img.at<uint8_t>(y,x); //Or, for example, cv::Vec3b in place of uint8_t in case of color images
                      img.at<uint8_t>(p);
                      //The following are valid only for grayscale 8-bit images, otherwise they have to be modified a bit
                      img.ptr(y)[x];
                      img.ptr()[y * stride + x];


                      In fact, once you switch to cv::Mat you can find other extensive answers here OpenCV get pixel channel value from Mat image and here Accessing certain pixel RGB value in openCV






                      share|improve this answer

























                      • ^^^the ptr versions will need a type, too.

                        – berak
                        Feb 19 '15 at 12:34






                      • 1





                        @berak Not if you are using grayscale 8-bit images

                        – Antonio
                        Feb 19 '15 at 12:35












                      • @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

                        – Himanshu Sharma
                        Feb 26 '15 at 10:09













                      1












                      1








                      1







                      First of all, switch to cv::Mat
                      Then, you have several ways to access pixel x,y:



                      cv::Mat img;
                      int x,y;

                      //[...] Initialize here x and y

                      cv::Point p(x,y);
                      int stride = img.step1();

                      //All of these are valid ways to access pixel x,y
                      img.at<uint8_t>(y,x); //Or, for example, cv::Vec3b in place of uint8_t in case of color images
                      img.at<uint8_t>(p);
                      //The following are valid only for grayscale 8-bit images, otherwise they have to be modified a bit
                      img.ptr(y)[x];
                      img.ptr()[y * stride + x];


                      In fact, once you switch to cv::Mat you can find other extensive answers here OpenCV get pixel channel value from Mat image and here Accessing certain pixel RGB value in openCV






                      share|improve this answer















                      First of all, switch to cv::Mat
                      Then, you have several ways to access pixel x,y:



                      cv::Mat img;
                      int x,y;

                      //[...] Initialize here x and y

                      cv::Point p(x,y);
                      int stride = img.step1();

                      //All of these are valid ways to access pixel x,y
                      img.at<uint8_t>(y,x); //Or, for example, cv::Vec3b in place of uint8_t in case of color images
                      img.at<uint8_t>(p);
                      //The following are valid only for grayscale 8-bit images, otherwise they have to be modified a bit
                      img.ptr(y)[x];
                      img.ptr()[y * stride + x];


                      In fact, once you switch to cv::Mat you can find other extensive answers here OpenCV get pixel channel value from Mat image and here Accessing certain pixel RGB value in openCV







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 23 '17 at 11:50









                      Community

                      11




                      11










                      answered Feb 19 '15 at 12:31









                      AntonioAntonio

                      11.9k644145




                      11.9k644145












                      • ^^^the ptr versions will need a type, too.

                        – berak
                        Feb 19 '15 at 12:34






                      • 1





                        @berak Not if you are using grayscale 8-bit images

                        – Antonio
                        Feb 19 '15 at 12:35












                      • @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

                        – Himanshu Sharma
                        Feb 26 '15 at 10:09

















                      • ^^^the ptr versions will need a type, too.

                        – berak
                        Feb 19 '15 at 12:34






                      • 1





                        @berak Not if you are using grayscale 8-bit images

                        – Antonio
                        Feb 19 '15 at 12:35












                      • @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

                        – Himanshu Sharma
                        Feb 26 '15 at 10:09
















                      ^^^the ptr versions will need a type, too.

                      – berak
                      Feb 19 '15 at 12:34





                      ^^^the ptr versions will need a type, too.

                      – berak
                      Feb 19 '15 at 12:34




                      1




                      1





                      @berak Not if you are using grayscale 8-bit images

                      – Antonio
                      Feb 19 '15 at 12:35






                      @berak Not if you are using grayscale 8-bit images

                      – Antonio
                      Feb 19 '15 at 12:35














                      @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

                      – Himanshu Sharma
                      Feb 26 '15 at 10:09





                      @Antonio thank you very much for your suggestion But I found the solution and I am posting it so that some get helped.

                      – Himanshu Sharma
                      Feb 26 '15 at 10:09











                      0














                      This is an old question, just for anybody who do not have the luxury to use the newer cv:mat format, and must use cvmat to access pixel. Tested using OpenCV 1.1.



                      static unsigned long get_color(IplImage *img, CvPoint* pt, double *luma) 
                      uchar blue, green, red;
                      unsigned long color = 0;
                      CvMat hdr;
                      CvMat *mat = cvGetMat(img, &hdr);
                      int col = mat->step / mat->cols;
                      uchar *pix = mat->data.ptr + (pt->y * mat->step + pt->x * col);

                      if (col == 1)
                      // Grayscale
                      color = *pix;
                      blue = color * 11 / 100;
                      green = color * 59 / 100;
                      red = color * 30 / 100;
                      else if (col == 3)
                      // 3 channel RGB
                      blue = *pix;
                      green = *(pix + 1);
                      red = *(pix + 2);
                      color = red << 16 else
                      printf("Unsupported number of channel %dn", col);
                      return 0;


                      if (luma)
                      *luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

                      printf("nnb=%x g=%x, r=%x color=%xn", blue, green, red, color);
                      printf("cols=%d, step=%d, col=%d, x=%d, y=%d loc=%dn",
                      mat->cols, mat->step, col, pt->x, pt->y,
                      (pt->y * mat->step + pt->x * col));
                      return color;



                      Output:



                      1. Output from a grayscaled 600x600 Red.jpeg file
                      // Pixel (0,0)
                      b=8 g=2c, r=16 color=4c
                      cols=600, step=600, col=1, x=0, y=0 loc=0

                      // Pixel (1,0)
                      b=8 g=2c, r=16 color=4c
                      cols=600, step=600, col=1, x=1, y=0 loc=1

                      // Pixel (1,1)
                      b=8 g=2c, r=16 color=4c
                      cols=600, step=600, col=1, x=1, y=1 loc=601


                      2. Output from a 3 channel rgb 600x600 Red.jpeg file
                      // Pixel (0,0)
                      b=0 g=0, r=fe color=fe0000
                      cols=600, step=1800, col=3, x=0, y=0 loc=0

                      // Pixel (1,0)
                      b=0 g=0, r=fe color=fe0000
                      cols=600, step=1800, col=3, x=1, y=0 loc=3

                      // Pixel (1,1)
                      cols=600, step=1800, col=3, x=1, y=1 loc=1803
                      b=0 g=0, r=fe color=fe0000





                      share|improve this answer



























                        0














                        This is an old question, just for anybody who do not have the luxury to use the newer cv:mat format, and must use cvmat to access pixel. Tested using OpenCV 1.1.



                        static unsigned long get_color(IplImage *img, CvPoint* pt, double *luma) 
                        uchar blue, green, red;
                        unsigned long color = 0;
                        CvMat hdr;
                        CvMat *mat = cvGetMat(img, &hdr);
                        int col = mat->step / mat->cols;
                        uchar *pix = mat->data.ptr + (pt->y * mat->step + pt->x * col);

                        if (col == 1)
                        // Grayscale
                        color = *pix;
                        blue = color * 11 / 100;
                        green = color * 59 / 100;
                        red = color * 30 / 100;
                        else if (col == 3)
                        // 3 channel RGB
                        blue = *pix;
                        green = *(pix + 1);
                        red = *(pix + 2);
                        color = red << 16 else
                        printf("Unsupported number of channel %dn", col);
                        return 0;


                        if (luma)
                        *luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

                        printf("nnb=%x g=%x, r=%x color=%xn", blue, green, red, color);
                        printf("cols=%d, step=%d, col=%d, x=%d, y=%d loc=%dn",
                        mat->cols, mat->step, col, pt->x, pt->y,
                        (pt->y * mat->step + pt->x * col));
                        return color;



                        Output:



                        1. Output from a grayscaled 600x600 Red.jpeg file
                        // Pixel (0,0)
                        b=8 g=2c, r=16 color=4c
                        cols=600, step=600, col=1, x=0, y=0 loc=0

                        // Pixel (1,0)
                        b=8 g=2c, r=16 color=4c
                        cols=600, step=600, col=1, x=1, y=0 loc=1

                        // Pixel (1,1)
                        b=8 g=2c, r=16 color=4c
                        cols=600, step=600, col=1, x=1, y=1 loc=601


                        2. Output from a 3 channel rgb 600x600 Red.jpeg file
                        // Pixel (0,0)
                        b=0 g=0, r=fe color=fe0000
                        cols=600, step=1800, col=3, x=0, y=0 loc=0

                        // Pixel (1,0)
                        b=0 g=0, r=fe color=fe0000
                        cols=600, step=1800, col=3, x=1, y=0 loc=3

                        // Pixel (1,1)
                        cols=600, step=1800, col=3, x=1, y=1 loc=1803
                        b=0 g=0, r=fe color=fe0000





                        share|improve this answer

























                          0












                          0








                          0







                          This is an old question, just for anybody who do not have the luxury to use the newer cv:mat format, and must use cvmat to access pixel. Tested using OpenCV 1.1.



                          static unsigned long get_color(IplImage *img, CvPoint* pt, double *luma) 
                          uchar blue, green, red;
                          unsigned long color = 0;
                          CvMat hdr;
                          CvMat *mat = cvGetMat(img, &hdr);
                          int col = mat->step / mat->cols;
                          uchar *pix = mat->data.ptr + (pt->y * mat->step + pt->x * col);

                          if (col == 1)
                          // Grayscale
                          color = *pix;
                          blue = color * 11 / 100;
                          green = color * 59 / 100;
                          red = color * 30 / 100;
                          else if (col == 3)
                          // 3 channel RGB
                          blue = *pix;
                          green = *(pix + 1);
                          red = *(pix + 2);
                          color = red << 16 else
                          printf("Unsupported number of channel %dn", col);
                          return 0;


                          if (luma)
                          *luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

                          printf("nnb=%x g=%x, r=%x color=%xn", blue, green, red, color);
                          printf("cols=%d, step=%d, col=%d, x=%d, y=%d loc=%dn",
                          mat->cols, mat->step, col, pt->x, pt->y,
                          (pt->y * mat->step + pt->x * col));
                          return color;



                          Output:



                          1. Output from a grayscaled 600x600 Red.jpeg file
                          // Pixel (0,0)
                          b=8 g=2c, r=16 color=4c
                          cols=600, step=600, col=1, x=0, y=0 loc=0

                          // Pixel (1,0)
                          b=8 g=2c, r=16 color=4c
                          cols=600, step=600, col=1, x=1, y=0 loc=1

                          // Pixel (1,1)
                          b=8 g=2c, r=16 color=4c
                          cols=600, step=600, col=1, x=1, y=1 loc=601


                          2. Output from a 3 channel rgb 600x600 Red.jpeg file
                          // Pixel (0,0)
                          b=0 g=0, r=fe color=fe0000
                          cols=600, step=1800, col=3, x=0, y=0 loc=0

                          // Pixel (1,0)
                          b=0 g=0, r=fe color=fe0000
                          cols=600, step=1800, col=3, x=1, y=0 loc=3

                          // Pixel (1,1)
                          cols=600, step=1800, col=3, x=1, y=1 loc=1803
                          b=0 g=0, r=fe color=fe0000





                          share|improve this answer













                          This is an old question, just for anybody who do not have the luxury to use the newer cv:mat format, and must use cvmat to access pixel. Tested using OpenCV 1.1.



                          static unsigned long get_color(IplImage *img, CvPoint* pt, double *luma) 
                          uchar blue, green, red;
                          unsigned long color = 0;
                          CvMat hdr;
                          CvMat *mat = cvGetMat(img, &hdr);
                          int col = mat->step / mat->cols;
                          uchar *pix = mat->data.ptr + (pt->y * mat->step + pt->x * col);

                          if (col == 1)
                          // Grayscale
                          color = *pix;
                          blue = color * 11 / 100;
                          green = color * 59 / 100;
                          red = color * 30 / 100;
                          else if (col == 3)
                          // 3 channel RGB
                          blue = *pix;
                          green = *(pix + 1);
                          red = *(pix + 2);
                          color = red << 16 else
                          printf("Unsupported number of channel %dn", col);
                          return 0;


                          if (luma)
                          *luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;

                          printf("nnb=%x g=%x, r=%x color=%xn", blue, green, red, color);
                          printf("cols=%d, step=%d, col=%d, x=%d, y=%d loc=%dn",
                          mat->cols, mat->step, col, pt->x, pt->y,
                          (pt->y * mat->step + pt->x * col));
                          return color;



                          Output:



                          1. Output from a grayscaled 600x600 Red.jpeg file
                          // Pixel (0,0)
                          b=8 g=2c, r=16 color=4c
                          cols=600, step=600, col=1, x=0, y=0 loc=0

                          // Pixel (1,0)
                          b=8 g=2c, r=16 color=4c
                          cols=600, step=600, col=1, x=1, y=0 loc=1

                          // Pixel (1,1)
                          b=8 g=2c, r=16 color=4c
                          cols=600, step=600, col=1, x=1, y=1 loc=601


                          2. Output from a 3 channel rgb 600x600 Red.jpeg file
                          // Pixel (0,0)
                          b=0 g=0, r=fe color=fe0000
                          cols=600, step=1800, col=3, x=0, y=0 loc=0

                          // Pixel (1,0)
                          b=0 g=0, r=fe color=fe0000
                          cols=600, step=1800, col=3, x=1, y=0 loc=3

                          // Pixel (1,1)
                          cols=600, step=1800, col=3, x=1, y=1 loc=1803
                          b=0 g=0, r=fe color=fe0000






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 22 at 5:22









                          Nigel HsiungNigel Hsiung

                          1




                          1





















                              -1














                              for accessing data using CvMat you have to use "img->data.ptr[x*col+y]" it can used to store data of uchar. CvMat also support double,float,string and integer type. So you can store data according to your convince.






                              share|improve this answer























                              • x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                                – Antonio
                                Feb 26 '15 at 10:41












                              • The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                                – Antonio
                                Feb 26 '15 at 10:51
















                              -1














                              for accessing data using CvMat you have to use "img->data.ptr[x*col+y]" it can used to store data of uchar. CvMat also support double,float,string and integer type. So you can store data according to your convince.






                              share|improve this answer























                              • x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                                – Antonio
                                Feb 26 '15 at 10:41












                              • The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                                – Antonio
                                Feb 26 '15 at 10:51














                              -1












                              -1








                              -1







                              for accessing data using CvMat you have to use "img->data.ptr[x*col+y]" it can used to store data of uchar. CvMat also support double,float,string and integer type. So you can store data according to your convince.






                              share|improve this answer













                              for accessing data using CvMat you have to use "img->data.ptr[x*col+y]" it can used to store data of uchar. CvMat also support double,float,string and integer type. So you can store data according to your convince.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 26 '15 at 10:18









                              Himanshu SharmaHimanshu Sharma

                              41




                              41












                              • x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                                – Antonio
                                Feb 26 '15 at 10:41












                              • The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                                – Antonio
                                Feb 26 '15 at 10:51


















                              • x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                                – Antonio
                                Feb 26 '15 at 10:41












                              • The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                                – Antonio
                                Feb 26 '15 at 10:51

















                              x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                              – Antonio
                              Feb 26 '15 at 10:41






                              x*col is unsafe as the distance in bytes between 2 adjacent pixels belonging to the same column ("stride" or "step") might be different from the number of columns (by the way, you should name the variable cols, not col). I do not know where to find CvMat documentation so I wouldn't know where the stride value is saved (in my answer there's an example for cv::Mat), maybe in the field step.

                              – Antonio
                              Feb 26 '15 at 10:41














                              The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                              – Antonio
                              Feb 26 '15 at 10:51






                              The conversion between CvMat and cv::Mat is so straightforward (and efficient, there's no memory copy) that I do not understand why you want to struggle with that and keep using obsoleted stuff. Anyway, if you really need documentation of CvMat you can find it here. Indeed the stride is in the step field. Note that stride != cols can be the case also for 8-bit grayscale images, e.g. because of choices about memory alignment.

                              – Antonio
                              Feb 26 '15 at 10:51


















                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28603693%2fhow-can-retrieve-pixel-value-using-cvmat%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                              155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해