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

268 lines
7.3 KiB

  1. #include "sse.h"
  2. #include "doubleOps.h"
  3. #include "stdio.h"
  4. #include <iostream>
  5. #include "quaternions.h"
  6. #include "opencv2/core/core.hpp"
  7. #include "opencv2/imgproc/imgproc.hpp"
  8. #include "opencv2/imgproc/types_c.h"
  9. #include "opencv2/highgui/highgui.hpp"
  10. #include "opencv2/photo/photo.hpp"
  11. //#include "opencv2/gpu/gpu.hpp"
  12. #ifdef _MSC_VER
  13. #include "windows.h"
  14. #include "time.h"
  15. #endif
  16. using namespace std;
  17. #define ERROR_EPS 1.0e-12
  18. #define fequal(a, b) (fabs((a) - (b))< ( (fabs(a) + fabs(b)) / 2 * ERROR_EPS))
  19. void Test_2D_cross_product()
  20. {
  21. #if EMGU_SSE2
  22. __m128d v0 = _mm_set_pd(0.01, 0.02);
  23. __m128d v1 = _mm_set_pd(0.03, 0.04);
  24. double val0;
  25. _mm_store_sd(&val0, _cross_product(v1, v0));
  26. double val1 = 0.01 * 0.04 - 0.02 * 0.03;
  27. cout <<"Test 2D cross product: " << (fequal(val0, val1) ? "Passed" : "Failed") << std::endl;
  28. #endif
  29. }
  30. void Test_3D_cross_product()
  31. {
  32. CvPoint3D64f
  33. x = cvPoint3D64f(1.0, 0.0, 0.0),
  34. y = cvPoint3D64f(0.0, 1.0, 0.0),
  35. z = cvPoint3D64f(0.0, 0.0, 1.0),
  36. temp;
  37. bool pass = true;
  38. cvPoint3D64fCrossProduct(&x, &y, &temp);
  39. pass &= cvPoint3D64Equals(&temp, &z);
  40. cvPoint3D64fCrossProduct(&y, &z, &temp);
  41. pass &= cvPoint3D64Equals(&temp, &x);
  42. cvPoint3D64fCrossProduct(&z, &x, &temp);
  43. pass &= cvPoint3D64Equals(&temp, &y);
  44. cout <<"Test cvPoint3D64f cross product: " << (pass ? "Passed" : "Failed") << std::endl;
  45. }
  46. void Test_UMat_MinMaxLoc()
  47. {
  48. cv::UMat m(5, 5, CV_8UC1);
  49. m.setTo(255);
  50. double minVal, maxVal;
  51. cv::Point minLoc, maxLoc;
  52. cv::minMaxLoc(m, &minVal, &maxVal, &minLoc, &maxLoc);
  53. cout << "minVal: " << minVal << "; maxVal: " << maxVal << std::endl;
  54. }
  55. void Test_double_MulS()
  56. {
  57. double val0[] = {0.1, 0.2, 0.3};
  58. double scale = 0.12345;
  59. double val1[3];
  60. bool success = true;
  61. doubleOps::mulS(val0, scale, 3, val1);
  62. for (int i = 0; i < 3; i++)
  63. {
  64. bool equals = fequal(val1[i], (val0[i] * scale));
  65. if (!equals) cout << val1[i] << " != " << (val0[i] * scale) << std::endl;
  66. success &= equals;
  67. }
  68. memset(val1, 0, 3* sizeof(double));
  69. doubleOps::mulS(val0, scale, 2, val1);
  70. for (int i = 0; i < 2; i++)
  71. {
  72. bool equals = fequal(val1[i], (val0[i] * scale));
  73. if (!equals) cout << val1[i] << " != " << (val0[i] * scale) << std::endl;
  74. success &= equals;
  75. }
  76. cout <<"Test mulS: " << (success ? "Passed" : "Failed") << std::endl;
  77. }
  78. void Test_quaternions()
  79. {
  80. const double eps = 1.0e-10;
  81. Quaternions q1, q2, q;
  82. CvPoint3D64f a1, a2;
  83. a1.x = 0.0; a1.y = 175.0 / 180.0 * CV_PI; a1.z = 0.0;
  84. a2.x = 0.0; a2.y = 5.0 / 180.0 * CV_PI; a2.z = 0.0;
  85. q1.setAxisAngle(&a1);
  86. q2.setAxisAngle(&a2);
  87. q1.slerp(&q2, 0.5, &q);
  88. double x=0, y=0, z=0;
  89. q.getEuler(&x, &y, &z);
  90. cout << "Test quaternions slerp: " << ((
  91. (fabs(x) < eps || fabs(x - CV_PI) < eps)
  92. && (fabs(y-CV_PI / 2.0) < eps )
  93. && (fabs(z) < eps || fabs(x - CV_PI) < eps))
  94. ? "Passed" : "Failed" ) << std::endl;
  95. q2 = q1;
  96. q1.conjugate();
  97. q1.multiply(&q2, &q);
  98. cout << "Test quaternions inverse: " <<
  99. ( ( fabs(q.w - 1.0) < eps && fabs(q.x) < eps && fabs(q.y) < eps && fabs(q.z) < eps)
  100. ? "Passed" : "Failed") << std::endl;
  101. }
  102. /*
  103. void Test_GpuMatCopy()
  104. {
  105. cv::gpu::GpuMat m1(480, 320, CV_8UC1);
  106. cv::gpu::GpuMat m2(480, 320, CV_8UC1);
  107. m1.copyTo(m2);
  108. }*/
  109. void Test_vectorOfPoint_to_mat()
  110. {
  111. std::vector< cv::Point3f > pts;
  112. pts.push_back(cv::Point3f(0, 1, 2));
  113. pts.push_back(cv::Point3f(1, 2, 3));
  114. std::vector< std::vector< cv::Point3f > > vecOfVec;
  115. vecOfVec.push_back(pts);
  116. cv::InputArray iaPts = cv::InputArray(vecOfVec);
  117. //cv::Mat m = iaPts.getMat();
  118. }
  119. #ifdef _MSC_VER
  120. void Test_MSMF_VideoWriter()
  121. {
  122. cv::Size frameSize(640, 480);
  123. cv::VideoWriter writer("tmp.mp4", 1400, cv::VideoWriter::fourcc('H', '2', '6', '4'), 24, frameSize, true);
  124. bool isOpen = writer.isOpened();
  125. cv::Mat frame(frameSize, CV_8UC3, cv::Scalar(255, 0, 0));
  126. writer.write(frame);
  127. writer.release();
  128. }
  129. void Test_quaternions_performance()
  130. {
  131. LARGE_INTEGER begin;
  132. LARGE_INTEGER end;
  133. Quaternions q1, q2, q;
  134. /* initialize random seed: */
  135. srand ( time(NULL) );
  136. q1.w = rand(); q1.x = rand(); q1.y = rand(); q1.z = rand();
  137. q2.w = rand(); q2.x = rand(); q2.y = rand(); q2.z = rand();
  138. q1.renorm();
  139. q2.renorm();
  140. int count = 100000;
  141. {
  142. QueryPerformanceCounter(&begin);
  143. for (int i = 0; i < count; i++)
  144. {
  145. //perform tasks
  146. q1.multiply(&q2, &q1);
  147. }
  148. QueryPerformanceCounter(&end);
  149. cout <<"Quaternions multiplication total CPU Cycle: " << (end.QuadPart - begin.QuadPart) << std::endl;
  150. }
  151. {
  152. QueryPerformanceCounter(&begin);
  153. for (int i = 0; i < count; i++)
  154. {
  155. //perform tasks
  156. q1.renorm();
  157. }
  158. QueryPerformanceCounter(&end);
  159. cout <<"Quaternions renorm total CPU Cycle: " << (end.QuadPart - begin.QuadPart) << std::endl;
  160. }
  161. }
  162. #endif
  163. void Test_MatchTemplate()
  164. {
  165. cv::Point offset(39, 123);
  166. cv::Mat templ(54, 71, CV_8UC3, cv::Scalar_<double>(255, 255, 255));
  167. cv::Mat img(300, 200, CV_8UC3, cv::Scalar_<double>(0,0,0));
  168. cv::Mat submat = img(cv::Range(offset.y, offset.y+templ.rows), cv::Range(offset.x, offset.x + templ.cols));
  169. templ.copyTo(submat);
  170. cv::Mat result;
  171. cv::matchTemplate(img, templ, result, CV_TM_SQDIFF_NORMED);
  172. double minVal, maxVal;
  173. cv::Point minLoc, maxLoc;
  174. cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
  175. cout << "Template matched expected: " << offset.x << "," << offset.y << "; computed: " << minLoc.x << "," << minLoc.y << /*"; maxLoc: " << maxLoc.x << "," <<maxLoc.y <<*/ std::endl;
  176. }
  177. void Test_SeamlessClone(int size)
  178. {
  179. //cv::Mat source = cv::imread("C:\\work\\sourceforge\\emgucv\\libs\\x64\\lena.jpg");
  180. cv::Mat source = cv::imread(".\\lena.jpg");
  181. if (!source.empty())
  182. {
  183. cv::Mat img1;
  184. cv::resize(source, img1, cv::Size(size, size));
  185. cv::Mat img2;
  186. cv::resize(source, img2, cv::Size(size / 2, size / 2));
  187. cv::Mat mask(img2.size(), CV_8UC1);
  188. int rows = mask.rows;
  189. int cols = mask.cols;
  190. int radius = (int)((std::min)(rows, cols) / 2.0);
  191. cv::circle(mask, cv::Point(mask.rows / 2, mask.cols / 2), radius, cv::Scalar(255), -1);
  192. cv::TickMeter meter;
  193. cv::Mat blend(img1.size(), CV_8UC3);
  194. meter.start();
  195. cv::seamlessClone(img2, img1, mask, cv::Point(mask.rows / 2, mask.cols / 2), blend, cv::NORMAL_CLONE);
  196. meter.stop();
  197. cout << "Seamless clone time: " << meter.getTimeMilli() << " milliseconds. " << std::endl;
  198. }
  199. }
  200. int main()
  201. {
  202. char tmp;
  203. //Test_CvPoint2D32f();
  204. Test_2D_cross_product();
  205. Test_3D_cross_product();
  206. Test_double_MulS();
  207. Test_quaternions();
  208. //Test_GpuMatCopy();
  209. Test_MatchTemplate();
  210. Test_UMat_MinMaxLoc();
  211. cout << "Size of CvSize (expected " << sizeof(int) * 2 << "): " << sizeof(CvSize) << std::endl;
  212. cout << "Size of CvPoint2D32f (expected " << sizeof(float) * 2 << "): " << sizeof(CvSize) << std::endl;
  213. cout << "Size of CvRect (expected " << sizeof(int) * 4 << "): " << sizeof(CvRect) << std::endl;
  214. cout << "Size of IplImage (expected 144):" << sizeof(IplImage) << std::endl;
  215. cout << "Size of CvScalar (expected " << sizeof(double) * 4 << "): " << sizeof(CvScalar) << std::endl;
  216. Test_vectorOfPoint_to_mat();
  217. #ifdef _MSC_VER
  218. Test_quaternions_performance();
  219. Test_SeamlessClone(3840);
  220. Test_MSMF_VideoWriter();
  221. cin >>tmp; //wait for input only if compiling with visual C++
  222. #endif
  223. }