Get the App
SLTechnology News&Howtos  ›  Development  › 

How C++ opencv uses grabCut algorithm to realize matting

Shulou Source: shulou.com Published: 2022-05-31 17:38:23 09月21日 Update

Today, I would like to share with you how C++ opencv uses grabCut algorithm to achieve matting related knowledge points, detailed content, clear logic, I believe most people are too aware of this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

Preface

The grabCut algorithm makes use of the texture (color) information and boundary (contrast) information in the image, and can get better segmentation results with only a small amount of user interaction, which is similar to that of Shunfeng, the watershed, but the calculation speed is slow and the results are more accurate.

Usage: input a picture and mark some pixels belonging to the background or foreground. The algorithm will calculate the segmentation lines of the foreground and background in the whole image according to this local mark.

GrabCut function void grabCut (InputArray img, InputOutputArray mask, Rect rect, InputOutputArray bgdModel, InputOutputArray fgdModel, int iterCount, int mode = GC_EVAL) Img input image mask output mask rect user selected foreground rectangular area bgdModel output background image fgdModel output foreground image iterCount iterations mode is used to indicate what operation the function performs 2. Compare function

Compare function is mainly used for pixel-by-pixel comparison between two images.

Void compare (InputArray src1, InputArray src2, OutputArray dst, int cmpop); src1 original image 1 src2 original image 2 dst result image cmpop operation type 3, code # include#includeusing namespace std;using namespace cv;int main () {Mat img1; img1 = imread ("test2.jpg"); imshow ("original image", img1); Rect rect (84,84,406,318) Mat img2, bg, fg; grabCut (img1, img2, rect, bg, fg,1,GC_INIT_WITH_RECT); compare (img2, GC_PR_FGD, img2, CMP_EQ); imshow ("img2", img2); Mat img3 (img1.size (), CV_8UC3, Scalar (255,255,255); img1.copyTo (img3, img2); imshow ("img3", img3) WaitKey (0);}

Effect picture:

These are all the contents of this article entitled "how C++ opencv uses grabCut algorithm to achieve matting". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Images algorithms functions foreground knowledge articles results backgrounds outputs clips originals information pixels content tags users inputs different large similar Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Huawei NVidia macOS