test_sift.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. TEST(Features2d_SIFT, descriptor_type)
  7. {
  8. Mat image = imread(cvtest::findDataFile("features2d/tsukuba.png"));
  9. ASSERT_FALSE(image.empty());
  10. Mat gray;
  11. cvtColor(image, gray, COLOR_BGR2GRAY);
  12. vector<KeyPoint> keypoints;
  13. Mat descriptorsFloat, descriptorsUchar;
  14. Ptr<SIFT> siftFloat = cv::SIFT::create(0, 3, 0.04, 10, 1.6, CV_32F);
  15. siftFloat->detectAndCompute(gray, Mat(), keypoints, descriptorsFloat, false);
  16. ASSERT_EQ(descriptorsFloat.type(), CV_32F) << "type mismatch";
  17. Ptr<SIFT> siftUchar = cv::SIFT::create(0, 3, 0.04, 10, 1.6, CV_8U);
  18. siftUchar->detectAndCompute(gray, Mat(), keypoints, descriptorsUchar, false);
  19. ASSERT_EQ(descriptorsUchar.type(), CV_8U) << "type mismatch";
  20. Mat descriptorsFloat2;
  21. descriptorsUchar.assignTo(descriptorsFloat2, CV_32F);
  22. Mat diff = descriptorsFloat != descriptorsFloat2;
  23. ASSERT_EQ(countNonZero(diff), 0) << "descriptors are not identical";
  24. }
  25. TEST(Features2d_SIFT, regression_26139)
  26. {
  27. auto extractor = cv::SIFT::create();
  28. cv::Mat1b image{cv::Size{300, 300}, 0};
  29. std::vector<cv::KeyPoint> kps {
  30. cv::KeyPoint(154.076813f, 136.160904f, 111.078636f, 216.195618f, 0.00000899323549f, 7)
  31. };
  32. cv::Mat descriptors;
  33. extractor->compute(image, kps, descriptors); // we expect no memory corruption
  34. ASSERT_EQ(descriptors.size(), Size(128, 1));
  35. }
  36. }} // namespace