gpt4 book ai didi

c++ - "conversion from ‘IplImage* {aka _IplImage*}’ 到非标量类型 ‘cv::Mat’ 请求“使用 cvQueryFrame 时出错

转载 作者:行者123 更新时间:2023-11-27 22:53:51 25 4
gpt4 key购买 nike

我正在尝试运行以下代码:

#include <highgui.h>
#include <iostream>
#include <stdio.h>
#include <cv.h>

using namespace std;
using namespace cv;
using namespace std;

int main()
{
cvNamedWindow("Brezel detecting camera", 1);
// Capture images from any camera connected to the system
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);

// Load the trained model
CascadeClassifier brezelDetector;
brezelDetector.load("src/person.xml");

if (brezelDetector.empty())
{
printf("Empty model.");
return 0;
}

char key;
while (true)
{
// Get a frame from the camera
Mat frame = cvQueryFrame(capture); //----->>>>>>> This line

std::vector<Rect> brezels;

// Detect brezels
brezelDetector.detectMultiScale(frame, brezels, 1.1, 30,
0 | CV_HAAR_SCALE_IMAGE, Size(200, 320));

for (int i = 0; i < (int) brezels.size(); i++)
{
Point pt1(brezels[i].x, brezels[i].y);
Point pt2(brezels[i].x + brezels[i].width,
brezels[i].y + brezels[i].width);

// Draw a rectangle around the detected brezel
rectangle(frame, pt1, pt2, Scalar(0, 0, 255), 2);
putText(frame, "Brezel", pt1, FONT_HERSHEY_PLAIN, 1.0,
Scalar(255, 0, 0), 2.0);

}

// Show the transformed frame
imshow("Brezel detecting camera", frame);

// Read keystrokes, exit after ESC pressed
key = cvWaitKey(10);
if (char(key) == 27)
{
break;
}
}

return 0;
}

但我在这一行收到“从‘IplImage* {aka _IplImage*}’到非标量类型‘cv::Mat’请求的转换”错误:

Mat frame = cvQueryFrame(capture);

我正在使用 opencv3。我该如何解决这个问题?

谢谢,

最佳答案

嗯,我真的不建议你使用旧的/过时的 IplImage 格式,但是转换成 cv::Mat 是可能的,如下所示:

cv::Ptr<IplImage> iplimg(cvQueryFrame(capture)); // cv::Ptr<T> is safe ref-counting pointer class
if(!iplimg)
{
break;
}

// cv::Mat replaces the CvMat and IplImage, but it's easy to convert
// between the old and the new data structures (by default, only the header
// is converted, while the data is shared)
cv::Mat img = cv::cvarrToMat(iplimg);

你为什么不使用cv::VideoCapture相反?

cv::VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;

while(true)
{
cv::Mat frame;
cap >> frame; // get a new frame from camera

cv::imshow("frame", frame);
cv::waitKey(1);
}

关于c++ - "conversion from ‘IplImage* {aka _IplImage*}’ 到非标量类型 ‘cv::Mat’ 请求“使用 cvQueryFrame 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35011952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com