Skip to content

Instantly share code, notes, and snippets.

@nikhil9
Last active August 20, 2017 05:37
Show Gist options
  • Save nikhil9/4955915 to your computer and use it in GitHub Desktop.
Save nikhil9/4955915 to your computer and use it in GitHub Desktop.
Get RGB color space values of image pixel on mouse hover using JavaCV.
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_highgui.*;
public class getRGB{
public static int x_co;
public static int y_co;
public static void main (String[] args){
final IplImage src = cvLoadImage("resources/test.png"); //Images 'test.png' located under resource folder
cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
CvMouseCallback on_mouse = new CvMouseCallback() {
@Override
public void call(int event, int x, int y, int flags,
com.googlecode.javacpp.Pointer param) {
if (event == CV_EVENT_MOUSEMOVE){
x_co = x;
y_co = y;
}
CvScalar s=cvGet2D(src,y_co,x_co);
System.out.println( "B:"+ s.val(0) + " G:" + s.val(1) + " R:" + s.val(2));//Print values
}
};
cvSetMouseCallback("Image", on_mouse, null);
cvShowImage("Image", src);
cvWaitKey(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment