Haris
Haris

Reputation: 14053

OpenCV Access Pixel Value Using Mouse

Can any one tell what wrong with the below code. I am getting segmentation fault while mouse moving last portion of the image. I am just printing R,G,B value according to the mouse position.

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

using namespace cv;
using namespace std;
 Mat image;
 char window_name[20]="Pixel Value Demo";

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

 Vec3b pix=image.at<Vec3b>(x,y);
 int B=pix.val[0];
 int G=pix.val[1];
 int R=pix.val[2];
 cout<<R<<endl<<G<<endl<<B<<endl;

}



int main( int argc, char** argv )
{
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );
  image = imread( "src.jpg");
  imshow( window_name, image );    
  setMouseCallback( window_name, onMouse, 0 );
  waitKey(0);
  return 0;
}

Thanks in advance......

Upvotes: 0

Views: 3353

Answers (1)

berak
berak

Reputation: 39816

Vec3b pix=image.at<Vec3b>(x,y);

should be :

Vec3b pix=image.at<Vec3b>(y,x);  // row,col !!

Upvotes: 2

Related Questions