1

Consider:

int cn = img_V.channels();
Scalar_<unit8_t> bgrPixel;

for (int i = 0; i < img_V.rows; i++)
{
    uint8_t* rowPtr = img_V.row(i);
    for (int j = 0; j < img_V.cols; j++)
    {
        bgrPixel.val[0] = rowPtr[j*cn + 0]; // B
        bgrPixel.val[1] = rowPtr[j*cn + 1]; // G
        bgrPixel.val[2] = rowPtr[j*cn + 2]; // R

        // Do something with BGR values...
    }
}

I am using Visual Studio and OpenCV, but it can't identify the uint8_t showing error. How can I fix this?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Rappy Saha
  • 13
  • 6
  • Possible duplicate of [unknown type name 'uint8\_t', MinGW](http://stackoverflow.com/questions/8953274/unknown-type-name-uint8-t-mingw) – zeromus May 04 '16 at 13:26
  • Why in the opposite order, *BGR*? Why not the standard order, [RGB](https://en.wikipedia.org/wiki/RGB_color_model)? – Peter Mortensen Apr 26 '21 at 13:58
  • @PeterMortensen: That’s one of the many stupid things OpenCV does. It seems like an easy choice, storing RGB values in the right order, but no... :) – Cris Luengo Apr 26 '21 at 14:22

2 Answers2

3

You need to add

#include <cstdint>

to see uint8_t.

Please note, however, in OpenCV you should use uchar.


Also please note that you're not looping correctly. A correct approach would be:

for (int i = 0; i < img_V.rows; i++)
{
    Vec3b* rowPtr = img_V.ptr<Vec3b>(i);
    for (int j = 0; j < img_V.cols; j++)
    {
        Vec3b& bgrPixel = rowPtr[j];

        // Do something with BGR values...
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Miki
  • 40,887
  • 13
  • 123
  • 202
  • thanks for the suggestion of the library. :D but if I want to use your loop system then how can I display the pixel value? :) – Rappy Saha May 05 '16 at 04:11
  • `bgrPixel` will contain the values for each pixel. To display the value, you can just `cout << bgrPixel;` , or to access each channel: `cout << "B:" << int(bgrPixel[0]) << " G: " << int(bgrPixel[1]) << " R: " << int(bgrPixel[2]);` – Miki May 05 '16 at 09:40
  • Thanks a lot. This is what I wanted :) – Rappy Saha May 10 '16 at 06:19
0

Since you are probably dealing with an image, I imagine that img_V is a cv::Mat. According to the most recent documentation, cv::Mat::row(int y) returns another cv::Mat. But there isn't any default conversion to uint8_t or uchar.

However, if you want to get a pointer to a specific row of the image, you should use cv::Mat::ptr(int i0), which returns a uchar *, or any other type pointer if you use its template version.

But you want a uint8_t pointer, you first need to do as Miki says, and #include <csdtint>.

Bernardo Reis
  • 49
  • 1
  • 5