![](/img/trans.png)
[英]Open CV OCR improve data extraction from color image with background
[英]Changing background color for part of image in Open CV
我能夠從具有白色背景的圖像中提取數據,而我無法提取具有黑色背景的數據。 如何獲取這些數據。
下面是我試過但沒有用的代碼..
#denoise
#For normal image
#img = cv2.fastNlMeansDenoisingColored(img, None, 3, 3, 7, 21)
#gray
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Morp operation
#kernel = np.ones((1, 1), np.uint8)
#img = cv2.dilate(img, kernel, iterations=1)
#img = cv2.erode(img, kernel, iterations=1)
一種使用cv2.findContours
和cv2.boudingRect
反轉大暗區顏色的方法
#denoise
#For normal image
img = cv2.fastNlMeansDenoisingColored(img, None, 3, 3, 7, 21)
#gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Morp operation
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
#Transforms gray to white
img[img > 215] = 255
binary = cv2.bitwise_not(img)
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in contours:
(x,y,w,h) = cv2.boundingRect(contour)
# Ignore small areas
if w*h < 500:
continue
# Invert colors
img[y:y+h, x:x+w] = ~img[y:y+h, x:x+w]
# Transforms gray to white
img[img > 215] = 255
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.