2024-10-06 12:39:43 发布
网友
我正在尝试处理有彩色背景的矩形文本块的图像
请看下面的原始图片-我需要所有文本和数字为黑色,所有背景为白色,以便于阅读文本
我正在考虑一个灰度版本和它的对立面,找到彩色背景的区域,并使用反转图片中的区域替换灰度中的相同区域。但我找不到感兴趣的矩形(图中为蓝色和黄色)
有没有办法在整个图像中以白色背景将所有文本变为黑色
我能够检测到彩色背景的轮廓(见下面的代码)-什么是确保这些块中的文本为黑色并将背景变为白色的最佳转换
谢谢大家!
`
# Make greyscale version and inverted, thresholded greyscale version gr = cv2.cvtColor(page_image,cv2.COLOR_BGR2GRAY) plt.figure(figsize = (30,30)) plt.imshow(gr) plt.show() blur = cv2.GaussianBlur(gr, (7,7), 0) thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Create rectangular structuring element and dilate kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2)) #5,5 dilate = cv2.dilate(thresh, kernel, iterations=4) # Find contours and draw rectangle cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] thr=(gr.shape[1]/2) print(thr) for c in cnts: x,y,w,h = cv2.boundingRect(c) if w>thr and h>20 : cv2.rectangle(gr, (x,y ), (x+w,y+h ), (36,255,12), 2)
就你而言
1. get connected components (connected components, MSER, region growing, ..., etc.) 2. thresh by width. # half the size of image would do in the example. long (background / boxes / lines) : white short (letters) : black
我能够检测到彩色背景的轮廓(见下面的代码)-什么是确保这些块中的文本为黑色并将背景变为白色的最佳转换
谢谢大家!
`
`
就你而言
相关问题 更多 >
编程相关推荐