Ask Your Question
0

Error in imwrite code specified in the opencv api reference

asked 2016-06-05 00:02:17 -0600

niranjan.kotha gravatar image

I am very new to opencv and started using OPENCV 3.0 I have run code taken from imwrite in the OPENCV API reference documentation as it is from link text and got the following error:

error: ‘CV_IMWRITE_PNG_COMPRESSION’ was not declared in this scope
     compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);

and the code is as follows:

#include <vector>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

void createAlphaMat(Mat &mat)
{
    for (int i = 0; i < mat.rows; ++i) {
        for (int j = 0; j < mat.cols; ++j) {
            Vec4b& rgba = mat.at<Vec4b>(i, j);
            rgba[0] = UCHAR_MAX;
            rgba[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX);
            rgba[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX);
            rgba[3] = saturate_cast<uchar>(0.5 * (rgba[1] + rgba[2]));
        }
    }
}

int main(int argv, char **argc)
{
    // Create mat with alpha channel
    Mat mat(480, 640, CV_8UC4);
    createAlphaMat(mat);

    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);

    try {
        imwrite("alpha.png", mat, compression_params);
    }
    catch (runtime_error& ex) {
        fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
        return 1;
    }

    fprintf(stdout, "Saved PNG file with alpha data.\n");
    return 0;
}

Can someone help me in debugging this?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2016-06-05 00:42:04 -0600

berak gravatar image

please do not use the 3.0 beta docs, theyre outdated, but master

the flag you're looking for is simply: cv::IMWRITE_PNG_COMPRESSION (all flags now in cv namespace, without CV_ prefix)

edit flag offensive delete link more

Comments

Thank you so much for the help

niranjan.kotha gravatar imageniranjan.kotha ( 2016-06-05 11:58:27 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-05 00:00:26 -0600

Seen: 4,795 times

Last updated: Jun 05 '16