PNG transparent channel lost after perspective transformation. [closed]

asked 2015-09-25 00:30:43 -0600

realkill gravatar image

I am perspective transform a RGBA PNG image (depth:8, channel: 4, background: transparent), but the transparent background turns into black after transformation. What's wrong with the following code?

I am a new guy to OpenCV, thank you!

void cv_test(void) {

int i, t, ret;
CvPoint2D32f srcQuad[4];
CvPoint2D32f dstQuad[4];
CvMat *warp_matrix;
IplImage *src, *dst;

src = cvLoadImage("c:\\aqxj.png", CV_LOAD_IMAGE_UNCHANGED);
if(!src) {
    fprintf(stderr, "src:%p", src);
    return;
}

dst = cvCreateImage(cvSize(src->width, src->height), 8, 4);
//dst->origin = src->origin;
ret = cvSaveImage("/home/metalwood/s.png", dst);
cvZero(dst);

fprintf(stderr, "dst width:%d, height:%d.\n", dst->width, dst->height);

srcQuad[0].x = 0; /* Top left. */
srcQuad[0].y = 0;
srcQuad[1].x = src->width - 1; /* Top right. */
srcQuad[1].y = 0;
srcQuad[2].x = 0; /* Bottom left. */
srcQuad[2].y = src->height - 1;
srcQuad[3].x = src->width - 1; /* Bottom right. */
srcQuad[3].y = src->height - 1;

dstQuad[0].x = src->width * 0.05;
dstQuad[0].y = src->height * 0.33;
dstQuad[1].x = src->width * 0.9;
dstQuad[1].y = src->height * 0.25;
dstQuad[2].x = src->width * 0.2;
dstQuad[2].y = src->height * 0.7;
dstQuad[3].x = src->width * 0.8;
dstQuad[3].y = src->height * 0.9;

warp_matrix = cvCreateMat(3, 3, CV_32FC1);
cvGetPerspectiveTransform(srcQuad, dstQuad, warp_matrix);
cvWarpPerspective(src, dst, warp_matrix);

cvNamedWindow("Perspective_Warp", 1);
cvShowImage("Perspective_Warp", dst);
cvWaitKey();

//Save disk
ret = cvSaveImage("c:\\save.png", src);
fprintf(stderr, "ret:%d.\n", ret);

cvReleaseImage(&dst);
cvReleaseMat(&warp_matrix);

}

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2015-10-12 20:48:59.867853

Comments

3

please do not use the deprecated c-api, opencv moved away from that half a decade ago (and so should you)

you will have to use cv::Mat and functions from namespace cv, not IplImage or cv*Functions

berak gravatar imageberak ( 2015-09-25 00:37:03 -0600 )edit
2

ps, i made a quick check using warpPerspective , and it correctly preserves alpha (if present) , so have a look at related tutorials and update your code !

berak gravatar imageberak ( 2015-09-25 00:59:02 -0600 )edit

Thanks, I used cv::imread(path, CV_LOAD_IMAGE_UNCHANGED) to resolve this question, thanks a lot.

realkill gravatar imagerealkill ( 2015-10-12 20:44:55 -0600 )edit