fdas
fdas

Reputation: 143

OpenCV Display Pixels Value

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

Upvotes: 4

Views: 11246

Answers (4)

user3476405
user3476405

Reputation: 26

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.

Upvotes: 0

kmdreko
kmdreko

Reputation: 59952

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.

Upvotes: 5

Santhosh Nagasanthosh
Santhosh Nagasanthosh

Reputation: 23

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

Upvotes: -2

Santhosh Nagasanthosh
Santhosh Nagasanthosh

Reputation: 23

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

Upvotes: 0

Related Questions