利用R,G,B三通道对图像分别生成mask并叠加,最终得到背景和书两种区域
实验室的学长TQL,这个三色道方法是他教我的
import os
import os.path as osp
import sys
import multiprocessing as mp
import numpy as np
import cv2
import matplotlib.pyplot as plt
from glob import glob
def get_box(im):
t = 40
epsilon=100
img_b = im[:,:,0]
img_g = im[:,:,1]
img_r = im[:,:,2]
_, mask_b = cv2.threshold(img_b, t, 255, 1)
_, mask_g = cv2.threshold(img_g, t, 255, 1)
_, mask_r = cv2.threshold(img_r, t, 255, 1)
mask = cv2.bitwise_not(cv2.bitwise_and(cv2.bitwise_and(mask_b, mask_g), mask_r))
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours=list(contours)
contours.sort(key=lambda x:cv2.contourArea(x), reverse=True)
cnt = cv2.approxPolyDP(contours[0], epsilon=epsilon, closed=True)
cnt = cv2.minAreaRect(cnt)
box = np.int0(cv2.boxPoin