面具

部分清潔:

uj5u.com熱心網友回復:
獲取邊緣
擴張以關閉線條
霍夫線檢測線
過濾掉非水平線
修補面具
獲得邊緣
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize=3)

- 擴張以關閉線條
img_dilation = cv2.dilate(edges, np.ones((3,3), np.uint8), iterations=1)

- 霍夫線檢測線
lines = cv2.HoughLinesP(
img_dilation, # Input edge image
1, # Distance resolution in pixels
np.pi/180, # Angle resolution in radians
threshold=100, # Min number of votes for valid line
minLineLength=5, # Min allowed length of line
maxLineGap=10 # Max allowed gap between line for joining them
)
- 使用斜率過濾掉非水平線。
lines_list = []
for points in lines:
x1,y1,x2,y2=points[0]
lines_list.append([(x1,y1),(x2,y2)])
slope = ((y2-y1) / (x2-x1)) if (x2-x1) != 0 else np.inf
if slope <= 1:
cv2.line(mask,(x1,y1),(x2,y2), color=(255, 255, 255),thickness = 2)
- 修補面具
result = cv2.inpaint(image,mask,3,cv2.INPAINT_TELEA)

完整代碼:
import cv2
import numpy as np
# Read image
image = cv2.imread('input.jpg')
mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
# Convert image to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# Use canny edge detection
edges = cv2.Canny(gray,50,150,apertureSize=3)
# Dilating
img_dilation = cv2.dilate(edges, np.ones((3,3), np.uint8), iterations=1)
# Apply HoughLinesP method to
# to directly obtain line end points
lines = cv2.HoughLinesP(
img_dilation, # Input edge image
1, # Distance resolution in pixels
np.pi/180, # Angle resolution in radians
threshold=100, # Min number of votes for valid line
minLineLength=5, # Min allowed length of line
maxLineGap=10 # Max allowed gap between line for joining them
)
lines_list = []
for points in lines:
x1,y1,x2,y2=points[0]
lines_list.append([(x1,y1),(x2,y2)])
slope = ((y2-y1) / (x2-x1)) if (x2-x1) != 0 else np.inf
if slope <= 1:
cv2.line(mask,(x1,y1),(x2,y2), color=(255, 255, 255),thickness = 2)
result = cv2.inpaint(image,mask,3,cv2.INPAINT_TELEA)
uj5u.com熱心網友回復:
所以,我看到在與紙分開的圖紙上作業會導致更好的結果。我使用 MORPH_CLOSE 處理紙張,使用 MORPH_OPEN 處理內部的線條。我希望你的女兒喜歡它:)
img = cv2.imread(r'E:\Downloads\i0RDA.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Remove horizontal lines
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,81,17)
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1))
# Using morph close to get lines outside the drawing
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, horizontal_kernel, iterations=3)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
mask = np.zeros(gray.shape, np.uint8)
for c in cnts:
cv2.drawContours(mask, [c], -1, (255,255,255),2)
# First inpaint
img_dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)


gray_dst = cv2.cvtColor(img_dst, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_dst, 50, 150, apertureSize = 3)
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
# Using morph open to get lines inside the drawing
opening = cv2.morphologyEx(edges, cv2.MORPH_OPEN, horizontal_kernel)
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
mask = np.uint8(img_dst)
mask = np.zeros(gray_dst.shape, np.uint8)
for c in cnts:
cv2.drawContours(mask, [c], -1, (255,255,255),2)
# Second inpaint
img2_dst = cv2.inpaint(img_dst, mask, 3, cv2.INPAINT_TELEA)

uj5u.com熱心網友回復:

uj5u.com熱心網友回復:
一種方法是定義僅掩蓋所需細節的 HSV 掩碼(在這種情況下,它們是人、閃光和簽名)。
獲得合適的蒙版后,簡單地對未蒙版部分的影像進行模糊處理。這是 HSV 掩碼的下界0, 0, 160和上界的結果116, 30, 253:

這是影像的處理,按以下順序:
(原始影像),(蒙版),
(模糊影像),(得到的蒙版影像):

代碼:
import cv2
import numpy as np
img = cv2.imread("input.jpg")
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 160])
upper = np.array([116, 30, 253])
mask = cv2.inRange(img_hsv, lower, upper)
img_blurred = cv2.GaussianBlur(img, (31, 31), 10)
img_blurred[mask == 0] = img[mask == 0]
cv2.imshow("Result", img_blurred)
cv2.waitKey(0)
正如你所看到的,這個人頭發上的波浪線比預期的要細。這可以通過二進制掩碼的一些侵蝕迭代來修復(只需添加到變數mask = cv2.erode(mask, np.ones((3, 3)), 3)定義下的代碼中mask) :
import cv2
import numpy as np
img = cv2.imread("input.jpg")
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 160])
upper = np.array([116, 30, 253])
mask = cv2.inRange(img_hsv, lower, upper)
mask = cv2.erode(mask, np.ones((3, 3)), 3)
img_blurred = cv2.GaussianBlur(img, (31, 31), 10)
img_blurred[mask == 0] = img[mask == 0]
cv2.imshow("Result", img_blurred)
cv2.waitKey(0)
輸出:

再次按相同順序執行該程序:

I've added a post here to include the program that you can use to tweak the values and see the results in real-time, in case you have other images you want to apply the same method to.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443680.html
