ImageFusion.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include <opencv2/opencv.hpp>
  3. #include "OptimizeCal.h"
  4. #include "MfcLabel.h"
  5. #include "FusionAndTailor.cuh"
  6. #include <queue>
  7. const int OVERLAP_WIDEN = 20;
  8. const int FUSION_WINDOW_SIZE = 5;
  9. #define CATCH_OPENCV_ERROR(Func) \
  10. try \
  11. { \
  12. Func; \
  13. } \
  14. catch (cv::Exception& e) \
  15. { \
  16. std::cout << e.what() << std::endl; \
  17. } \
  18. typedef std::vector<cv::Mat> vMat;
  19. /// <summary>
  20. /// the class is used to fuse the image
  21. /// </summary>
  22. class ImageFusion
  23. {
  24. public:
  25. ImageFusion();
  26. //********************************laplace fusion function**************************
  27. //set a function to initialize the laplace ImageFusion
  28. void Init(unsigned char * pImage, int nPanoWidth, int nPanoHeight,int nPanoPitch);
  29. void Init_Gpu(unsigned char* pImage, int nPanoWidth, int nPanoHeight, int nPanoPitch);
  30. //set a function to get the Image laplace Pyramid
  31. void GetLaplacePyramid(cv::Mat& Image, std::vector<cv::Mat>& vLaplacePyramid,int nLayer,int nSize,float Sigma);
  32. void GetDogPyramid(cv::Mat& Mask, std::vector<cv::Mat>& DogPyr,int nLevel);
  33. //set a function to get the Image gauss Pyramid
  34. void GetGaussPyramid(cv::Mat& Image, std::vector<cv::Mat>& vGaussPyramid, int nLayer, int nSize, float Sigma);
  35. //set a funcion to cal the blend image
  36. void CalBlendImage(vMat& vInferPy, vMat& vTargetPy, vMat& vMaskPy, vMat& vResultPy);
  37. //the function to use laplace pyramid to fuse the image
  38. void FusionImageByLaplacePyramid(cv::Mat* pLeft,cv::Mat* Right,int nLayer,int nSize,float Sigma);
  39. //cv::Mat Pano
  40. void FusionImageByLaplacePyramid(cv::Mat* pPano,cv::Rect InferRc ,cv::Mat* pCvTarget, int nLayer, int nSize, float Sigma);
  41. //unsigned char* Pano
  42. void FusionImageByLaplacePyramid(unsigned char* pPano, cv::Rect InferRc,unsigned char* pCvTarget,cv::Size SzTargetMat, int nLayer, int nSize, float Sigma, cv::Point LeftTop);
  43. //get result
  44. void GetLaplaceFusionImage(unsigned char* pFusionImage, int& nWidth, int& nHeight, int& nPitch);
  45. /***************************Thin Rect Window Slide Mean Fusion***********************************************************/
  46. //define a function to fusion image by mean image
  47. void FusionImageByThinRectWindowSlideMean(unsigned char* pPano, cv::Rect InferRc, int nSize, int nType = 0, float Sigma = 0);
  48. //define a function to fusion image by mean image,fusion single refresh mode
  49. void FusionImageByThinRectWindowSlideMean(unsigned char* pPano, cv::Rect InferRc, cv::Mat& InferImg,cv::Rect CurCoordinateRc,cv::Mat& TargetImg,cv::Rect TargetRc,int nSize, int nType=0, float Sigma=0);
  50. cv::Mat CreateThinRectWindow(int nHeight, int nWidth, int nType, int nChannels);
  51. //
  52. double CaculateGaussWeight(double x, double sigma);
  53. void Convolution2D(cv::Mat& Src, cv::Mat& Dst, cv::Mat& Kernel);
  54. //1d convolution
  55. void Convolution1D(cv::Mat& Src, cv::Mat& Dst, cv::Mat& Kernel);
  56. /*****************************Blending gradient*********************************/
  57. void FusionImageByBlendingGradient(unsigned char* pPano, cv::Rect InferRc, cv::Mat& InferImg, cv::Rect CurCoordinateRc, cv::Mat& TargetImg, cv::Rect TargetRc);
  58. void FusionImageByBlendingGradientYUV(
  59. unsigned char* pPano,
  60. cv::Rect InferRc,
  61. unsigned char* pInfer,
  62. int nInferWidth,
  63. int nInferHeight,
  64. int nInferPitch,
  65. cv::Rect CurCoordinateRc,
  66. unsigned char* pTarget,
  67. int nTargetWidth,
  68. int nTargetHeight,
  69. int nTargetPitch,
  70. cv::Rect TargetRc,
  71. bool bUseSSE2 = false
  72. );
  73. void FusionImageByBlendingGradientYUVByGpu(
  74. unsigned char* pPano,
  75. cv::Rect InferRc,
  76. unsigned char* pInfer,
  77. int nInferWidth,
  78. int nInferHeight,
  79. int nInferPitch,
  80. cv::Rect CurCoordinateRc,
  81. unsigned char* pTarget,
  82. int nTargetWidth,
  83. int nTargetHeight,
  84. int nTargetPitch,
  85. cv::Rect TargetRc
  86. );
  87. void GetGradientMask(cv::Rect Infer, cv::Rect Target, cv::Mat& MaskTarget,cv::Mat& MaskInfer ,std::vector<bool>& bAddOrSub);
  88. /*****************************Blending gradient by yuv***************************************************/
  89. void BlendingLinearYUV(
  90. unsigned char* pInfer,
  91. int nInferWidth,
  92. int nInferHeight,
  93. int nInferPitch,
  94. unsigned char* pTarget,
  95. int nTargetWidth,
  96. int nTargetHeight,
  97. int nTargetPitch,
  98. cv::Mat& cvInferWeight,
  99. cv::Mat& cvTargetWeight,
  100. unsigned char*& pResult,
  101. bool bUseAVX = false
  102. );
  103. void BlendingLinearYUVByGpu(
  104. unsigned char* pInfer,
  105. int nInferWidth,
  106. int nInferHeight,
  107. int nInferPitch,
  108. unsigned char* pTarget,
  109. int nTargetWidth,
  110. int nTargetHeight,
  111. int nTargetPitch,
  112. cv::Mat& cvInferWeight,
  113. cv::Mat& cvTargetWeight,
  114. unsigned char*& pResult
  115. );
  116. /*****************************************粘贴指定部分yuv图像到yuv全景上面*************************************/
  117. void PastePartInWholeImageYUV(
  118. unsigned char* pPanoImg, int nPanoWidth, int nPanoHeight, int nPanoPitch,
  119. unsigned char* pPartImg, int nPartWidth, int nPartHeight, int nPartPitch,
  120. int nLeft, int nTop,
  121. bool bUseAVX = false
  122. );
  123. void PastePartInWholeImageYUVByGpu(
  124. unsigned char* pPanoImg, int nPanoWidth, int nPanoHeight, int nPanoPitch,
  125. unsigned char* pPartImg, int nPartWidth, int nPartHeight, int nPartPitch,
  126. int nLeft, int nTop,
  127. CUstream* pStream = NULL
  128. );
  129. /****************************************************GPU初始化**************************************************/
  130. private:
  131. //pointer to store the image,and this image has been aligned4
  132. unsigned char* pPanoImageBuffer;
  133. //设置一个用于存放gpu运算的buffer
  134. unsigned char* pPanoImageBufferGpu;
  135. //pano image width
  136. int nPanoWidth;
  137. //pano image height
  138. int nPanoHeight;
  139. //pano image pitch
  140. int nPanoPitch;
  141. //Additional Window Widen
  142. int nAdditionalWiden;
  143. //create a queue to send the command let the painter process work
  144. bool bIsModified;
  145. };