You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.h 2.0 kB

7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Created by Jack Yu on 04/04/2017.
  3. //
  4. #include <opencv2/opencv.hpp>
  5. namespace util{
  6. template <class T> void swap ( T& a, T& b )
  7. {
  8. T c(a); a=b; b=c;
  9. }
  10. template <class T> T min(T& a,T& b )
  11. {
  12. return a>b?b:a;
  13. }
  14. cv::Mat cropFromImage(const cv::Mat &image,cv::Rect rect){
  15. int w = image.cols-1;
  16. int h = image.rows-1;
  17. rect.x = std::max(rect.x,0);
  18. rect.y = std::max(rect.y,0);
  19. rect.height = std::min(rect.height,h-rect.y);
  20. rect.width = std::min(rect.width,w-rect.x);
  21. cv::Mat temp(rect.size(), image.type());
  22. cv::Mat cropped;
  23. temp = image(rect);
  24. temp.copyTo(cropped);
  25. return cropped;
  26. }
  27. cv::Mat cropBox2dFromImage(const cv::Mat &image,cv::RotatedRect rect)
  28. {
  29. cv::Mat M, rotated, cropped;
  30. float angle = rect.angle;
  31. cv::Size rect_size(rect.size.width,rect.size.height);
  32. if (rect.angle < -45.) {
  33. angle += 90.0;
  34. swap(rect_size.width, rect_size.height);
  35. }
  36. M = cv::getRotationMatrix2D(rect.center, angle, 1.0);
  37. cv::warpAffine(image, rotated, M, image.size(), cv::INTER_CUBIC);
  38. cv::getRectSubPix(rotated, rect_size, rect.center, cropped);
  39. return cropped;
  40. }
  41. cv::Mat calcHist(const cv::Mat &image)
  42. {
  43. cv::Mat hsv;
  44. std::vector<cv::Mat> hsv_planes;
  45. cv::cvtColor(image,hsv,cv::COLOR_BGR2HSV);
  46. cv::split(hsv,hsv_planes);
  47. cv::Mat hist;
  48. int histSize = 256;
  49. float range[] = {0,255};
  50. const float* histRange = {range};
  51. cv::calcHist( &hsv_planes[0], 1, 0, cv::Mat(), hist, 1, &histSize, &histRange,true, true);
  52. return hist;
  53. }
  54. float computeSimilir(const cv::Mat &A,const cv::Mat &B)
  55. {
  56. cv::Mat histA,histB;
  57. histA = calcHist(A);
  58. histB = calcHist(B);
  59. return cv::compareHist(histA,histB,CV_COMP_CORREL);
  60. }
  61. }//namespace util