PNG transparent channel lost after perspective transformation. [closed]
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);
}
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
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 !
Thanks, I used cv::imread(path, CV_LOAD_IMAGE_UNCHANGED) to resolve this question, thanks a lot.