gpt4 book ai didi

c++ - OpenCV imwrite 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 22:10:34 27 4
gpt4 key购买 nike

我有一个简单的 OpenCV 应用程序,它从网络摄像头获取视频流,当按下 spacebar 时,它会捕获当前图像并卡住在该图像上。当我尝试使用 cv::imwrite() 方法将图片保存到磁盘时,它不起作用。代码成功编译,但不保存图像。它也从调用中返回一个假值。我不确定这是图像类型问题还是其他问题,但我似乎被难住了。

这是我当前的 cpp 类的代码:

#include <iostream>

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

Mat picture;
char key;

class FacialRec {

};

int main() {
//Starting the video
VideoCapture videoCapture(0);

if (!videoCapture.isOpened()) {
cout << "Unable to open video file." << endl;
return 1;
}

namedWindow("Webcam", CV_WINDOW_AUTOSIZE);

while(true) {
Mat frame;
videoCapture.retrieve(frame);
bool success = videoCapture.read(frame);

if (!success) {
cout << "Could not read from video file" << endl;
return 1;
}

imshow("Webcam", frame);
key = waitKey(30);

if (key == 27) { //escape key pressed: stop program
cout << "ESC pressed. Program closing..." << endl;
break;
}else if (key == ' ') { //spacebar pressed: take a picture
picture = frame;
key = -1;

while (true) {
imshow("Webcam", picture);

key = waitKey(30);

if (key == 27 || key == 32) {
cout << "ESC or SPACE pressed. Returning to video..." << endl;
break;
}

if (key == 115) {
//trying to save to current directory
bool maybe = imwrite("/testimage.jpg", picture);
// maybe bool is always getting value of 0, or false
cout << "s was pressed. saving image " << maybe << endl;
}
}
}

}

return 0;
}

最佳答案

您正在尝试将 testimage.jpg 写入 / 目录。正在执行的程序可能没有足够的权限写入该目录。根据您的评论,您可能想要

//trying to save to current directory
bool maybe = imwrite("./testimage.jpg", picture);

因为 . 表示当前工作目录。

关于c++ - OpenCV imwrite 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39355291/

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