|
@@ -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) {
|