Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- image = cv2.imread('dogs.png')
- # 转换为灰度图
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # 边缘检测
- edges = cv2.Canny(gray, 100, 150)
- # 轮廓
- contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- # 掩膜
- mask = np.zeros_like(gray)
- cv2.drawContours(mask, contours, -1, (255), thickness=cv2.FILLED)
- mask = cv2.GaussianBlur(mask, (21, 21), 0)
- # 模糊整个图像
- blurred = cv2.GaussianBlur(image, (51, 51), 0)
- # 创建掩膜和反掩膜的三通道版本
- mask_3ch = cv2.merge([mask, mask, mask])
- inverse_mask = cv2.bitwise_not(mask_3ch)
- # 使用掩膜将模糊的背景和原始图像结合
- background = cv2.bitwise_and(blurred, mask_3ch)
- foreground = cv2.bitwise_and(image, inverse_mask)
- result = cv2.add(background, foreground)
- cv2.imshow('1', edges)
- cv2.imshow('Detected Potholes', result)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- # 保存结果图像
- cv2.imwrite('result_chose.png', result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement