简体   繁体   English

OpenCV imwrite无效

[英]OpenCV imwrite is not working

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. 我有一个简单的OpenCV应用程序,它从网络摄像头获取视频流,当按下spacebar时,它会捕获当前图像并冻结该图像。 When I try to use the cv::imwrite() method to save the picture to disk, it does not work. 当我尝试使用cv::imwrite()方法将图片保存到磁盘时,它不起作用。 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. 我不确定这是一个图像类型或其他类型的问题,但我似乎很难过。

Here is the code for my current cpp class: 这是我当前的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;
}

You are attempting to write testimage.jpg to the / directory. 您正在尝试将testimage.jpg写入/目录。 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 sometimes has problems to write to a .jpg image. OpenCV有时会出现写入.jpg图像的问题。 Try to change that to .png or .bmp to see if that makes a difference in your case. 尝试将其更改为.png.bmp ,看看是否会对您的情况产生影响。

If you have further issues with writing images, you can debug them in OpenCV by adding this few lines of code to display them and see if they are valid: 如果您在编写图像时遇到其他问题,可以在OpenCV调试它们,方法是添加以下几行代码来显示它们,看看它们是否有效:

// Create a window for display.
namedWindow( "Display window", WINDOW_AUTOSIZE );

// Show our image inside it.
imshow( "Display window", picture );                   

// Wait for a keystroke in the window
waitKey(0);                                         

A few suggestions. 一些建议。

  1. try a different file format. 尝试不同的文件格式。
  2. does your IDE defiantly know your target folder / home path directory? 您的IDE是否知道您的目标文件夹/主路径目录?
  3. Is your image definitely valid? 你的形象绝对有效吗? does it show when you imshow()? 当你imshow()时它会显示出来吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM