OpenCV写入不起作用 [英] OpenCV imwrite is not working
问题描述
我有一个简单的OpenCV应用程序,该应用程序从网络摄像头获取视频流,按下spacebar
时,它将捕获当前图像并冻结在该图像上.当我尝试使用cv::imwrite()
方法将图片保存到磁盘时,它不起作用.该代码可以成功编译,但不会保存图像.它也从调用中返回一个假值.我不确定这是否是图像类型或其他问题,但是我似乎很困惑.
I have a simple OpenCV application that takes a video stream from the webcam, and when the spacebar
is pressed it captures the current image and freezes on that image. When I try to use the cv::imwrite()
method to save the picture to disk, it does not work. The code successfully compiles but it does not save the image. It is returning a false value as well from the call. I am not sure if this is an issue of type of image or something else, but I seem to be stumped.
这是我当前的cpp类的代码:
Here is the code for my current cpp class:
#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
写入/
目录.执行程序可能没有足够的权限来写入该目录.根据您的评论,您可能想要
You are attempting to write testimage.jpg
to the /
directory. The executing program probably doesn't have sufficient permissions to write to that directory. Based on your comment, you probably want
//trying to save to current directory
bool maybe = imwrite("./testimage.jpg", picture);
因为.
表示当前工作目录.
Since .
denotes the current working directory.
这篇关于OpenCV写入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!