簡體   English   中英

在 Open CV 中更改部分圖像的背景顏色

[英]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.findContourscv2.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM