Advertisement
dan-masek

Notes for Anu

Jul 25th, 2017
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. https://stackoverflow.com/questions/45305957/modifying-a-function-in-opencv-and-emgucv-by-adding-extra-parameter
  2.  
  3.  
  4. Original OpenCV filter2D signature:
  5.  
  6. CV_EXPORTS_W void filter2D(InputArray src
  7. , OutputArray dst
  8. , int ddepth
  9. , InputArray kernel
  10. , Point anchor = Point(-1,-1)
  11. , double delta = 0
  12. , int borderType = BORDER_DEFAULT);
  13.  
  14. Modified OpenCV filter2D signature (according to your comment, an extra integer parameter):
  15.  
  16. CV_EXPORTS_W void filter2D(InputArray src
  17. , OutputArray dst
  18. , int ddepth
  19. , InputArray kernel
  20. , Point anchor = Point(-1,-1)
  21. , double delta = 0
  22. , int borderType = BORDER_DEFAULT
  23. , int extra = 0);
  24.  
  25.  
  26.  
  27.  
  28. Let's begin with https://github.com/emgucv/emgucv/blob/master/Emgu.CV.Extern/imgproc/imgproc_c.cpp#L24
  29.  
  30. void cveFilter2D( cv::_InputArray* src, cv::_OutputArray* dst, cv::_InputArray* kernel, CvPoint* anchor, double delta, int borderType )
  31. {
  32. CV_Assert( src->size() == dst->size() && src->channels() == dst->channels() );
  33. cv::filter2D( *src, *dst, dst->depth(), *kernel, *anchor, delta, borderType );
  34. }
  35.  
  36. becomes
  37.  
  38. void cveFilter2D( cv::_InputArray* src, cv::_OutputArray* dst, cv::_InputArray* kernel, CvPoint* anchor, double delta, int borderType, int extra )
  39. {
  40. CV_Assert( src->size() == dst->size() && src->channels() == dst->channels() );
  41. cv::filter2D( *src, *dst, dst->depth(), *kernel, *anchor, delta, borderType, extra );
  42. }
  43.  
  44.  
  45.  
  46. We also need to change the associated header: https://github.com/emgucv/emgucv/blob/master/Emgu.CV.Extern/imgproc/imgproc_c.h#L21
  47.  
  48. CVAPI(void) cveFilter2D(cv::_InputArray* src, cv::_OutputArray* dst, cv::_InputArray* kernel, CvPoint* anchor, double delta, int borderType );
  49.  
  50. becomes
  51.  
  52. CVAPI(void) cveFilter2D(cv::_InputArray* src, cv::_OutputArray* dst, cv::_InputArray* kernel, CvPoint* anchor, double delta, int borderType, int extra );
  53.  
  54.  
  55.  
  56. Next, https://github.com/emgucv/emgucv/blob/master/Emgu.CV/PInvoke/CvInvokeCvextern.cs
  57.  
  58. First, let's fix the dll import https://github.com/emgucv/emgucv/blob/master/Emgu.CV/PInvoke/CvInvokeCvextern.cs#L244
  59.  
  60. [DllImport(ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  61. private static extern void cveFilter2D(IntPtr src, IntPtr dst, IntPtr kernel, ref Point anchor, double delta, Emgu.CV.CvEnum.BorderType borderType);
  62.  
  63. becomes
  64.  
  65. [DllImport(ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
  66. private static extern void cveFilter2D(IntPtr src, IntPtr dst, IntPtr kernel, ref Point anchor, double delta, Emgu.CV.CvEnum.BorderType borderType, int extra);
  67.  
  68.  
  69.  
  70. Now the wrapper function: https://github.com/emgucv/emgucv/blob/master/Emgu.CV/PInvoke/CvInvokeCvextern.cs#L226
  71.  
  72. public static void Filter2D(IInputArray src, IOutputArray dst, IInputArray kernel, Point anchor, double delta = 0,
  73. Emgu.CV.CvEnum.BorderType borderType = CvEnum.BorderType.Default)
  74. {
  75. using (InputArray iaSrc = src.GetInputArray())
  76. using (OutputArray oaDst = dst.GetOutputArray())
  77. using (InputArray iaKernel = kernel.GetInputArray())
  78. cveFilter2D(iaSrc, oaDst, iaKernel, ref anchor, delta, borderType);
  79. }
  80.  
  81. becomes (you can fix the documentation comments yourself)
  82.  
  83.  
  84. public static void Filter2D(IInputArray src, IOutputArray dst, IInputArray kernel, Point anchor, double delta = 0,
  85. Emgu.CV.CvEnum.BorderType borderType = CvEnum.BorderType.Default, int extra = 0)
  86. {
  87. using (InputArray iaSrc = src.GetInputArray())
  88. using (OutputArray oaDst = dst.GetOutputArray())
  89. using (InputArray iaKernel = kernel.GetInputArray())
  90. cveFilter2D(iaSrc, oaDst, iaKernel, ref anchor, delta, borderType, extra);
  91. }
  92.  
  93.  
  94.  
  95. Finally, Image: https://github.com/emgucv/emgucv/blob/master/Emgu.CV/Image.cs#L3653
  96.  
  97.  
  98. public Image<TColor, Single> Convolution(ConvolutionKernelF kernel)
  99. {
  100. Image<TColor, Single> floatImage =
  101. (typeof(TDepth) == typeof(Single)) ?
  102. this as Image<TColor, Single>
  103. : Convert<TColor, Single>();
  104.  
  105. Image<TColor, Single> res = new Image<TColor, Single>(Size);
  106. ForEachDuplicateChannel(
  107. delegate (IImage srcFloat, IImage dest, int channel)
  108. {
  109.  
  110. //perform the convolution operation
  111. CvInvoke.Filter2D(
  112. srcFloat,
  113. dest,
  114. kernel,
  115. kernel.Center,
  116. 0);
  117.  
  118. },
  119. res);
  120.  
  121. if (!object.ReferenceEquals(floatImage, this))
  122. floatImage.Dispose();
  123.  
  124. return res;
  125. }
  126.  
  127.  
  128.  
  129. becomes something like this perhaps:
  130.  
  131.  
  132. public Image<TColor, Single> Convolution(ConvolutionKernelF kernel, int extra = 0)
  133. {
  134. Image<TColor, Single> floatImage =
  135. (typeof(TDepth) == typeof(Single)) ?
  136. this as Image<TColor, Single>
  137. : Convert<TColor, Single>();
  138.  
  139. Image<TColor, Single> res = new Image<TColor, Single>(Size);
  140. ForEachDuplicateChannel(
  141. delegate (IImage srcFloat, IImage dest, int channel)
  142. {
  143.  
  144. //perform the convolution operation
  145. CvInvoke.Filter2D(
  146. srcFloat,
  147. dest,
  148. kernel,
  149. kernel.Center,
  150. 0,
  151. CvEnum.BorderType.Default,
  152. extra);
  153.  
  154. },
  155. res);
  156.  
  157. if (!object.ReferenceEquals(floatImage, this))
  158. floatImage.Dispose();
  159.  
  160. return res;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement