Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to change the background with opencv

Shulou Source: shulou.com Published: 2022-06-01 05:22:50 09月14日 Update

This article shows you how to use opencv to change the background. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something from the detailed introduction of this article.

Let's get to the point. After changing new clothes, I want to change my wife. After changing my name, I naturally want to change my avatar! So I went to find an opencv logo, and then used PPT to match a word, and there it was:

What was going on with this green portrait? The color really reminded me of something bad, so I thought I'd replace the background with Iron Man's vintage gold.

The idea is, first of all, to pull out the three circles of opencv, as follows:

Note that the white (255) of the binary image above is what we really dig out, that is, the background is dug out as a pickled film.

This step is the key, that is, image segmentation, this image is very simple, I can directly use HSV color space can be separated out, can also threshold ah, watershed and so on, various segmentation.

Mat srcImage, HSVImage; srcImage = imread("icon.jpg");imshow("original icon", srcImage);cvtColor(srcImage, HSVImage, COLOR_BGR2HSV);//BGR to HSV//Split matting based on HSV Mat binary_Mask;//BinaryImage inRange(HSVImage, Scalar(70, 100, 100), Scalar(115, 255, 255), binary_Mask);//BinaryImage imshow("binaryImage," binary_Mask);

Then customize a background, which is the old gold mentioned above:

//customize a background Mat backImage(srcImage.size(), CV_8UC3, Scalar(65, 168, 214));//master gold imshow("background image", backImage);

Then we mix the marinated film with the new background and original image, and it's OK:

//mix background, marinade, original image backImage.copyTo(srcImage, binary_Mask); imshow("Result image", srcImage); imwrite("New avatar.jpg", srcImage); The above content is how to change the background with opencv. Have you learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.

Tags: Background that is my lord color that is content original picture image icon avatar skills knowledge mixing concise concise three bad things key watershed Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Shulou Tech Info Linux Shulou Information