Ask Your Question
0

Alpha channel changed on OpenCV python

asked 2018-02-19 10:54:42 -0500

developer2000 gravatar image

updated 2018-02-19 11:43:47 -0500

LBerger gravatar image

I have an imgae with alpha channel. The alpha value is equal to 1. for all foreground pixels and is equal to 0 for all background pixels. When I read it using python OpenCV, I remark that the Alpha value is changed to 1 (255) for all pixels. I used this script to read the image. I think it is a bug on OpenCV, I have checked on mathematica and the value are coorect!

image withAlpha=cv2.imread("lace1.png",-1)# -1 or cv2.IMREAD_UNCHANGED

C:\fakepath\3.png

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-02-19 11:51:04 -0500

LBerger gravatar image

updated 2018-02-19 11:53:32 -0500

Please add a test program : everybody will be able to check if problem exist. Give opencv version and platform too

Now in C++ no problem :

    Mat w(3, 3, CV_8UC4, Scalar(1, 2, 3, 0));
    w.at<Vec4b>(0, 0) = Scalar(0, 0, 0, 255);
    w.at<Vec4b>(1, 0) = Scalar(0, 0, 0, 128);
    w.at<Vec4b>(2, 0) = Scalar(0, 0, 0, 1);
    cout << w << endl;
    imwrite("w.png", w);
    cout << "After imwrite and imread\n";
    Mat x = imread("w.png", IMREAD_UNCHANGED);
    cout << x;

results :

[  0,   0,   0, 255,   1,   2,   3,   0,   1,   2,   3,   0;
   0,   0,   0, 128,   1,   2,   3,   0,   1,   2,   3,   0;
   0,   0,   0,   1,   1,   2,   3,   0,   1,   2,   3,   0]
After imwrite and imread
[  0,   0,   0, 255,   1,   2,   3,   0,   1,   2,   3,   0;
   0,   0,   0, 128,   1,   2,   3,   0,   1,   2,   3,   0;
   0,   0,   0,   1,   1,   2,   3,   0,   1,   2,   3,   0]
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-02-19 10:54:42 -0500

Seen: 435 times

Last updated: Feb 19 '18