我在S5P6818开发板编写opencv代码获取/dev/video1摄像头画面并处理,调用动态编译的opencv lib库,然后报错
Corrupt JPEG data: premature end of data segment
[ INFO:0] Initialize OpenCL runtime...
OpenCV(3.4.1) Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /opt/opencv-makeArm/opencv-3.4.1/modules/highgui/src/window.cpp, line 636
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(3.4.1) /opt/opencv-makeArm/opencv-3.4.1/modules/highgui/src/window.cpp:636: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage
我的lib库内容
我的opencv代码
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
// 创建视频捕获对象
cv::VideoCapture cap;
double scale = 4;
double fx = 1 / scale;
// 打开网络摄像头视频流
//std::string streamUrl = "http://192.168.1.165:8080/?action=stream";
//if (!cap.open(streamUrl)) {
// std::cout << "无法打开网络摄像头视频流" << std::endl;
// return -1;
// }
std::string devicePath = "/dev/video0";
if (!cap.open(devicePath)) {
std::cout << "无法打开视频设备" << std::endl;
return -1;
}
// 加载猫脸检测器 Haar cascade
cv::CascadeClassifier detector;
detector.load("haarcascade_frontalcatface_extended.xml");
while (true) {
// 读取视频帧
cv::Mat frame;
cap.read(frame);
// 将帧转换为灰度图像
cv::Mat gray;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
/* 缩放图像 */
cv::Mat smallImg;
#ifdef VERSION_2_4
cv::resize( gray, smallImg, cv::Size(), fx, fx, cv::INTER_LINEAR );
#else
cv::resize( gray, smallImg, cv::Size(), fx, fx, cv::INTER_LINEAR_EXACT);
#endif
cv::equalizeHist( smallImg, smallImg ); // 直方图均衡化,提高图像质量
// 在灰度图像中检测猫脸
std::vector<cv::Rect> rects;
detector.detectMultiScale(smallImg, rects, 1.05, 10, cv::CASCADE_SCALE_IMAGE, cv::Size(25, 25));
// 遍历猫脸并绘制每个矩形边界
for (size_t i = 0; i < rects.size(); ++i) {
//cv::rectangle(frame, rects[i], cv::Scalar(0, 0, 255), 2);
//cv::putText(frame, "Cat #" + std::to_string(i + 1), cv::Point(rects[i].x, rects[i].y - 10), cv::FONT_HERSHEY_SIMPLEX, 0.55, cv::Scalar(0, 0, 255), 2);
cv::rectangle(frame, cv::Rect(rects[i].x * 4, rects[i].y * 4, rects[i].width * 4, rects[i].height * 4), cv::Scalar(0, 0, 255), 2);
// 打印猫脸的坐标
std::cout << "猫脸 #" << i + 1 << " 的坐标:(" << rects[i].x *4<< ", " << rects[i].y*4 << ")" << std::endl;
}
// 显示带有猫脸框的视频帧
cv::imshow("Cat Faces", frame);
// 按下 'q' 键退出循环
if (cv::waitKey(1) == 'q') {
break;
}
}
// 释放视频捕获对象和关闭窗口
cap.release();
cv::destroyAllWindows();
return 0;
}
这段代码在电脑端ubuntu本地测试是可以执行的,但是移植到开发板后就报错了
根据报错信息,OpenCV 在 S5P6818 开发板上编译时没有启用 GTK 支持,导致 cvShowImage 接口不可用。
你可以尝试以下方法解决: