首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >庭院内检测(cv2)

庭院内检测(cv2)
EN

Stack Overflow用户
提问于 2020-07-14 08:25:24
回答 1查看 151关注 0票数 1

我想用栅栏、地下室和石头把花园里的花园和外面的花园分开。脚本应该能够绘制您可以在第二张图像中看到的红线。使用cv2应该是可行的

我试过在车道检测系统中找到的一个代码,但不幸的是它不适合gras。

代码语言:javascript
复制
gray = cv2.cvtColor(gras_image, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
canny = cv2.Canny(blur, 50, 200)

感谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2020-07-14 17:50:38

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

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

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62886284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档