// g++ ./perspective.cpp -I/usr/share/include/opencv4/ -lopencv_core -lopencv_calib3d -lopencv_highgui -lopencv_xfeatures2d -lopencv_features2d -lopencv_imgproc -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -o perspective.a
//
-#include "opencv2/core.hpp"
-#include "opencv2/highgui.hpp"
-#include "opencv2/xfeatures2d.hpp"
-#include "opencv2/calib3d.hpp"
+#include "/usr/local/include/opencv4/opencv2/core.hpp"
+#include "/usr/local/include/opencv4/opencv2/highgui.hpp"
+#include "/usr/local/include/opencv4/opencv2/xfeatures2d.hpp"
+#include "/usr/local/include/opencv4/opencv2/calib3d.hpp"
+#include "/usr/local/include/opencv4/opencv2/imgproc.hpp"
+#include <cstring>
+#include <opencv4/opencv2/core/hal/interface.h>
using namespace cv;
using namespace cv::xfeatures2d;
const size_t WINDOW_SIZE = 128;
const size_t CHUNK_SIZE = 72;
const size_t SPECTOGRAM_AREA = WINDOW_SIZE * CHUNK_SIZE;
+const int FLAT_AREA = SPECTOGRAM_AREA * 3;
const Size DSIZE_AREA = Size(WINDOW_SIZE, CHUNK_SIZE);
+const size_t IMAGE_WIDTH = 1920;
+const size_t IMAGE_HEIGHT = 1080;
+
// Mat get_frame()
// {
// VideoCapture cap;
// return frame;
// }
-int main()
-{
- std::cout << "all normal";
- return 0;
-}
+// int main()
+// {
+// Mat img1 = imread( samples::findFile("box.jpg"), IMREAD_GRAYSCALE );
+// Mat img2 = imread( samples::findFile("box_in_scene.jpg"), IMREAD_GRAYSCALE );
+//
+// // detect keypoints and compute descriptors
+// int minHessian = 400;
+// Ptr<SURF> detector = SURF::create( minHessian );
+//
+// std::vector<KeyPoint> keypoints1, keypoints2;
+// Mat descriptors1, descriptors2;
+// detector->detectAndCompute( img1, noArray(), keypoints1, descriptors1 );
+// detector->detectAndCompute( img2, noArray(), keypoints2, descriptors2 );
+//
+// // match descriptors
+// Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
+// std::vector< std::vector<DMatch> > knn_matches;
+//
+// matcher->knnMatch( descriptors1, descriptors2, knn_matches, 2 );
+//
+// // filter matches by the ratio test
+// const float ratio_thresh = 0.7f;
+// std::vector<DMatch> good_matches;
+// for (size_t i = 0; i < knn_matches.size(); i++)
+// {
+// if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
+// {
+// good_matches.push_back(knn_matches[i][0]);
+// }
+// }
+//
+// // get the source and destination points
+// std::vector<Point2f> source_points, dst_points;
+// for (size_t i = 0; i < good_matches.size(); i++)
+// {
+// Point2f s_point = keypoints2[good_matches[i].trainIdx].pt;
+// Point2f d_point = keypoints1[good_matches[i].queryIdx].pt;
+// source_points.push_back(s_point);
+// dst_points.push_back(d_point);
+// }
+//
+// // perform homography
+// double ransac_thresh = 5.0f;
+// Mat homography = findHomography(source_points, dst_points, RANSAC, ransac_thresh);
+//
+// const double* h_ptr = homography.ptr<double>(0);
+// for(int i = 0; i < 9; i++)
+// {
+// std::cout << std::max(h_ptr[i], 0.) << "\n";
+// }
+// }
extern "C"
{
void GetHomography(uint8_t *camera_ptr, double *homography_ptr)
{
- Mat img1 = imread( samples::findFile("box.jpg"), IMREAD_GRAYSCALE );
- Mat img2 = imread( samples::findFile("box_in_scene.jpg"), IMREAD_GRAYSCALE );
+ Mat capture(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3, camera_ptr), img2;
+
+ Mat img1 = imread( samples::findFile("calibration.jpg"), IMREAD_GRAYSCALE );
+ cv::cvtColor(capture, img2, COLOR_RGB2GRAY);;
// detect keypoints and compute descriptors
int minHessian = 400;
double ransac_thresh = 5.0f;
Mat homography = findHomography(source_points, dst_points, RANSAC, ransac_thresh);
- const double* h_ptr = homography.ptr<double>(0);
- for(int i = 0; i < 9; i++)
- *homography_ptr = std::max(h_ptr[i], 0.);
+ // copy the result to the homography location
+ // it should be possible to just point the result of findHomography at the pointer location
+ const double* result_ptr = homography.ptr<double>(0);
+ std::memcpy(homography_ptr, result_ptr, 72); // size of [f64; 9]
}
- void ApplyHomography(uint8_t *camera_ptr, uint8_t *buffer_ptr, float *homography_ptr)
+ void ApplyHomography(uint8_t *camera_ptr, uint8_t *buffer_ptr, double *homography_ptr)
{
- Mat output_image;
- // warpPerspective(img2, output_image, homography, img1.size(), INTER_NEAREST);
- // imwrite("output.jpg", output_image );
+ Mat capture(IMAGE_HEIGHT, IMAGE_WIDTH, CV_8UC3, camera_ptr);
+ Mat buffer(CHUNK_SIZE, WINDOW_SIZE, CV_8UC3, buffer_ptr);
+ Mat homography(3, 3, CV_64F, homography_ptr);
+
+ warpPerspective(capture, buffer, homography, buffer.size());
}
}