Extracting transparent background of an image with opencv -
Extracting transparent background of an image with opencv -
i have got mask calculated in grab_cut(which calculates foreground). want extract background leaving foreground transparent. manage using next code in order extract foreground(background transparent). how possible opposite?
int border = 20; int border2 = border + border; cv::rect rectangle(border,border,image.cols-border2,image.rows-border2); cv::mat result; // segmentation result (4 possible values) cv::mat bgmodel,fgmodel; / cv::grabcut(image, // input image result, // segmentation result rectangle,// rectangle containing foreground bgmodel,fgmodel, // models 1, // number of iterations cv::gc_init_with_rect); // utilize rectangle cv::compare(result,cv::gc_pr_fgd,result,cv::cmp_eq); cv::mat foreground(image.size(),cv_8uc3,cv::scalar(255,255,255)); image.copyto(foreground,result); // bg pixels not copied cv::rectangle(image, rectangle, cv::scalar(255,255,255),1); cv::imwrite(argv[2], foreground); cv::imwrite(argv[3], image); mat dst;//(src.rows,src.cols,cv_8uc4); mat tmp,alpha; cvtcolor(foreground,tmp,cv_bgr2gray); threshold(tmp,alpha,100,255,thresh_binary); mat rgb[3]; split(foreground,rgb); mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha}; merge(rgba,4,dst); imwrite("dst.png",dst); basically think ve got alter lines:
cv::mat foreground(image.size(),cv_8uc3,cv::scalar(255,255,255)); image.copyto(foreground,result); // bg pixels not copied how is possible select rest of image opposite of result?
just invert mask in:
cv::mat background(image.size(),cv_8uc3,cv::scalar(255,255,255)); image.copyto(background, ~result); // fg pixels not copied opencv background foreground
Comments
Post a Comment