Parcourir la source

解决单图片模式下出现报错问题

w il y a 1 mois
Parent
commit
3f876c7cf7
3 fichiers modifiés avec 15 ajouts et 25 suppressions
  1. 3 0
      README
  2. 1 0
      include/FeedLevelDetector.h
  3. 11 25
      src/FeedLevelDetector.cpp

+ 3 - 0
README

@@ -1,3 +1,6 @@
+使用方式:单图片模式:cd build ./ feed_level_detection ../test6.jpg
+        摄像头模式:cd build ./ fees_level_detection --camera 10
+         此模式下:10s 30s 60s 可选
 实现目标:通过单目视觉测量饲料表面与图像顶部的垂直距离
 实现思路:图像处理,边缘检测,表面定位(取最高点),计算距离,结果可视化
 输入参数:图像

+ 1 - 0
include/FeedLevelDetector.h

@@ -19,6 +19,7 @@ public:
     float detectFeedLevel(const std::string& imagePath);
     
 private:
+    bool useCamera; // 标志位
     int captureInterval; // 采集间隔(秒)
     cv::VideoCapture camera;
 

+ 11 - 25
src/FeedLevelDetector.cpp

@@ -3,11 +3,7 @@
 #include <iostream>
 #include <thread>
 
-FeedLevelDetector::FeedLevelDetector() : captureInterval(10) {
-    camera.open(0); // 默认打开第一个摄像头
-    if (!camera.isOpened()) {
-        std::cerr << "Error: Could not open camera!" << std::endl;
-    }
+FeedLevelDetector::FeedLevelDetector() : captureInterval(10), useCamera(false) {
     lastCaptureTime = std::chrono::system_clock::now();
 }
 
@@ -26,6 +22,8 @@ bool FeedLevelDetector::shouldCaptureNow() {
 }
 
 void FeedLevelDetector::startDetectionFromCamera() {
+    useCamera = true; // 标记为摄像头模式
+    camera.open(0);   // 打开摄像头
     if (!camera.isOpened()) {
         std::cerr << "Camera not available!" << std::endl;
         return;
@@ -68,10 +66,11 @@ cv::Mat FeedLevelDetector::preprocessImage(const cv::Mat& input) {
     
     // 转换为灰度图像
     cv::cvtColor(input, gray, cv::COLOR_BGR2GRAY);
-    
+    //cv::imshow("gray image", gray);
     // 高斯模糊去噪
-    cv::GaussianBlur(gray, blurred, cv::Size(5, 5), 1.5);
-    
+    //cv::GaussianBlur(gray, blurred, cv::Size(5, 5), 1.5);
+    cv::GaussianBlur(gray, blurred, cv::Size(7, 7), 3.0);
+    //cv::imshow("blurred image", blurred);
     return blurred;
 }
 
@@ -79,8 +78,9 @@ float FeedLevelDetector::findFeedSurface(const cv::Mat& image) {
     cv::Mat edges;
     
     // Canny边缘检测
-    cv::Canny(image, edges, 100, 150);
-    //cv::Canny(image, edges, 100, 200);
+    cv::Canny(image, edges, 150, 200);
+    //cv::Canny(image, edges, 150, 255);
+    cv::imshow("edges image", edges);
     //cv::Canny(image, edges, 50, 100);
     // 查找轮廓
     std::vector<std::vector<cv::Point>> contours;
@@ -128,23 +128,9 @@ void FeedLevelDetector::displayResult(cv::Mat& image, float distance) {
     auto nextCapture = lastCaptureTime + std::chrono::seconds(captureInterval);
     auto remain = std::chrono::duration_cast<std::chrono::seconds>(nextCapture - now).count();
 
-//    std::string timerText = "Next capture in: " + std::to_string(remain) + "s";
-//    cv::putText(displayImage, timerText,
-//                cv::Point(20, 60),
-//                cv::FONT_HERSHEY_SIMPLEX,
-//                0.6, cv::Scalar(255, 255, 0), 1);
-
     // 更新显示
     cv::imshow("Feed Level Monitoring", displayImage);
-    
-//    // 显示距离信息(图像坐标系原点在左上角)
-//    std::string distanceText = "Distance: " + std::to_string(distance) + " pixels";
-//    cv::putText(image, distanceText, cv::Point(20, 30),
-//                cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 255, 0), 2);
-//
-//    // 显示图像
-//    cv::imshow("Feed Level Detection", image);
-//    cv::waitKey(0);
+
 }
 
 float FeedLevelDetector::detectFeedLevel(const std::string& imagePath) {