1,抠出屏的图
import cv2 from PIL import Image def getCoordinate(img): rectangle = [] gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 灰度图 ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # 二值化 element3 = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8)) # 设置膨胀和腐蚀操作 dilation = cv2.dilate(binary, element3, iterations=1) # 膨胀一次,让轮廓突出 contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) # 检测轮廓 for contour in contours: x, y, w, h = cv2.boundingRect(contour) rectangle.append((x, y, x + w, y + h)) print(f'rectangle: {rectangle}') return rectangle def savePic(rectangle): for i in range(len(rectangle)-1): imgPath = rf"E:\Work\space\raspberry\test_{i}.jpg" # notes: 图片的扩展名要一致 im = Image.open(defaultImgPath) im = im.crop(rectangle[i]) # 对图片进行切割 im.crop(top_x, top_y, bottom_x, bottom_y) im.save(imgPath) if __name__ == '__main__': defaultImgPath = r"E:\Work\space\raspberry\test.jpg" img = cv2.imread(defaultImgPath) coordinateValue = getCoordinate(img) savePic(coordinateValue)View Code
2,
标签:__,学习,img,defaultImgPath,python,cv2,相机,im,rectangle From: https://www.cnblogs.com/147258llj/p/17483646.html