From 0a6b347614deeb1acad26a9fedb059f831a56d1c Mon Sep 17 00:00:00 2001 From: will Date: Sun, 3 Nov 2024 02:02:24 +0000 Subject: [PATCH] Fixed build issue and moved build script --- Cargo.toml | 7 +++- build.rs | 12 ++++++ src/build.rs | 25 ------------ src/camera.cpp | 19 --------- src/main.rs | 9 +++-- src/perspective.cpp | 94 ++++++++++++++++++++++++++++++++++++--------- 6 files changed, 98 insertions(+), 68 deletions(-) create mode 100644 build.rs delete mode 100644 src/build.rs delete mode 100644 src/camera.cpp diff --git a/Cargo.toml b/Cargo.toml index 54442bc..056ed5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 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 index 580e1f5..0000000 --- a/src/build.rs +++ /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 index 386b584..0000000 --- a/src/camera.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -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); - } - } -} diff --git a/src/main.rs b/src/main.rs index c1b5207..4f403d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,9 +37,9 @@ extern "C" { } struct ImageArray { - data: [u8; SPECTOGRAM_AREA * 3], + data: Vec, homography: [f64; 9], - camera_buffer: [u8; IMAGE_AREA], + camera_buffer: Vec, 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); } diff --git a/src/perspective.cpp b/src/perspective.cpp index dce16a6..3dec65a 100644 --- a/src/perspective.cpp +++ b/src/perspective.cpp @@ -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 +#include 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 detector = SURF::create( minHessian ); +// +// std::vector keypoints1, keypoints2; +// Mat descriptors1, descriptors2; +// detector->detectAndCompute( img1, noArray(), keypoints1, descriptors1 ); +// detector->detectAndCompute( img2, noArray(), keypoints2, descriptors2 ); +// +// // match descriptors +// Ptr matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED); +// std::vector< std::vector > knn_matches; +// +// matcher->knnMatch( descriptors1, descriptors2, knn_matches, 2 ); +// +// // filter matches by the ratio test +// const float ratio_thresh = 0.7f; +// std::vector 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 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(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(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(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()); } } -- 2.39.2