Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How C++ opencv uses grabCut algorithm to realize matting

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report