目錄
效果不理想
代碼:lunkuo_mohu.py
效果不理想
代碼:lunkuo_mohu.py
import cv2
import numpy as np
img_mask = cv2.imread(r'F:\project\ronghe\Poisson-Blending-main\mask_new.jpg', 0)
img_path = r'F:\project\ronghe\Poisson-Blending-main\res2.jpg'
image = cv2.imread(img_path)
edges = cv2.Canny(img_mask, threshold1=100, threshold2=200)
if 0:#輪廓膨脹
kernel = np.ones((5, 5), np.uint8)
# 向外擴(kuò)展(膨脹)
dilated = cv2.dilate(edges, kernel, iterations=2)
# 向內(nèi)擴(kuò)展(腐蝕)
eroded = cv2.erode(edges, kernel, iterations=2)
# 合并膨脹和腐蝕后的結(jié)果(實(shí)現(xiàn)同時(shí)向內(nèi)和向外擴(kuò)展)
edges = cv2.bitwise_or(dilated, eroded)
# 獲取圖像的高度和寬度
height, width = edges.shape
# 創(chuàng)建一個(gè)輸出圖像,初始時(shí)與原圖像相同
output_image = image.copy()
# 遍歷所有邊緣像素
for y in range(1, height - 1):
for x in range(1, width - 1):
if edges[y, x] != 0: # 檢查是否是邊緣像素
# 獲取該像素周圍8個(gè)鄰域像素(3x3鄰域內(nèi)的其他8個(gè)像素)
region = image[y - 1:y + 2, x - 1:x + 2]
# 計(jì)算鄰域像素的平均顏色(忽略中心點(diǎn))
neighbors = region[region != region[1, 1]] # 不包括中心點(diǎn)
avg_color = np.mean(image[neighbors], axis=(0, 1)) # 計(jì)算平均顏色
# 將該邊緣像素的顏色設(shè)置為平均顏色
output_image[y, x] = avg_color.astype(np.uint8)
# 顯示結(jié)果
cv2.imshow('edges', edges)
cv2.imshow('Edges Colored by Neighbors Average', output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()