falsecolor.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/imgcodecs.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include <iostream>
  5. using namespace cv;
  6. using namespace std;
  7. enum MyShape{MyCIRCLE=0,MyRECTANGLE,MyELLIPSE};
  8. struct ParamColorMap {
  9. int iColormap;
  10. Mat img;
  11. };
  12. String winName="False color";
  13. static const String ColorMaps[] = { "Autumn", "Bone", "Jet", "Winter", "Rainbow", "Ocean", "Summer", "Spring",
  14. "Cool", "HSV", "Pink", "Hot", "Parula", "Magma", "Inferno", "Plasma", "Viridis",
  15. "Cividis", "Twilight", "Twilight Shifted", "Turbo", "Deep Green", "User defined (random)" };
  16. static void TrackColorMap(int x, void *r)
  17. {
  18. ParamColorMap *p = (ParamColorMap*)r;
  19. Mat dst;
  20. p->iColormap= x;
  21. if (x == COLORMAP_DEEPGREEN + 1)
  22. {
  23. Mat lutRND(256, 1, CV_8UC3);
  24. randu(lutRND, Scalar(0, 0, 0), Scalar(255, 255, 255));
  25. applyColorMap(p->img, dst, lutRND);
  26. }
  27. else
  28. applyColorMap(p->img,dst,p->iColormap);
  29. putText(dst, "Colormap : "+ColorMaps[p->iColormap], Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(255, 255, 255),2);
  30. imshow(winName, dst);
  31. }
  32. static Mat DrawMyImage(int thickness,int nbShape)
  33. {
  34. Mat img=Mat::zeros(500,256*thickness+100,CV_8UC1);
  35. int offsetx = 50, offsety = 25;
  36. int lineLength = 50;
  37. for (int i=0;i<256;i++)
  38. line(img,Point(thickness*i+ offsetx, offsety),Point(thickness*i+ offsetx, offsety+ lineLength),Scalar(i), thickness);
  39. RNG r;
  40. Point center;
  41. int radius;
  42. int width,height;
  43. int angle;
  44. Rect rc;
  45. for (int i=1;i<=nbShape;i++)
  46. {
  47. int typeShape = r.uniform(MyCIRCLE, MyELLIPSE+1);
  48. switch (typeShape) {
  49. case MyCIRCLE:
  50. center = Point(r.uniform(offsetx,img.cols- offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
  51. radius = r.uniform(1, min(offsetx, offsety));
  52. circle(img,center,radius,Scalar(i),-1);
  53. break;
  54. case MyRECTANGLE:
  55. center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
  56. width = r.uniform(1, min(offsetx, offsety));
  57. height = r.uniform(1, min(offsetx, offsety));
  58. rc = Rect(center-Point(width ,height )/2, center + Point(width , height )/2);
  59. rectangle(img,rc, Scalar(i), -1);
  60. break;
  61. case MyELLIPSE:
  62. center = Point(r.uniform(offsetx, img.cols - offsetx), r.uniform(offsety + lineLength, img.rows - offsety));
  63. width = r.uniform(1, min(offsetx, offsety));
  64. height = r.uniform(1, min(offsetx, offsety));
  65. angle = r.uniform(0, 180);
  66. ellipse(img, center,Size(width/2,height/2),angle,0,360, Scalar(i), -1);
  67. break;
  68. }
  69. }
  70. return img;
  71. }
  72. int main(int argc, char** argv)
  73. {
  74. cout << "This program demonstrates the use of applyColorMap function.\n\n";
  75. ParamColorMap p;
  76. Mat img;
  77. if (argc > 1)
  78. img = imread(samples::findFile(argv[1]), IMREAD_GRAYSCALE);
  79. else
  80. img = DrawMyImage(2,256);
  81. p.img=img;
  82. p.iColormap=0;
  83. imshow("Gray image",img);
  84. namedWindow(winName);
  85. createTrackbar("colormap", winName, NULL, COLORMAP_DEEPGREEN + 1, TrackColorMap, (void*)&p);
  86. setTrackbarMin("colormap", winName, COLORMAP_AUTUMN);
  87. setTrackbarMax("colormap", winName, COLORMAP_DEEPGREEN + 1);
  88. setTrackbarPos("colormap", winName, COLORMAP_AUTUMN);
  89. TrackColorMap(0, (void*)&p);
  90. cout << "Press a key to exit" << endl;
  91. waitKey(0);
  92. return 0;
  93. }