Home>
Cannot get the expected value (0-255) from grayscale image

My name is Baum, and I'm too excited about OpenCV and the program.

OpenCV execution environment is as follows.

  • OpenCV 3.4.8

I want to get the pixel value of the specified row in the following grayscale image (640x480).

I tried changing the

type specification in several ways, but when I check the acquired value,expected value (0 to 255)is thrown into the array did not.

If i find something wrong, please let me know.

Below is the source code to get the value of the 240th line of the above image.
Some are omitted for easy viewing.

unsigned int tget_line [640];// Information for each pixel of the specified line (expects 0 to 255 to be stored)
int main (int argh, char * argv [])
{
        Mat gray = imread ("bin.png", IMREAD_COLOR);// Assign the prepared grayscale image to Mat gray
        int x;
        int y = 240;// Specify the line (specify the 240th center line of h = 480)
        for (x = 0;x<640;++ x)
        {
            tget_line [x] = gray.at<int>(y, x);
            printf ("% d, \ n,", tget_line [x]);
        }
        return 0;
}

The output tget_line [] value is as follows.
The black part has a positive value, and the white part has a negative value. These values ​​are the results that are required in 0 to 255 levels.

-1717986919,
-1717986919,
-1717986919,
-1701143910,
-1717986662,
-1701143911,
-1734829928,
-1717987176,
-1717986919,
-1717986919,
-1717986919,
-1701143911,
-1717921126,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1701209703,
-1717986662,
-1701143911,
-1717921126,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1701143911,
-1717921126,
-1717986919,
-1701143911,
-1717921126,
-1717986919,
-1717986919,
-1701209703,
-1701143910,
-1717986918,
-1717986919,
-1701144167,
-1717986918,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1701143911,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1734829927,
-1718052712,
-1717986919,
-1717986919,
-1717986919,
-1734829671,
-1734829928,
-1717986919,
-1717986919,
-1717986919,
-1734829928,
-1717987176,
-1717986919,
-1701209703,
-1717986662,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1734829927,
-1718052712,
-1717986919,
-1701143911,
-1717921126,
-1701144167,
-1717986918,
-1701209703,
-1717986662,
-1717986919,
-1734829928,
-1701144424,
-1701143910,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1734829927,
-1718052712,
-1717986919,
-1717986919,
-1734764135,
-1717987176,
-1734829927,
-1717986919,
-1734829671,
-1734829928,
-1717986919,
-1717986919,
-1734829927,
-1718052712,
-1717986919,
-1717986919,
-1734829928,
-1717987176,
-1717986919,
-1717986919,
-1734829671,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1751672936,
-1751672937,
-1734830185,
-1734829928,
-1734829928,
-1717987176,
-1717986919,
-1734829928,
-1734829928,
-1751672936,
-1734895721,
-1734829928,
-1751672936,-1751672937,
-1734830185,
-1751672936,
-1751672937,
-1751672937,
-1734829929,
-1751672937,
-1751672937,
-1751672937,
-1734895721,
-1751672680,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1768450153,
-1751673194,
-1751672937,
-1751672937,
-1751672937,
-1768515945,
-1768515946,
-1768515946,
-1768515946,
-1768515946,
-1768515946,
-1768515946,
-1751672937,
-1751672937,
-1734829929,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1768515945,
-1751738730,
-1751672937,
-1751672937,
-1751738730,
-1734830185,
-1751672936,
-1768515946,
-1751673194,
-1734829929,
-1768450153,
-1768515946,
-1768515946,
-1785293162,
-1819044459,
-1869573997,
1334282119,
471617359,
471604252,
505092891,
488447518,
471604253,
471538459,
471604252,
488447260,
438049820,
437918234,
437918234,
437918234,
421075482,
437918233,
421075225,
437917977,
421075226,
437852441,
421075482,
437918233,
421141018,
421075225,
421075225,
421075225,
421075225,
421075225,
404298009,
421074968,
404232217,
404232216,
387389464,
387389207,
404232216,
404232216,
387389208,
387455000,
387389207,
404232215,
387389207,
387389207,
370546199,
353768982,
387388693,
387389207,
353768982,
353703189,
370546197,
353768982,
353703189,
353703189,
404100630,
387389464,
370546199,
353703189,
353703189,
353703189,
336925973,
370545684,
387389206,
387323414,
353703703,
353703189,
353703189,
353703189,
353703189,
353703189,
336860437,
336860180,
353703189,
353703189,
353703189,
353703189,
353703189,
353703189,
353637396,
353703189,
353703189,
353703189,
353703189,
370546197,
353768982,
353703189,
353703189,
370414612,
370546198,
353703190,
370546198,
370546198,
370546198,
370480405,
370546198,
387389206,
387389207,
370546455,
370546198,
420943639,
320018713,
1482184723,
-1819308145,
-1802202221,
-1785358956,
-1768515946,
-1751673194,
-1751672937,
-1768515946,
-1768515946,
-1768515946,
-1768515946,
-1768515946,
-1751672938,
-1768450153,
-1768515946,
-1768515946,-1751672937,
-1751672937,
-1768515945,
-1751672937,
-1768515689,
-1751672938,
-1751672937,
-1751672937,
-1751672937,
-1768450153,
-1768515946,
-1768515946,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1734830185,
-1734829928,
-1734895721,
-1751672680,
-1751672937,
-1734895721,
-1734829928,
-1751672936,
-1734895721,
-1734829928,
-1751672936,
-1734895721,
-1751672680,
-1751672937,
-1734895721,
-1734829928,
-1734829928,
-1751672937,
-1734830185,
-1734829928,
-1734829928,
-1717987176,
-1717986919,
-1734829928,
-1734829928,
-1734829928,
-1701143910,
-1734829414,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1717986919,
-1734829671,
-1734829928,
-1717986919,
-1717986919,
-1717986919,
-1734829928,
-1717987176,
-1717986919,
-1717986919,
-1717986919,
-1701143911,
-1734764135,
-1717987176,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1734829927,
-1718052712,
-1701144167,
-1701143910,
-1717921126,
-1717986919,
-1717986919,
-1701143910,
-1701143910,
-1717986918,
-1717986919,
-1701144167,
-1701143910,
-1701143910,
-1717986662,
-1717986919,
-1701209703,
-1717986662,
-1701143911,
-1717921126,
-1717986919,
-1717986919,
-1701143910,
-1717986662,
-1701143911,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701078117,
-1701143910,
-1701143910,
-1684366694,
-1701143653,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1684300901,
-1701143653,
-1701143910,
-1701143910,
-1701143910,
-1684300902,
-1684300901,
-1701143653,
-1684300902,
-1684300901,
-1701143653,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701143910,
-1701078117,
-1701143910,
-1701143910,
-1684300901,
-1684300901,
-1701143909,
-1684300901,
-1684300901,
-1701143909,
-1684366694,
-1684300901,
-1684300901,
-1701078117,
-1701143910,
-1684300902,
-1684300901,
-1701143653,
-1701143910,-1684366694,
-1701143653,
-1684300902,
-1734764135,
-1734829928,
-1717986920,
-1717986919,
-1701144167,
-1701143910,
-1734829928,
-1717987176,
-1717986919,
-1701143910,
-1717986662,
-1701143911,
-1701143910,
-1717986662,
-1734829927,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1701209703,
-1701143910,
-1717986918,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1701143910,
-1717986662,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1701143911,
-1734764135,
-1734829928,
-1701143912,
-1717986919,
-1701144167,
-1701143910,
-1717986919,
-1717986919,
-1717986919,
-1734829928,
-1734829928,
-1734829928,
-1718052712,
-1717986919,
-1717986919,
-1717986919,
-1734829671,
-1717986920,
-1701143910,
-1701143910,
-1717986918,
-1734829928,
-1734829928,
-1717986920,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717921126,
-1717986919,
-1734829927,
-1734829928,
-1734829928,
-1717986920,
-1718052712,
-1717986919,
-1717986919,
-1701143910,
-1717986662,
-1717986919,
-1734764135,
-1734829928,
-1734829928,
-1734764135,
-1717987176,
-1701143911,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1717986919,
-1734829927,
-1734829928,
-1717987176,
-1717986919,
-1734764135,
-1717987176,
-1734829927,
-1718052712,
-1717986919,
-1717986919,
-1718052712,
-1717986919,
-1734829927,
-1734829928,
-1717987176,
-1717986919,
-1734829928,
-1734829928,
-1734829928,
-1734764135,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1734829928,
-1751672680,
-1751672937,
-1734895721,
-1734829928,
-1751672936,
-1751607144,
-1734830185,
-1734829928,
-1734895721,
-1734829928,
-1751672936,
-1734895721,
-1734829928,
-1751672936,
-1751672937,
-1734830185,
-1768515944,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1751672937,
-1768515945,
-1768450153,
-1768515946,
-1751672938,
-1768450153,
-1768515946,
-1768515946,
-1751738730,

This grayscale image seems to be unrelated to the question, but this grayscale image was converted to grayscale using the Mat frame image (YUV422) imported from the Web camera with the cvtColor function as the third argument CV_BGR2GRAY It is an image saved in Mat gray later.

cvtColor (frame, gray, CV_BGR2GRAY);
  • Answer # 1

    If it is a grayscale image, specify IMREAD_GRAYSCALE and read it as 8bit1ch.

    In addition,tget_line [x] = gray.at(y, x)is replaced bygray.at(y, x)?


    At present, images are read as 8bit3ch.
    In that case, the pixel data of a row is
    [B0] [G0] [R0] [B1] [G1] [R1] [B2] [G2] [R2] ...
    (Please think that one [] is 1 byte).
    1 pixel information is 3 bytes.
    I'm trying to read this as an int (forcing the int size to be 4 bytes)
    [B0] [G0] [R0] [B1] [G1] [R1] [B2] [G2] [R2] [B3] [G3] [R3] ...
    In this form, the memory contents every 4 bytes are interpreted as ints.

Related articles