Advertisement
wyx0311

电宇智控视觉组2_5

May 31st, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | Source Code | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. image = cv2.imread('dogs.png')
  5.  
  6. # 转换为灰度图
  7. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  8.  
  9. # 边缘检测
  10. edges = cv2.Canny(gray, 100, 150)
  11.  
  12. # 轮廓
  13. contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  14.  
  15. # 掩膜
  16. mask = np.zeros_like(gray)
  17.  
  18. cv2.drawContours(mask, contours, -1, (255), thickness=cv2.FILLED)
  19.  
  20. mask = cv2.GaussianBlur(mask, (21, 21), 0)
  21.  
  22. # 模糊整个图像
  23. blurred = cv2.GaussianBlur(image, (51, 51), 0)
  24.  
  25. # 创建掩膜和反掩膜的三通道版本
  26. mask_3ch = cv2.merge([mask, mask, mask])
  27. inverse_mask = cv2.bitwise_not(mask_3ch)
  28.  
  29. # 使用掩膜将模糊的背景和原始图像结合
  30. background = cv2.bitwise_and(blurred, mask_3ch)
  31. foreground = cv2.bitwise_and(image, inverse_mask)
  32. result = cv2.add(background, foreground)
  33.  
  34. cv2.imshow('1', edges)
  35. cv2.imshow('Detected Potholes', result)
  36. cv2.waitKey(0)
  37. cv2.destroyAllWindows()
  38.  
  39. # 保存结果图像
  40. cv2.imwrite('result_chose.png', result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement