python - parking 位角点坐标通过 Python 3.6 中的 OpenCV 收集

标签 python python-3.x opencv

我想用 OpenCV 和 Python 制作一个 parking 位检测器,但我一直无法找到每个 parking 位的坐标。

所以,我的想法是应用 hughlines 函数来检测线条,然后跟踪角,然后应用一个函数来确定该点是否为空。

这是我到目前为止得到的:

检测点是否为空的函数:

def f(img,a,b,c,d):
sub_img=img[a:b, c:d]
edg= cv2.Canny(sub_img,100,150)
pix=cv2.countNonZero(edg)
if pix>1400:
    cv2.rectangle(img,(d,b),(c,a),(0,0,255),3)
else:cv2.rectangle(img,(d,b),(c,a),(0,255,),3)

我尝试检测 Blob 的部分:

import cv2
import numpy as np
import csv
from matplotlib import pyplot as plt

img = cv2.imread('parking.jpg',1)

im_gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(thresh, im_bw)= cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY| cv2.THRESH_OTSU)
tresh=127
im_bw= cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imshow('binary',im_bw)
edges = cv2.Canny(im_bw,300,350)
imagem = cv2.bitwise_not(im_bw)
height, width = im_bw.shape[:2]


contours=np.zeros((height, width,3),np.uint8)
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=0.1,maxLineGap=30)
for line in lines:
    x1,y1,x2,y2 = line[0]
    cv2.circle(im_bw,(x1,y1),3,(0,255,0),3)
    cv2.circle(im_bw,(x2,y2),3,(0,0,255),3)
    cv2.line(contours,(x1,y1),(x2,y2),255,2)


gray = cv2.cvtColor(contours,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
contours[dst>0.1*dst.max()]=[0,0,255]

# Printing corners coords
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
ret, dst = cv2.threshold(dst,0.01*dst.max(),255,0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
k=tuple(corners)
print(tuple(corners))

with open('csv.txt','w',newline='')as outf:
    csvw= csv.writer(outf)
    csvw.writerows(k)
corn=open('csv.txt')
cv2.line(contours,(corn[1],y1),(x2,y2),255,2)

cv2.imshow('contours',contours)
cv2.imshow('houghlines5.jpg',img)
# plt.imshow(contours, cmap = 'gray', interpolation = 'bicubic')
# plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
# plt.show()

cv2.waitKey(0)
cv2.destroyAllWindows()

原图

1

houghlines和harrisCorner检测结果

2

这是检测开放 parking 位的正确方法吗?如果没有,什么会更好?

最佳答案

要使角检测器正常工作,您可以做的是在找到线后将线的粗细减少到一个像素。您可以直接执行此操作,也可以使用骨架化算法 ( http://homepages.inf.ed.ac.uk/rbf/HIPR2/skeleton.htm )。如果这不起作用,我建议您使用 AKAZE Detector 来查找这些交叉点。但是,这对于您的应用程序来说可能有点夸张。

我希望这对你有进一步的帮助! :)

关于python - parking 位角点坐标通过 Python 3.6 中的 OpenCV 收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50473598/

相关文章:

python - Django:在沙箱环境中运行python shell,类似于rails console --sandbox

python - 我的 tkinter 窗口立即关闭

python - OpenCV的RotatedRect在Python3中没有属性 'size'。如何解决这个问题?

c++ - 在 OpenCV 中创建随机彩色图像

python - 我怎么能在 python/nltk 中使用完整的 penn treebank 数据集

python - 如何将 TfidfVectorizer 的输出提供给 Sklearn 中的 LinearSVC 分类器?

python - 如何将额外参数传递给 Google Pagespeed API 服务对象

python - Django - 如何获取输入列表,选择使用 javascript 动态创建的?

Python Opencv 利用霍夫圆变换从二值图像中检测圆

python - 使用 OpenCV 从图像背景中去除波浪噪声