utils.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3. using namespace cv;
  4. using namespace std;
  5. void show(Mat src, const char wd[], float i = 0.3) {
  6. int maxs = src.cols > src.rows ? src.cols : src.rows;
  7. float ra = 400 / (maxs + DBL_MIN);
  8. i = ra;
  9. //------չʾͼÏñ²Ù×÷------
  10. namedWindow(wd, WINDOW_FREERATIO);
  11. if (i > 100)
  12. {
  13. resizeWindow(wd, i, i);//´°¿Ú³ß´çµ÷½Ú
  14. }
  15. else
  16. {
  17. resizeWindow(wd, int(i * src.cols), int(i * src.rows));//´°¿Ú³ß´çµ÷½Ú
  18. }
  19. Mat src1 = src.clone();
  20. src1.convertTo(src1, 5);
  21. normalize(src1, src1, 0, 1, NORM_MINMAX);
  22. imshow(wd, src1);
  23. waitKey(0);
  24. }
  25. int applyMask(unsigned short* input, unsigned char* pmask, int Width, int Height, unsigned short* output)
  26. {
  27. if (input == nullptr)
  28. {
  29. return 0;
  30. }
  31. if (pmask == nullptr)
  32. {
  33. return 0;
  34. }
  35. Mat src(Size(Width, Height), CV_16U, input);
  36. Mat mask(Size(Width, Height), CV_8U, pmask);
  37. Mat result;
  38. bitwise_and(src, src, result, mask);
  39. if (result.empty() || result.rows != Height || result.cols != Width )
  40. {
  41. return 0;
  42. }
  43. memcpy(output, result.data, Width * Height * sizeof(unsigned short));
  44. return 1;
  45. }
  46. int getMaskRect(unsigned char* pmask, int Width, int Height, int &x,int &y,int &RectWidth,int &RectHeight)
  47. {
  48. if (pmask == nullptr)
  49. {
  50. return 0;
  51. }
  52. Mat mask(Size(Width, Height), CV_8U, pmask);
  53. if (!sum(mask)[0])
  54. {
  55. return 0;
  56. }
  57. cv::Mat nonZeroPoints;
  58. cv::findNonZero(mask, nonZeroPoints);
  59. cv::Rect rect = cv::boundingRect(nonZeroPoints);
  60. x = rect.x;
  61. y = rect.y;
  62. RectWidth = rect.width;
  63. RectHeight = rect.height;
  64. return 1;
  65. }
  66. int cropImg(unsigned short *input, unsigned short *output, int Width, int Height, int x, int y, int RectWidth, int RectHeight)
  67. {
  68. if (input == nullptr)
  69. {
  70. return 0;
  71. }
  72. if (output == nullptr)
  73. {
  74. return 0;
  75. }
  76. Mat src(Size(Width, Height), CV_16U, input);
  77. memcpy(output, src(Rect(x, y, RectWidth, RectHeight)).clone().data, RectWidth * RectHeight * sizeof(unsigned short));
  78. return 1;
  79. }
  80. int main()
  81. {
  82. Mat input = imread(R"(Image_FinalImage.tif)",-1);
  83. Mat mask = imread(R"(mask.tif)", -1);
  84. int x = 0;
  85. int y = 0;
  86. int width = 0;
  87. int height = 0;
  88. getMaskRect(mask.ptr<uchar>(), input.cols, input.rows, x, y, width, height);
  89. unsigned short* output = new unsigned short[width * height];
  90. cropImg(input.ptr<ushort>(), output,input.cols,input.rows,x,y,width,height);
  91. Mat result(Size(width, height), CV_16U, output);
  92. show(result, "result");
  93. return 0;
  94. }