]> OzVa Git service - rust_fft/commitdiff
Fixed build issue and moved build script
authorwill <greenwoodw50@gmail.com>
Sun, 3 Nov 2024 02:02:24 +0000 (02:02 +0000)
committerwill <greenwoodw50@gmail.com>
Sun, 3 Nov 2024 02:02:24 +0000 (02:02 +0000)
Cargo.toml
build.rs [new file with mode: 0644]
src/build.rs [deleted file]
src/camera.cpp [deleted file]
src/main.rs
src/perspective.cpp

index 54442bc5b98e7d7fecb85bdf0dbb907db532fee5..056ed5d8873bd3e8793a156a37b4bd38683127c9 100644 (file)
@@ -2,8 +2,8 @@
 name = "rust_fft"
 version = "0.1.0"
 edition = "2021"
-build = "src/build.rs"
-include = ["src/camera.cpp"]
+build = "build.rs"
+include = ["src/perspective.cpp"]
 
 [dependencies]
 libc = "0.2.161"
@@ -15,3 +15,6 @@ rscam = "0.5.5"
 
 [build-dependencies]
 cc = "1.0"
+
+[env]
+CXXFLAGS = "-Wunused-parameter -lopencv_core -lopencv_highgui -lopencv_xfeatures2d -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_features2d -lopencv_imgproc -lstdc++"
diff --git a/build.rs b/build.rs
new file mode 100644 (file)
index 0000000..771e13d
--- /dev/null
+++ b/build.rs
@@ -0,0 +1,12 @@
+fn main() {
+       println!("cargo::rerun-if-changed=src/perspective.cpp");
+       println!("cargo::rustc-env=CXXFLAGS=-Wunused-parameter -lopencv_core -lopencv_highgui -lopencv_xfeatures2d -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_features2d");
+
+       cc::Build::new()
+               .file("src/perspective.cpp")
+               .cpp(true)
+               .include("/usr/share/include/opencv4/")
+               .compile("perspective.a");
+
+       println!("cargo::rustc-flags=-lopencv_core -lopencv_highgui -lopencv_xfeatures2d -lopencv_calib3d -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_features2d");
+}
diff --git a/src/build.rs b/src/build.rs
deleted file mode 100644 (file)
index 580e1f5..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-fn main() {
-       println!("cargo:rerun-if-changed=src/perspective.cpp");
-       println!("cargo::rustc-link-lib=dylib=opencv_core");
-       println!("cargo::rustc-link-lib=dylib=opencv_highgui");
-       println!("cargo::rustc-link-lib=dylib=opencv_xfeatures2d");
-       println!("cargo::rustc-link-lib=dylib=opencv_calib3d");
-       println!("cargo::rustc-link-lib=dylib=opencv_videoio");
-       println!("cargo::rustc-link-lib=dylib=opencv_imgcodecs");
-       println!("cargo::rustc-link-lib=dylib=opencv_features2d");
-
-       let opencv_path = std::path::Path::new("/usr/share/include/opencv4/");
-       cc::Build::new()
-               .cpp(true)
-               .include(opencv_path)
-               .flag("-Wunused-parameter")
-               .flag("-lopencv_core")
-               .flag("-lopencv_highgui")
-               .flag("-lopencv_xfeatures2d")
-               .flag("-lopencv_calib3d")
-               .flag("-lopencv_videoio")
-               .flag("-lopencv_imgcodecs")
-               .flag("-lopencv_features2d")
-               .file("src/perspective.cpp")
-               .compile("perspective.a");
-}
diff --git a/src/camera.cpp b/src/camera.cpp
deleted file mode 100644 (file)
index 386b584..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-#include <iostream>
-#include <string>
-
-using std::size_t;
-using std::uint8_t;
-
-const size_t WINDOW_SIZE = 128;
-const size_t CHUNK_SIZE = 72;
-const size_t SPECTOGRAM_AREA = WINDOW_SIZE * CHUNK_SIZE;
-
-extern "C" {
-       void GetData(uint8_t *ptr) {
-
-               uint8_t image[SPECTOGRAM_AREA * 3];
-               for (long unsigned int i = 0; i < SPECTOGRAM_AREA * 3; i++) {
-                       image[i] = *(ptr + i);
-               }
-       }
-}
index c1b5207ac9bab7407e76bbef5f671ecdaee0b66f..4f403d53984ded5b3ae4051a1525069c793e0d13 100644 (file)
@@ -37,9 +37,9 @@ extern "C" {
 }
 
 struct ImageArray {
-       data: [u8; SPECTOGRAM_AREA * 3],
+       data: Vec<u8>,
        homography: [f64; 9],
-       camera_buffer: [u8; IMAGE_AREA],
+       camera_buffer: Vec<u8>,
        camera: Camera,
        chunks: usize
 }
@@ -47,9 +47,9 @@ struct ImageArray {
 impl ImageArray {
        fn new (homography: [f64; 9]) -> Self {
                let mut array = Self {
-                       data: [0u8; SPECTOGRAM_AREA * 3],
+                       data: vec![0u8; SPECTOGRAM_AREA * 3],
                        homography,
-                       camera_buffer: [0u8; IMAGE_AREA],
+                       camera_buffer: vec![0u8; IMAGE_AREA],
 
                        camera: Camera::new("/dev/video0").unwrap(),
 
@@ -71,6 +71,7 @@ impl ImageArray {
        }
 
        fn calibrate (&mut self) {
+               println!("Getting homography... unsafe!");
                unsafe {
                        GetHomography(self.camera_buffer.as_ptr() as usize, self.homography.as_ptr() as usize);
                }
index dce16a6b56b956f06d8d7d1c4a93acfa16c3be78..3dec65a8369d829dc2327c66bc736206bf8dfd0f 100644 (file)
@@ -1,9 +1,12 @@
 // 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;
@@ -11,8 +14,12 @@ 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;
@@ -27,18 +34,66 @@ const Size DSIZE_AREA = Size(WINDOW_SIZE, CHUNK_SIZE);
 //     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;
@@ -80,16 +135,19 @@ extern "C"
                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());
        }
 }