4

I have below code. When I run the program, unknown characters instead of pixel values comes to the screen. I want to display pixel values. How do I do this? Thank you.

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat image = imread("/home/fd/baby.jpg");
    for( int i = 0 ; i < image.rows ; i++)
    {
        for( int j = 0 ; j < image.cols ; j++ )
        {
            if(image.type() == CV_8UC1)
            {
                image.at<uchar>(i,j) = 255;
            }
            else if(image.type() == CV_8UC3)
            {
                cout << image.at<Vec3b>(i,j)[0] << " " << image.at<Vec3b>(i,j)[1] << " " << image.at<Vec3b>(i,j)[2] << endl;

                image.at<Vec3b>(i,j)[0] = 255;
                image.at<Vec3b>(i,j)[1] = 255;
                image.at<Vec3b>(i,j)[2] = 255;

                cout << image.at<Vec3b>(i,j)[0] << " " << image.at<Vec3b>(i,j)[1] << " " << image.at<Vec3b>(i,j)[2] << endl;
            }
            else
            {
                cout << "Anknown image format" << endl;
                return 0;
            }
        }
    }
    imshow("Result İmage", image);
    waitKey(0);
}

This is the result screen:

enter image description here

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
fdas
  • 113
  • 1
  • 1
  • 8
  • 3
    possible duplicate of [Why is std::cout not printing the correct value for my int8\_t number?](http://stackoverflow.com/questions/7587782/why-is-stdcout-not-printing-the-correct-value-for-my-int8-t-number) – Alan Stokes Apr 25 '15 at 13:24
  • But you know picture of pixels have values between 0 and 255. So I think my problem is not signed or unsigned pixel value. – fdas Apr 25 '15 at 13:43
  • 1
    `cout << int(uchar_value);` but better even, just print the whole Mat: `cout << img << endl;` – berak Apr 25 '15 at 13:56
  • 2
    It's not about signed or unsigned. Any flavour of `char` is printed as a single character not a number by default. Promoting to a bigger type prevents this. (If `cout << 'A'` printed 65 it would cause confusion. Blame backward compatibility.) – Alan Stokes Apr 25 '15 at 14:23
  • 1
    Thank you @berak, I solved my problem wity your answer. Thank you. cout << int(image.at(i,j)[0]) – fdas Apr 25 '15 at 15:02
  • possible duplicate of [Why "cout" works weird for "unsigned char"?](http://stackoverflow.com/questions/21374773/why-cout-works-weird-for-unsigned-char) – herohuyongtao Apr 26 '15 at 02:06
  • @AlanStokes `Vec3b` is `Vec`, not `Vec`. So it's not a duplicate to what you link. :) – herohuyongtao Apr 26 '15 at 02:08
  • why are you assigning 255 each time? – roschach Feb 16 '19 at 17:53

4 Answers4

5

Cast each of the outputs to an integer

<< image.at<Vec3b>(i,j)[0] ...

change to

<< (int)image.at<Vec3b>(i,j)[0] ...

You are printing a char (or probably unsigned char) which is printed through the stream as a single character (which at 255 looks like what you see). Cast to int forces it to display the numerical representation of the value.

The other answers changing the image.at<type> changes how the raw data is interpreted; don't do that. They must be interpreted properly.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
0

Change the

image.at<Vec3b>(i, j)

to

image.at<int>(i, j)

or

image.at<double>(i, j)

or

image.at<float>(i, j)

to print the values instead of characters

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
0

Vec3b px= image.at(x,y);

cout << "value: ("<<(int)px.val[0]<<", "<<(int)px.val[1]<<", "<<(int)px.val[2]<<")" << endl;

This works form me.

user3476405
  • 26
  • 1
  • 2
-2

You are displaying characters try converting using function image.at<int>(j,i);

Avijit Dasgupta
  • 2,055
  • 3
  • 22
  • 36