In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge of this article "how to use a line of python code to achieve grayscale matting" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use a line of python code to achieve grayscale matting" article.
One line of code to realize grayscale matting
Matting is the most basic skill of ps. Using python, you can realize grayscale matting with one line of code.
The basic algorithm is the Otsu method to determine the threshold of image binarization, which divides the image into two parts: background and foreground, and maximizes the inter-class variance between background and foreground.
In the specific theory part, you can search the Otsu method to understand it. In the code part, as long as you select the parameter THRESH_OTSU in the function that calls threshold, you can call Otsu method segmentation.
Ret1, th2 = cv2.threshold (gray, 0,255, cv2.THRESH_OTSU)
All the code is as follows. Compare the original image, histogram and segmented diagram together.
Image = cv2.imread ("pictures/dajin.jpg") # first converted into grayscale images gray = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY) plt.subplot, plt.imshow (image, "image") plt.title ("source image"), plt.xticks ([]), plt.yticks ([]) plt.subplot (132), plt.hist (image.ravel (), 256) plt.title ("Histogram") ret1, th2 = cv2.threshold (gray, 0,255) Cv2.THRESH_OTSU) # method selection is THRESH_OTSUplt.subplot, plt.imshow (th2, "gray") plt.title ("OTSU,threshold is" + str (ret1)), plt.xticks ([]), plt.yticks ([]) plt.show ()
The result of matting is as follows
However, this algorithm is very sensitive to noise and target size, and the segmentation effect is not good when the background and foreground pixels are close.
Python matting program source code
Zero basic learning python, listened to several free introductory lessons, impromptu wrote a program, as an exercise to achieve matting.
The manual operation of matting is that in the PS software, the picture is copied and pasted, the main body is retained, and the surrounding is erased as the background.
Erased area, transparent format. Save to png format file after success (png format allows you to store layers and keep transparent areas). Manual operation is laborious and time-consuming, improper operation needs to be returned and re-erased, and the erasure precision varies with the operator's proficiency. It takes a few minutes for a skilled person to erase the periphery of a character object.
Python language, it is convenient to call the third party function package to achieve the purpose of matting, the perimeter of the main object is relatively smooth, and the program execution only takes a few seconds, which greatly saves manpower and time.
Prepare for
Matting requires a third-party feature package removebg, which can be operated only after installation. To install the function package, you need to install it in the dos window of Windows, the win+r hotkey calls up the run dialog box, enter the cmd command, and open the dos window. Dos window input:
Pip install removebg (waiting for download and installation) os package and time package are included in the system and do not need to be installed.
The RemoveBg function call requires an APIKEY argument (password), which is a multi-digit string. You need to register at the url = www.remove.bg website to get a free APIKEY code. Awkwardly, only 50 pictures are allowed to be processed per day.
Program conception
1) it is required to allow the processing of multiple jpg format images, show the progress of the run, appropriate user interaction, and convert to exe publishing.
2) key code, there are only two lines of code to deal with a single image:
Rmbg = RemoveBg ('apiKey code', 'error message record file name') # 56 line
Send a request to the website to get the API calling object and return the object
Rmbg.remove_background_from_img_file ('. / picture/' + item) # 74 lines
Using the method of rmbg object, pick a picture.
3) check the location of the picture (if the folder exists, create the folder and exit if it does not exist)
4) check if there are any files in the folder, display information and exit if not, then rebuild the table to eliminate non-jpg format files.
5) checking and processing matting defines multiple functions to avoid confusion in the program and is not easy to read
Readkey ()-- read the APIkey code file
Founddir ()-check whether the folder where the picture files are stored exists and send a prompt
Cutout ()-matting, including checking the existence of picture files, and eliminating non-jpg format files, and prompting
6) the main program segment, at the end, clearly indicates whether the operation is ready, whether the interaction continues, or whether the interaction is cancelled.
7) package into exe file (abbreviated)
The screenshot of the complete code is as follows, and each statement has functional comments.
1 reference to the processing package
2 define a function to read the apikye code value from the attached file
3. A function that defines a search directory and returns true / false.
4 define the main function of matting processing
4 main program
The above is about "how to use a line of python code to achieve grayscale matting" of this article, I believe we all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more related knowledge content, 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.
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.