interactiveColorDetect.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "opencv2/opencv.hpp"
  2. #include <iostream>
  3. using namespace cv;
  4. using namespace std;
  5. //Global Variables
  6. Mat img, placeholder;
  7. // Callback function for any event on he mouse
  8. void onMouse( int event, int x, int y, int flags, void* userdata )
  9. {
  10. if( event == EVENT_MOUSEMOVE )
  11. {
  12. Vec3b bgrPixel(img.at<Vec3b>(y, x));
  13. Mat3b hsv,ycb,lab;
  14. // Create Mat object from vector since cvtColor accepts a Mat object
  15. Mat3b bgr (bgrPixel);
  16. //Convert the single pixel BGR Mat to other formats
  17. cvtColor(bgr, ycb, COLOR_BGR2YCrCb);
  18. cvtColor(bgr, hsv, COLOR_BGR2HSV);
  19. cvtColor(bgr, lab, COLOR_BGR2Lab);
  20. //Get back the vector from Mat
  21. Vec3b hsvPixel(hsv.at<Vec3b>(0,0));
  22. Vec3b ycbPixel(ycb.at<Vec3b>(0,0));
  23. Vec3b labPixel(lab.at<Vec3b>(0,0));
  24. // Create an empty placeholder for displaying the values
  25. placeholder = Mat::zeros(img.rows,400,CV_8UC3);
  26. //fill the placeholder with the values of color spaces
  27. putText(placeholder, format("BGR [%d, %d, %d]",bgrPixel[0],bgrPixel[1],bgrPixel[2]), Point(20, 70), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
  28. putText(placeholder, format("HSV [%d, %d, %d]",hsvPixel[0],hsvPixel[1],hsvPixel[2]), Point(20, 140), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
  29. putText(placeholder, format("YCrCb [%d, %d, %d]",ycbPixel[0],ycbPixel[1],ycbPixel[2]), Point(20, 210), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
  30. putText(placeholder, format("LAB [%d, %d, %d]",labPixel[0],labPixel[1],labPixel[2]), Point(20, 280), FONT_HERSHEY_COMPLEX, .9, Scalar(255,255,255), 1);
  31. Size sz1 = img.size();
  32. Size sz2 = placeholder.size();
  33. //Combine the two results to show side by side in a single image
  34. Mat combinedResult(sz1.height, sz1.width+sz2.width, CV_8UC3);
  35. Mat left(combinedResult, Rect(0, 0, sz1.width, sz1.height));
  36. img.copyTo(left);
  37. Mat right(combinedResult, Rect(sz1.width, 0, sz2.width, sz2.height));
  38. placeholder.copyTo(right);
  39. imshow("PRESS P for Previous, N for Next Image", combinedResult);
  40. }
  41. }
  42. int main( int argc, const char** argv )
  43. {
  44. // filename
  45. // Read the input image
  46. int image_number = 0;
  47. int nImages = 10;
  48. if(argc > 1)
  49. nImages = atoi(argv[1]);
  50. char filename[20];
  51. sprintf(filename,"images/rub%02d.jpg",image_number%nImages);
  52. img = imread(filename);
  53. // Resize the image to 400x400
  54. Size rsize(400,400);
  55. resize(img,img,rsize);
  56. if(img.empty())
  57. {
  58. return -1;
  59. }
  60. // Create an empty window
  61. namedWindow("PRESS P for Previous, N for Next Image", WINDOW_AUTOSIZE);
  62. // Create a callback function for any event on the mouse
  63. setMouseCallback( "PRESS P for Previous, N for Next Image", onMouse );
  64. imshow( "PRESS P for Previous, N for Next Image", img );
  65. while(1)
  66. {
  67. char k = waitKey(1) & 0xFF;
  68. if (k == 27)
  69. break;
  70. //Check next image in the folder
  71. if (k =='n')
  72. {
  73. image_number++;
  74. sprintf(filename,"images/rub%02d.jpg",image_number%nImages);
  75. img = imread(filename);
  76. resize(img,img,rsize);
  77. }
  78. //Check previous image in he folder
  79. else if (k =='p')
  80. {
  81. image_number--;
  82. sprintf(filename,"images/rub%02d.jpg",image_number%nImages);
  83. img = imread(filename);
  84. resize(img,img,rsize);
  85. }
  86. }
  87. return 0;
  88. }