Friday, 4 July 2014

opencv(c++) creates transparent images

this code is going to load an image and apply canny.

then create a transparent mat , copy edges from the canny result and put onto this transparent mat.

Mat src = imread("/Users/xxx/Desktop/myGTs/2/2 ok copy/p51.png", 1);
Mat resultMat(src.rows,src.cols,CV_8UC4);

resultMat.setTo(Scalar(255,255,255,0));  so by default, all pixels should be white and full transparent.  So if no further processing you should see a pure transparent image.

Canny(srcx, srcx, 100, 200, 3);  apply canny to input image

for(int i = 0 ; i < srcx.rows; i++){
    for(int j = 0 ; j < srcx.cols; j++){
        uchar temp =<uchar>(i,j);
        if(temp > 1){ an edge pixel
        Vec4b intensity =<Vec4b>(i,j); get the coorespondent pixels
        intensity[0] = intensity[0] - 15; draw on the result mat.
        intensity[1] = intensity[1] - 15;
        intensity[2] = intensity[2] - 15;
        intensity[3] = 255; make the edge pixels fully untransparent.<Vec4b>(i,j) = intensity;

bool write_success = cv::imwrite("/Users/xxx/Desktop/xddff.png", resultMat); output it.

the most important parts are:
1. all pixels should be initialized by 255,255,255,0 (0 means full transparent)
2. the output fill should be png format.  jpeg doesn't support it.

No comments:

Post a comment