Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 庭院内部花园检测(cv2)_Python_Image_Colors_Computer Vision_Cv2 - Fatal编程技术网

Python 庭院内部花园检测(cv2)

Python 庭院内部花园检测(cv2),python,image,colors,computer-vision,cv2,Python,Image,Colors,Computer Vision,Cv2,我想用篱笆和石头把花园里的草地和外面的草地分开。脚本应该能够绘制第二幅图像上可以看到的红线。 使用cv2应该是可能的 我尝试了一个我在车道检测系统中找到的代码,但不幸的是它对gras不起作用 gray = cv2.cvtColor(gras_image, cv2.COLOR_RGB2GRAY) blur = cv2.GaussianBlur(gray, (5,5), 0) canny = cv2.Canny(blur, 50, 200) 感谢您的帮助查看下面我的代码中的注释,了解如何在图像

我想用篱笆和石头把花园里的草地和外面的草地分开。脚本应该能够绘制第二幅图像上可以看到的红线。 使用cv2应该是可能的

我尝试了一个我在车道检测系统中找到的代码,但不幸的是它对gras不起作用

gray = cv2.cvtColor(gras_image, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
canny = cv2.Canny(blur, 50, 200)


感谢您的帮助

查看下面我的代码中的注释,了解如何在图像中选择您的院子

如果您的院子和非您的院子之间的边界颜色发生变化,这将不是一个可靠的解决方案。通过在GIMP中打开图像,并使用GIMP的颜色选择器工具选择右侧的棕色边框对象,我找到了一个合适的HSV范围。神奇的是,这种颜色也适用于灰色方块。通过使用颜色范围,或者甚至为棕色块和灰色块创建单独的遮罩,您可能会获得更好的效果

import cv2
import numpy as np

#load the image
image = cv2.imread("meadow.jpg")

#define the lower and upper bounds of colors to threshold for in the HSV color space.
hsv_lower = (35//2, 0, 120)
hsv_upper = (45//2, 255, 255)

#convert the image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#find the areas of the image corresponding to the colors set above
mask = cv2.inRange(hsv_image, hsv_lower, hsv_upper)

#set the detected areas to white and other areas to black
new_image = np.where(mask, np.uint8(0), np.uint8(255))

#erode to fill in the gaps between the black pixels.
new_image = cv2.erode(new_image, kernel=np.ones((7,7)))

#find connected components (the white areas)
labels, stats = cv2.connectedComponentsWithStats(new_image)[1:3]

#create a mask for the area excluding the largest component
not_my_yard = labels != np.argmax(stats[:,cv2.CC_STAT_AREA])

#set the color of the area excluding the largest component to black
image[not_my_yard] = 0

#save the new image
cv2.imwrite("my_yard.jpg", image)

请参见下面我的代码中的注释,了解如何在图像中选择您的院子

如果您的院子和非您的院子之间的边界颜色发生变化,这将不是一个可靠的解决方案。通过在GIMP中打开图像,并使用GIMP的颜色选择器工具选择右侧的棕色边框对象,我找到了一个合适的HSV范围。神奇的是,这种颜色也适用于灰色方块。通过使用颜色范围,或者甚至为棕色块和灰色块创建单独的遮罩,您可能会获得更好的效果

import cv2
import numpy as np

#load the image
image = cv2.imread("meadow.jpg")

#define the lower and upper bounds of colors to threshold for in the HSV color space.
hsv_lower = (35//2, 0, 120)
hsv_upper = (45//2, 255, 255)

#convert the image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

#find the areas of the image corresponding to the colors set above
mask = cv2.inRange(hsv_image, hsv_lower, hsv_upper)

#set the detected areas to white and other areas to black
new_image = np.where(mask, np.uint8(0), np.uint8(255))

#erode to fill in the gaps between the black pixels.
new_image = cv2.erode(new_image, kernel=np.ones((7,7)))

#find connected components (the white areas)
labels, stats = cv2.connectedComponentsWithStats(new_image)[1:3]

#create a mask for the area excluding the largest component
not_my_yard = labels != np.argmax(stats[:,cv2.CC_STAT_AREA])

#set the color of the area excluding the largest component to black
image[not_my_yard] = 0

#save the new image
cv2.imwrite("my_yard.jpg", image)

Sry,我忘了添加没有想象中的红线的图片。我想要的python脚本应该完成这一步。而且它应该独立于草的颜色,能够识别边缘。由于围栏底部和岩石的可视性很好,您认为使用cv2 canny()和HoughLinesP()可能吗?我假设您希望检测边界,而不是草的颜色,这就是我选择棕色块的原因。看起来它们在新图像中不可见,并且此方法可能不再有效。我不认为Canny或HoughLinesP能解决这个问题。试着和我做的一样,但是为你的深绿色篱笆和白色砖块制作一个面具,然后将面具组合起来。然后使用findContours而不是连接的组件。找到轮廓后,可以使用drawContours以红色绘制边界。如果我能找到更多的时间,我以后会帮忙的。你知道有没有一个模式查找器可以获取GRA和栅栏的颜色范围吗?为什么我要用HSV而不是RGB呢?我不知道有什么简单的方法可以找到颜色范围。如果您想要更精确的范围,那么可以裁剪出目标部分,将其加载到Python脚本中,并获得每个通道的最小-最大范围(色调、饱和度、值)。如果你想的话,你可以用RGB代替HSV,但是要找到合适的颜色范围可能会有点困难。有gras混凝土或其他类似材料的图案检测系统吗?Sry,我忘了添加没有想象红线的图片。我想要的python脚本应该完成这一步。而且它应该独立于草的颜色,能够识别边缘。由于围栏底部和岩石的可视性很好,您认为使用cv2 canny()和HoughLinesP()可能吗?我假设您希望检测边界,而不是草的颜色,这就是我选择棕色块的原因。看起来它们在新图像中不可见,并且此方法可能不再有效。我不认为Canny或HoughLinesP能解决这个问题。试着和我做的一样,但是为你的深绿色篱笆和白色砖块制作一个面具,然后将面具组合起来。然后使用findContours而不是连接的组件。找到轮廓后,可以使用drawContours以红色绘制边界。如果我能找到更多的时间,我以后会帮忙的。你知道有没有一个模式查找器可以获取GRA和栅栏的颜色范围吗?为什么我要用HSV而不是RGB呢?我不知道有什么简单的方法可以找到颜色范围。如果您想要更精确的范围,那么可以裁剪出目标部分,将其加载到Python脚本中,并获得每个通道的最小-最大范围(色调、饱和度、值)。如果你想的话,你可以用RGB代替HSV,但是要找到合适的颜色范围可能会有点困难。有没有gras混凝土或其他类似材料的图案检测系统?