Canny正在检测除道路以外的所有地方的边缘。 这是原始图像。 https://imgur.com/a/O4ZVvb6
这是示例图像。 https://imgur.com/a/AqENNbq
我已经尝试过使用Canny阈值。 并应用高斯模糊,中值模糊,bilateralFilter,
我也尝试过先使用黑白图像阈值拟合器,但是问题是地形会稍微改变颜色并导致问题。
import numpy as np
from PIL import ImageGrab
import cv2
import time
def screen_record():
last_time = time.time()
while(True):
# record upper left corner of screen to get the image.
printscreen = np.array(ImageGrab.grab(bbox=(0,40,1098,728)))
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
color = cv2.cvtColor(printscreen, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(color, 20, 100)
cv2.imshow('edges',canny)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
screen_record()
我希望只有一条实线。 像下面的图像。 https://imgur.com/a/09vTE9e
答案 0 :(得分:1)
计算机不像人类那样“看”东西!假设您更关心检测“道路”,而不是为什么Canny滤镜无法“工作”,我只是将红色与绿色通道进行比较,从而在道路和草地之间(在这种类型的图像中)实现了很好的分离。例如:
img_bgr = cv2.imread('road.png')
delta = img_bgr[:,:,1] - img_bgr[:,:,2]
delta = cv2.blur(delta, (200, 200))
road = delta > 128
给我:
即True
值(白色)是道路,False
值是草。比起Canny的“台词”,这应该更容易解释。
这当然会在其他地方做一些奇怪的事情,但可能足以让您入门
答案 1 :(得分:1)
您可以同时使用打开和关闭操作来过滤canny
中的嘈杂边缘。代码如下。
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('vLnzxEs.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 20, 100)
# visualize the canny image
plt.figure
plt.imshow(canny, cmap='gray')
#decleare kernel for closing and opening operations
kernel = np.ones((75,75),np.uint8)
# apply closing and opening
closing = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
# visualize the opening
plt.figure
plt.imshow(opening, cmap='gray')
输出类似于