洗掉棋盤圖案時遇到問題。我正在使用 cv2.Threshold 但它也選擇了意外像素(紅色標記)。
import cv2
import numpy as np
input = cv2.imread('image.png')
ret, logo_mask = cv2.threshold(input[:,:,0], 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
cv2.imshow(logo_mask)
輸入影像:

輸出影像:

任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
獲得涵蓋所有案例的完美結果具有挑戰性。
以下解決方案假設白色棋盤格顏色為 (255, 255, 255),灰色為 (230, 230, 230)。
另一個假設是影像其他部分中具有該特定顏色的簇非常小。
我們可以使用以下階段:
- 找到顏色為 (255, 255, 255) 和 (230, 230, 230) 的“白色蒙版”和“灰色蒙版”。
- 使用按位或創建統一掩碼。
- 查找輪廓,并從蒙版中洗掉小輪廓(假設為“噪聲”)。
代碼示例:
import cv2
import numpy as np
input = cv2.imread('image.png')
white_mask = np.all(input == 255, 2).astype(np.uint8)*255 # cv2.inRange(input, (255, 255, 255), (255, 255, 255))
gray_mask = np.all(input == 230, 2).astype(np.uint8)*255 # gray_mask = cv2.inRange(input, (230, 230, 230), (230, 230, 230))
mask = cv2.bitwise_or(white_mask, gray_mask) # Create unified mask
ctns = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2] # Find contours
# Remove small contours from mask
for c in ctns:
area = cv2.contourArea(c) # Find the area of each contours
if (area < 10): # Ignore small contours (assume noise).
cv2.drawContours(mask, [c], 0, 0, -1)
mask = cv2.dilate(mask, np.ones((3, 3), np.uint8)) # Dilate the mask - "cosmetics"
output = cv2.copyTo(input, 255-mask) # Put black color in the masked part.
# Show images for testing
cv2.imshow('input', input)
cv2.imshow('mask', mask)
cv2.imshow('output', output)
cv2.waitKey()
cv2.destroyAllWindows()
white_mask:

gray_mask:

mask:

output:

如果前景部分有較大的白色區域或灰色區域,上述解決方案可能不起作用。
我想到了一個只查找與白色和灰色矩形邊界重疊的區域的程序。
它不起作用,因為樹枝之間有小部分被排除在外。
下面的代碼可能會給你啟發:
import cv2
import numpy as np
input = cv2.imread('image.png')
#ret, logo_mask = cv2.threshold(input[:,:,0], 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
white_mask = np.all(input == 255, 2).astype(np.uint8)*255 # cv2.inRange(input, (255, 255, 255), (255, 255, 255))
gray_mask = np.all(input == 230, 2).astype(np.uint8)*255 # gray_mask = cv2.inRange(input, (230, 230, 230), (230, 230, 230))
cv2.imwrite('white_mask.png', white_mask)
cv2.imwrite('gray_mask.png', gray_mask)
# Apply opening for removing small clusters
opened_white_mask = cv2.morphologyEx(white_mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
opened_gray_mask = cv2.morphologyEx(gray_mask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
cv2.imwrite('opened_white_mask.png', opened_white_mask)
cv2.imwrite('opened_gray_mask.png', opened_gray_mask)
white_mask_shell = cv2.dilate(opened_white_mask, np.ones((3, 3), np.uint8)) - opened_white_mask # Dilate white_mask and keep only the "shell"
gray_mask_shell = cv2.dilate(opened_gray_mask, np.ones((3, 3), np.uint8)) - opened_gray_mask # Dilate gray_mask and keep only the "shell"
white_mask_shell = cv2.dilate(white_mask_shell, np.ones((3, 3), np.uint8)) # Dilate the "shell"
gray_mask_shell = cv2.dilate(gray_mask_shell, np.ones((3, 3), np.uint8)) # Dilate the "shell"
cv2.imwrite('white_mask_shell.png', white_mask_shell)
cv2.imwrite('gray_mask_shell.png', gray_mask_shell)
overlap_shell = cv2.bitwise_and(white_mask_shell, gray_mask_shell)
cv2.imwrite('overlap_shell.png', overlap_shell)
dilated_overlap_shell = cv2.dilate(overlap_shell, np.ones((17, 17), np.uint8))
mask = cv2.bitwise_or(cv2.bitwise_and(white_mask, dilated_overlap_shell), cv2.bitwise_and(gray_mask, dilated_overlap_shell))
cv2.imshow('input', input)
cv2.imshow('white_mask', white_mask)
cv2.imshow('gray_mask', gray_mask)
cv2.imshow('white_mask', white_mask)
cv2.imshow('gray_mask', gray_mask)
cv2.imshow('opened_white_mask', opened_white_mask)
cv2.imshow('opened_gray_mask', opened_gray_mask)
cv2.imshow('overlap_shell', overlap_shell)
cv2.imshow('dilated_overlap_shell', dilated_overlap_shell)
cv2.imshow('mask', mask)
cv2.waitKey()
cv2.destroyAllWindows()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535514.html
標籤:Python打开简历
