In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to extract ROI regions in Python+OpenCV digital image processing. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
1. Realization principle
First, the original RGB color image is converted into the image in the hsv color space through the cv.cvtColor () function, then the Mask of the ROI region is obtained by the cv.inRange () function, and finally the ROI region is extracted by the cv.bitwise () function.
2. A brief description of the functions used
(1) cv.cvtColor (img, cv.COLOR_BGR2HSV) function
Img is the original image to be converted into color space.
Cv.COLOR_BGR2HSV converts the original RGB color space into HSV color space.
(2) cv.inRange (hsv, (h_min, s_min, v_min), (h_max, s_max, v_max)) function
The cv.inRange function can obtain a binary mask diagram of different colors by setting different h, s, v min and max thresholds. The following is a table of thresholds for each color:
(3) cv.bitwise_and (img1, img2, mask), cv.bitwise_or (img1, img2, mask) and cv.bitwise_not (img)
The first function is the bitwise and operation function. The three components of img1 and img2 in the area of mask are bitwise and operated respectively. The second function is a bitwise or operation function. The three components of img1 and img2 in the area of mask are bitwise or operated respectively. The third function is the bitwise inverse operation function, which carries out the bitwise inverse operation of the three components of img in R _ Magi G ~ B respectively.
(4) cv.add (img1, img2) function
To add img1 and img2, img1 and img2 must be the same size.
3. Code implementation process
The original picture is as follows:
As shown in the picture, cartoon characters should be extracted from the picture and pasted on other backgrounds.
(1) read the original image src = cv.imread ('person.jpg') cv.imshow (' src', src)
(2) get maskhsv = cv.cvtColor (src, cv.COLOR_BGR2HSV) # convert to hsv color style mask = cv.inRange (hsv, (35,43,46), (99,255,255)) # generate maskcv.imshow ('mask1', mask) using inRange
Since the background is green, you can extract the mask of the green background. The values of green and cyan can be found from the above table. After setting the parameters, you can get mask (the white area is the mask area):
Note: if you get the mask with the mask as the background, we need to get the mask of the character.
(3) obtain the mask of a character
Through logical non-operation inversion, the mask area (white area) of the character can be obtained:
Mask = cv.bitwise_not (mask) cv.imshow ('mask2', mask)
(4) acquire characters
The original image and the original image can be obtained by logic and operation in the mask area.
Timg1 = cv.bitwise_and (src, src, mask=mask) cv.imshow ('timg1', timg1)
The above operations extract the ROI (cartoon character) area of the image, which is described below to paste the extracted map to other backgrounds.
(5) create a blue background image of the same size as the original image background = np.zeros (src.shape, src.dtype) background [:,: 0] = 255 (6) to get the blue background maskmask = cv.bitwise_not (mask) dst = cv.bitwise_or (timg1, background, mask=mask) cv.imshow ('dst1', dst)
(7) paste the figure picture on the blue background
Dst = cv.add (dst, timg1) cv.imshow ('dst2', dst)
4. The whole code import cv2 as cvimport numpy as npsrc = cv.imread ('person.jpg') cv.imshow (' src', src) hsv = cv.cvtColor (src, cv.COLOR_BGR2HSV) # converted to hsv color style mask = cv.inRange (hsv, (35,43,46), (99,255,255)) # using inRange to generate maskcv.imshow ('mask1', mask) cv.imwrite (' mask1.jpg') Mask) # get maskmask = cv.bitwise_not (mask) cv.imshow ('mask2', mask) cv.imwrite (' mask2.jpg', mask) timg1 = cv.bitwise_and (src, src, mask=mask) cv.imshow ('timg1', timg1) cv.imwrite (' timg1.jpg', timg1) # generate background background = np.zeros (src.shape, src.dtype) background [:,: 0] = 25 paste characters into the background mask= cv.bitwise_not (mask) dst = cv.bitwise_or (timg1, background, mask=mask) cv.imshow ('dst1', dst) cv.imwrite (' dst1.jpg', dst) dst = cv.add (dst, timg1) cv.imshow ('dst2', dst) cv.imwrite (' dst2.jpg', dst) cv.waitKey (0) cv.destroyAllWindows () above is how to extract ROI regions in Python+OpenCV digital image processing The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.