目录
效果不理想
代码: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)
# 向外扩展(膨胀)
dilated = cv2.dilate(edges, kernel, iterations=2)
# 向内扩展(腐蚀)
eroded = cv2.erode(edges, kernel, iterations=2)
# 合并膨胀和腐蚀后的结果(实现同时向内和向外扩展)
edges = cv2.bitwise_or(dilated, eroded)
# 获取图像的高度和宽度
height, width = edges.shape
# 创建一个输出图像,初始时与原图像相同
output_image = image.copy()
# 遍历所有边缘像素
for y in range(1, height - 1):
for x in range(1, width - 1):
if edges[y, x] != 0: # 检查是否是边缘像素
# 获取该像素周围8个邻域像素(3x3邻域内的其他8个像素)
region = image[y - 1:y + 2, x - 1:x + 2]
# 计算邻域像素的平均颜色(忽略中心点)
neighbors = region[region != region[1, 1]] # 不包括中心点
avg_color = np.mean(image[neighbors], axis=(0, 1)) # 计算平均颜色
# 将该边缘像素的颜色设置为平均颜色
output_image[y, x] = avg_color.astype(np.uint8)
# 显示结果
cv2.imshow('edges', edges)
cv2.imshow('Edges Colored by Neighbors Average', output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()