Problem while writing an image and video in a particular location

asked 2017-08-18 04:06:15 -0600

Sowmya Shree gravatar image

updated 2017-08-18 05:05:12 -0600

Image read is working properly. but while writing an image in a particular location such as C or D drive. Error is 'failed to write image'. same issue for video read and write also. please help me to solve this issue.

Image write program

#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
    Mat img(650, 600, CV_16UC3, Scalar(0, 50000, 50000)); 

    if (img.empty()) 
    {
        cout << "ERROR : Image cannot be loaded..!!" << endl;
        system("pause"); 
        return -1;
    }


    vector<int> compression_params; 

    compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); 

    compression_params.push_back(98); 

    bool bSuccess = imwrite("C:\\Users\\sbv\\Documnents\\TestImage.jpg", img, compression_params); 

    if (!bSuccess)
    {

        cout << "ERROR : Failed to save the image" << endl;
        system("pause"); 

    }

    namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); 
    imshow("MyWindow", img); 

    waitKey(0);  

    destroyWindow("MyWindow"); 

    return 0;
}


**Video write program**
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); 

    //VideoCapture cap("C:\\Users\\sbv\\Documents\\video.mp4");

    if (!cap.isOpened())  
    {
        cout << "ERROR: Cannot open the video file" << endl;
        return -1;
    }

    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); 

    double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
    double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); 

    cout << "Frame Size = " << dWidth << "x" << dHeight << endl;

    Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));

    VideoWriter VideoWriter("C:/MyVideo.avi", CV_FOURCC('P', 'I', 'M', '1'), 20, frameSize, true); 

    if (!VideoWriter.isOpened()) 
    {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
    }

    while (1)
    {

        Mat frame;

        bool bSuccess = cap.read(frame); 

        if (!bSuccess) 
        {
            cout << "ERROR: Cannot read a frame from video file" << endl;
            break;
        }

        VideoWriter.write(frame); 

        imshow("MyVideo", frame); 

        if (waitKey(10) == 27) 
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }

    return 0;

}
edit retag flag offensive close merge delete

Comments

please show the code

sturkmen gravatar imagesturkmen ( 2017-08-18 04:14:40 -0600 )edit
1

Please add relevant code to point out the bug in the program! Note: Please go through the tutorial documentation, If you cannot find relevant samples google it. This saves lot of your time while waiting for reply from here!

Balaji R gravatar imageBalaji R ( 2017-08-18 04:16:10 -0600 )edit

correct typo Documnents -> Documents

bool bSuccess = imwrite("C:\\Users\\sbv\\Documents\\TestImage.jpg", img, compression_params); 
sturkmen gravatar imagesturkmen ( 2017-08-18 04:54:27 -0600 )edit
1

try to save C:/Users/sbv/Documents ( maybe you are not allowed to write on root)

VideoWriter VideoWriter("C:/Users/sbv/Documents/MyVideo.avi", CV_FOURCC('P', 'I', 'M', '1'), 20, frameSize, true);

sturkmen gravatar imagesturkmen ( 2017-08-18 04:58:45 -0600 )edit