Advertisement
wyx0311

电宇智控视觉组2_4

May 31st, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | Source Code | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # 读取图像
  5. image = cv2.imread('road1.jpg')
  6.  
  7. # 转换为灰度图
  8. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  9.  
  10.  # 轮廓检测
  11. blurred = cv2.GaussianBlur(gray, (5, 5), 0)
  12.  
  13. edges = cv2.Canny(blurred, 5, 50)
  14.  
  15. contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  16.  
  17. # 绘制轮廓并填充红色
  18. for contour in contours:
  19.     if cv2.contourArea(contour) > 2000:  # 过滤
  20.         cv2.drawContours(image, [contour], -1, (0, 0, 255), -1)
  21.  
  22. # 显示和保存结果图像
  23. cv2.imshow('1', edges)
  24. cv2.imshow('Detected Potholes', image)
  25. cv2.waitKey(0)
  26. cv2.destroyAllWindows()
  27.  
  28. cv2.imwrite('result_4.jpg', image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement