Ask Your Question
0

Error showing values from an image in pixels with mouse callback

asked 2015-05-29 18:37:52 -0600

diegomez_86 gravatar image

updated 2015-05-30 10:41:06 -0600

Hello, I want to check pixel values with this program, however the results are not convincing at all for me. I am loading an image in grayscale and the colors are of course very different as it can be seen in the image

image description

However when I pass the mouse (with the implemented mouse callback function) I receive white colors with values of 90 for example, or black colors with values of 130.

/*Updated algorithm. Now it doesn't show the values. the window disspaears like with an exception when the mouse is passed through the image"

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

using namespace cv;
using namespace std;

Mat image, imageGreen;
char window_name[20]="Get coordinates";

static void onMouse( int event, int x, int y, int f, void* )
{       
    uchar intensity = imageGreen.at<uchar>(x,y);
cout << "x: "<< x << " y: " << y << endl << " value: " << (int)intensity << endl;
}

int main() {
    namedWindow( window_name, CV_WINDOW_NORMAL );
    image = imread("/home/diego/Humanoids/imageGreen0.png");
    Mat imageGreen = Mat::zeros(image.size(), CV_8UC1);
    Vec3b result;

    for (int i = 0; i < image.rows ; i++)
{
    for (int j = 0; j < image.cols ; j++)
    {
        result = image.at<cv::Vec3b>(i,j);
        int value = result[1];
        imageGreen.at<uchar>(i, j) = value;
    }
}

    cout<< "Image depth: "<<imageGreen.depth()<<" # of channels: "<<imageGreen.channels()<<"\n";
    imshow( window_name, imageGreen );
    setMouseCallback( window_name, onMouse, 0 );
    waitKey(0);
    return 0;
}

I want to learn how to use this because I want to get rid of the bottle shadow (by the way, here I put the green value of each pixel from an RGB image into the correspondent pixel in a grayscale image --> It's a green bottle) and I thought that maybe there can be a difference between the values of the bottle and the reflection from the sun which can allow me to make a threshold.

Can anyone help me telling me what is the error here? I am trying to stop being a newbie in OpenCV and C++, but I think that there is still a long way for it. Thank you

I changed the algorithm and now I'm using another image

image description

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-05-30 02:43:49 -0600

LBerger gravatar image

updated 2015-05-30 02:47:15 -0600

Your program is false. You don't have intensityin your file except for black and white image (luminance). You can have (generally) a colorB,G,R or R,G,B alpha for png file.... You must check depth of your image and type before give pixel value.... Something like this but you improve for black and white images....

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

using namespace cv;
using namespace std;

Mat image;
char window_name[20] = "Get coordinates";

static void onMouse(int event, int x, int y, int f, void*){



if (image.channels()==3)
{
Vec3b v= image.at<Vec3b>(y,x);

cout << "x: " << x << " y: " << y << " value: " << (int)v[0] << " " << (int)v[1] << " " << (int)v[2] << " "  << endl;
}
else if(image.channels()==4)
    {
    Vec4b v = image.at<Vec4b>(y, x);

    cout << "x: " << x << " y: " << y << " value: " << (int)v[0] << " " << (int)v[1] << " " << (int)v[2] << " " << (int)v[3] << endl;
    }

}

int main() {
    namedWindow(window_name, CV_WINDOW_NORMAL);
    image = imread("C:/Users/Public/Pictures/Sample Pictures/video.png",CV_LOAD_IMAGE_UNCHANGED);
    cout<<image.depth()<<" "<<image.channels()<<"\n";
    imshow(window_name, image);
    setMouseCallback(window_name, onMouse, 0);
    waitKey(0);
    return 0;
    }
edit flag offensive delete link more

Comments

Thanks for showing me, however I need the green value sent into a 1 channel Uchar mat. I tried the way you told me and it worked, however I want just one single value and use this procedure in order to find a way of getting rid of the bottle shadow (I am assuming that the bottle is green ). but now that I want to check at the value, the screen just disappears. I updated the new code into the question.

Thanks for what I just learned and sorry for the inconvenient.

diegomez_86 gravatar imagediegomez_86 ( 2015-05-30 10:27:46 -0600 )edit

It's not imageGreen.at<uchar>(x,y); but imageGreen.at<uchar>(y,x); you should use split (image,plan) instead of loop for. vector<mat> plan; May be you should try some example in opencv (opencv/sample/cpp)

LBerger gravatar imageLBerger ( 2015-05-30 11:49:03 -0600 )edit
0

answered 2015-06-01 03:28:55 -0600

diegomez_86 gravatar image

Hello and thanks to LBerger who made me take into account the row, columns thing. This is the final code:

 #include <iostream>
 #include <stdio.h>
 #include <opencv2/opencv.hpp>
 using namespace cv;
 using namespace std;

 Mat imageGreen;

 static void onMouse(int event, int x, int y, int f, void*){
cout <<(int)imageGreen.at<unsigned char>(y, x) << endl;
 }

 int main(int argc, char** argv) {
Mat inputImage = imread("/home/diego/Humanoids/imageGreen0.png");
imageGreen = Mat::zeros(inputImage.size(), CV_8UC1);

   //Getting the green channel value and putting it into a one channel value.

Vec3b result;

for (int i = 0; i < inputImage.rows ; i++)
{
    for (int j = 0; j < inputImage.cols ; j++)
    {
        result = inputImage.at<cv::Vec3b>(i,j);
        int value = result[1];
        imageGreen.at<uchar>(i, j) = value;
    }
}

cout <<imageGreen.channels()<<endl;

namedWindow( "imageGreen", CV_WINDOW_NORMAL);
imshow("imageGreen", imageGreen);
setMouseCallback("imageGreen", onMouse, 0);
waitKey(0);
return 0;

}

edit flag offensive delete link more

Comments

vector<mat> plan;

split(inputImage,plan);

and plan[1] must be equivalent to imageGreen (if inputimage is CV_8UC3 or CV_8UC4). This two lines can replace your loops

LBerger gravatar imageLBerger ( 2015-06-01 04:15:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-05-29 18:37:52 -0600

Seen: 1,274 times

Last updated: Jun 01 '15