import cv2 as cv
import matplotlib.pyplot as plt
img1 = cv.imread(r"C:\Users\Administrator\Desktop\logo.png")
rows,cols = img1.shape[0:2]
img2 = cv.imread(r"C:\Users\Administrator\Desktop\Messi.jpg")
roi = img2[0:rows,0:cols]
img1_gray = cv.cvtColor(img1,cv.COLOR_BGR2GRAY)
ret,img1_thres = cv.threshold(img1_gray,200,255,cv.THRESH_BINARY_INV)
img1_fg =cv.add(img1,img1,mask=img1_thres) #拿到logo图案的前景
The original picture is as follows:I want to snap off the picture in the upper left corner and use it as material
This completes all the steps of cutting out pictures and making materials.
Next, explain the code in detail:
img1_gray = cv.cvtColor(img1,cv.COLOR_BGR2GRAY)
Here the image is converted from the original image into a grayscale image
ret,img1_thres = cv.threshold(img1_gray,200,255,cv.THRESH_BINARY_INV)
The function of threshold function:
1. Remove noise, such as filtering image points with very small or large pixel values.
2. Threshold function python version prototype:
3.retval, dst = cv.threshold( src, thresh, maxval, type[, dst] ) a>
Parameter Description:
src: original image. dst: result image.
thresh: current threshold. maxVal: maximum threshold, generally 255.
cv.THRESH_BINARY_INV
The function of HRESH_BINARY_INV is opposite to that of THRESH_BINARY. Values greater than thresh are set to 0, and values less than or equal to thresh are set to maxval.
2.This is what it looks like after conversion:
3. Image fusion
img1_fg =cv.add(img1,img1,mask=img1_thres)
Finally, we got the black background cutout material.
Isn’t it perfect? You can also give it a try