Get the App
SLTechnology News&Howtos  ›  Development  › 

How to replace the background of AI photos with OpenCV

Shulou Source: shulou.com Published: 2022-06-02 03:30:19 09月28日 Update

This article mainly introduces "OpenCV how to achieve AI photo background replacement" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "OpenCV how to achieve AI photo background replacement" article can help you solve the problem.

Picture

1) attached main program # Daily zoom rows,cols, channels = img_back.shapeimg_back = cv2.resize (img_back, None, fx=0.7, fy=0.7) cv2.imshow ('img_back', img_back) rows,cols, channels = img.shapeimg = cv2.resize (img, None, fx=0.4, fy=0.4) cv2.imshow (' img', img) rows,cols, channels = img.shape # rows,cols must be the foreground image finally After traversing the image, you need to use # conversion hsvhsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV) # to get masklower_blue = np.array ([78,43,46]) upper_blue = np.array ([110,255,255]) mask = cv2.inRange (hsv, lower_blue, upper_blue) cv2.imshow ('Mask', mask) # corrosion expansion erode = cv2.erode (mask, None, iterations=1) cv2.imshow (' erode', erode) dilate = cv2.dilate (erode, None) Iterations=1) cv2.imshow ('dilate', dilate) # ergodic replacement center = [50,50] # position in the new background picture for i in range (rows): for j in range (cols): if dilate [I, j] = 0: # 0 represents the black dot img_back [center [0] + I, center [1] + j] = img [I, j] # replace the color here Is the BGR channel cv2.imshow ('res', img_back) cv2.waitKey (0) cv2.destroyAllWindows ()

All right! The above matting to change the background is the effect of this code!

2) display other

Let's take a look at another matting app I've been using recently. This matting effect is actually better than my code.

2.1 I won't say that's the original picture-can you guess?

2.2 from comics to reality

2.3 what background do you want to exchange for what background

This is the end of the introduction on "how to replace the background of AI photos by OpenCV". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Background picture photo knowledge code effect industry different practical main program representative location content prospect original picture practicality practicality that is article method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Linux Huawei Shulou Information