WebCamからビデオストリームを取得する簡単なOpenCVアプリケーションがあります。spacebar
を押すと、現在の画像がキャプチャされ、その画像がフリーズします。ディスクに画像を保存するためにcv::imwrite()
メソッドを使用しようとすると、画像が機能しません。コードは正常にコンパイルされますが、イメージは保存されません。呼び出しからも偽の値が返されます。これがイメージのタイプの問題なのかどうかは分かりませんが、私は困惑しているようです。OpenCV imwriteが動作しない
#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;
}
ありがとう!ディレクトリの作成方法について完全にはわかっていませんでした。これで解決しました。 –