整个代码主要分为以下几步:(1)对图像预处理

(2)抠出每个停车位的图,以及其地址

(3)使用keras训练一个2分类的模型,卷积网络选择vgg,为了节省时间,采用keras提供的api,并冻结前10层。

(4)从视频中取出每一帧,挨个训练,实现实时

def imggg():#读入一张原始图img=cv2.imread("../test_images/scene1410.jpg")#实现2值化操作,将在(120,255)以内的变成变成白色,将以外的变成黑色low=np.uint8([120,120,120])upper=np.uint8([255,255,255])white_img=cv2.inRange(img,low,upper)print(white_img.shape)cv2.imshow("111",white_img)cv2.waitKeyEx(0)cv2.destroyAllWindows()#与原图进行与运算,求出原图img=cv2.bitwise_and(img,img,mask=white_img)print(img.shape)cv2.imshow("111",img)cv2.waitKeyEx(0)cv2.destroyAllWindows()img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)#边缘检测img=cv2.Canny(img,100,200)cv2.imshow("111",img)cv2.waitKeyEx(0)cv2.destroyAllWindows()cv2.imwrite("canny.jpg",img)

以上主要是将图像2值化并边缘检测,为了后面的调用霍夫曼直线做准备

(2)制作一个mask,将停车场从图中抠出来

(1)先认为的选取图中的6个点,该6个点组成的一个多边形将停车场包围。

(2)生成一张和原始图同样大小的mask图,使用fillpoly()函数将上述6个点内填充为白色

(3)使用bitwith_and ()将原图与mask做mask运算,即可提取出停车场

import cv2
import numpy as np
img1=cv2.imread("../test_images/scene1380.jpg")
img=cv2.imread("canny.jpg",0)
img=img.astype("uint8")
rows, cols = img.shape[:2]
pt_1 = [cols*0.05, rows*0.90]
pt_2 = [cols*0.05, rows*0.70]
pt_3 = [cols*0.30, rows*0.55]
pt_4 = [cols*0.6, rows*0.15]
pt_5 = [cols*0.90, rows*0.15]
pt_6 = [cols*0.90, rows*0.90]
#注意这里采用的是int32类型,一定要指明,否则会报错
point=np.array([pt_1,pt_2,pt_3,pt_4,pt_5,pt_6],dtype=np.int32)
print(point)
for i in point:img1=cv2.circle(img1,(i[0],i[1]),10,(0,0,255))
cv2.imshow("111",img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
#填充图形
zreos=np.zeros((rows,cols))
cv2.imshow("111",zreos)
cv2.waitKeyEx(0)print(zreos.shape)
# cv2.destroyAllWindows()
mask=cv2.fillPoly(zreos,[point],255)
cv2.imshow("111",mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(img.shape)
print(mask.shape)
#一定要指明是uint8,不说又报错。
mask=mask.astype("uint8")
img4=cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("111",img4)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("mask_aprk.jpg",img4)

(3)提取出停车场后下一步就是要标识每一个停车位的位置,采用霍夫直线检测,这里需要反复的调整大小,同时也要注意图中除了停车线,还有许多其他的线,要将其他的线剔除(本段代码较长,逻辑比较复杂,就不细讲了)

(1)lines = cv2.HoughLinesP(edges,0.1,np.pi/180,15,minLineLength=9,maxLineGap=4)

1.霍夫变换是将坐标空间转换为霍夫空间,在霍夫空间中,笛卡尔坐标系中的每个点都是一条曲线,同一条直线上的点会过一个定点

2.cv中的霍夫空间是有极坐标系构成

3.各个参数分别代表 图片,像素精度,角度精度,最少多少个焦点可被判断为一条直线,相隔几个点能被判断为一条直线

    def hough_lines(self,image):#输入的图像需要是边缘检测后的结果#minLineLengh(线的最短长度,比这个短的都被忽略)和MaxLineCap(两条直线之间的最大间隔,小于此值,认为是一条直线)#rho距离精度,theta角度精度,threshod超过设定阈值才被检测出线段return cv2.HoughLinesP(image, rho=0.1, theta=np.pi/10, threshold=15, minLineLength=9, maxLineGap=4)def draw_lines(self,image, lines, color=[255, 0, 0], thickness=2, make_copy=True):# 过滤霍夫变换检测到直线if make_copy:image = np.copy(image) cleaned = []for line in lines:for x1,y1,x2,y2 in line:if abs(y2-y1) <=1 and abs(x2-x1) >=25 and abs(x2-x1) <= 55:cleaned.append((x1,y1,x2,y2))cv2.line(image, (x1, y1), (x2, y2), color, thickness)print(" No lines detected: ", len(cleaned))return image

可以看到,识别并不是很精准,这是由于汽车的存在导致的干扰。

    def identify_blocks(self,image, lines, make_copy=True):if make_copy:new_image = np.copy(image)#Step 1: 过滤部分直线cleaned = []for line in lines:for x1,y1,x2,y2 in line:if abs(y2-y1) <=1 and abs(x2-x1) >=25 and abs(x2-x1) <= 55:cleaned.append((x1,y1,x2,y2))#Step 2: 对直线按照x1进行排序import operatorlist1 = sorted(cleaned, key=operator.itemgetter(0, 1))#Step 3: 找到多个列,相当于每列是一排车clusters = {}dIndex = 0clus_dist = 5for i in range(len(list1) - 1):distance = abs(list1[i+1][0] - list1[i][0])if distance <clus_dist:if not dIndex in clusters.keys(): clusters[dIndex] = []clusters[dIndex].append(list1[i])clusters[dIndex].append(list1[i + 1]) else:dIndex += 1#Step 4: 得到坐标rects = {}i = 0for key in clusters:all_list = clusters[key]cleaned = list(set(all_list))if len(cleaned) > 5:cleaned = sorted(cleaned, key=lambda tup: tup[1])avg_y1 = cleaned[0][1]avg_y2 = cleaned[-1][1]avg_x1 = 0avg_x2 = 0for tup in cleaned:avg_x1 += tup[0]avg_x2 += tup[2]avg_x1 = avg_x1/len(cleaned)avg_x2 = avg_x2/len(cleaned)rects[i] = (avg_x1, avg_y1, avg_x2, avg_y2)i += 1print("Num Parking Lanes: ", len(rects))#Step 5: 把列矩形画出来buff = 7for key in rects:tup_topLeft = (int(rects[key][0] - buff), int(rects[key][1]))tup_botRight = (int(rects[key][2] + buff), int(rects[key][3]))cv2.rectangle(new_image, tup_topLeft,tup_botRight,(0,255,0),3)return new_image, rectsdef draw_parking(self,image, rects, make_copy = True, color=[255, 0, 0], thickness=2, save = True):if make_copy:new_image = np.copy(image)gap = 15.5spot_dict = {} # 字典:一个车位对应一个位置tot_spots = 0#微调adj_y1 = {0: 20, 1:-10, 2:0, 3:-11, 4:28, 5:5, 6:-15, 7:-15, 8:-10, 9:-30, 10:9, 11:-32}adj_y2 = {0: 30, 1: 50, 2:15, 3:10, 4:-15, 5:15, 6:15, 7:-20, 8:15, 9:15, 10:0, 11:30}adj_x1 = {0: -8, 1:-15, 2:-15, 3:-15, 4:-15, 5:-15, 6:-15, 7:-15, 8:-10, 9:-10, 10:-10, 11:0}adj_x2 = {0: 0, 1: 15, 2:15, 3:15, 4:15, 5:15, 6:15, 7:15, 8:10, 9:10, 10:10, 11:0}for key in rects:tup = rects[key]x1 = int(tup[0]+ adj_x1[key])x2 = int(tup[2]+ adj_x2[key])y1 = int(tup[1] + adj_y1[key])y2 = int(tup[3] + adj_y2[key])cv2.rectangle(new_image, (x1, y1),(x2,y2),(0,255,0),2)num_splits = int(abs(y2-y1)//gap)for i in range(0, num_splits+1):y = int(y1 + i*gap)cv2.line(new_image, (x1, y), (x2, y), color, thickness)if key > 0 and key < len(rects) -1 :        #竖直线x = int((x1 + x2)/2)cv2.line(new_image, (x, y1), (x, y2), color, thickness)# 计算数量if key == 0 or key == (len(rects) -1):tot_spots += num_splits +1else:tot_spots += 2*(num_splits +1)# 字典对应好if key == 0 or key == (len(rects) -1):for i in range(0, num_splits+1):cur_len = len(spot_dict)y = int(y1 + i*gap)spot_dict[(x1, y, x2, y+gap)] = cur_len +1        else:for i in range(0, num_splits+1):cur_len = len(spot_dict)y = int(y1 + i*gap)x = int((x1 + x2)/2)spot_dict[(x1, y, x, y+gap)] = cur_len +1spot_dict[(x, y, x2, y+gap)] = cur_len +2   print("total parking spaces: ", tot_spots, cur_len)if save:filename = 'with_parking.jpg'cv2.imwrite(filename, new_image)return new_image, spot_dict

又懒了,大家自己看,直接上结果

每个停车位的位置已经被获得,接下来,只要将图片抠出来即可。到这图像处理工作算是完成了,接下来就是训练模型的事了,有空在写。

停车场车位识别(一)相关推荐

  1. OpenCV计算机视觉实战,停车场车位识别!(完整代码)!

    任务描述:识别这种停车场图的 空车位 与 被占用车位 识别流程:预处理 -> 获得车位坐标的字典 -> 训练VGG网络进行二分类 img_process 图像预处理过程 1.select_ ...

  2. 16.停车场车位识别

    目录 1  项目介绍 2  展示图像 2.1  park_test 2.1.1  导入库 2.1.2  读取测试数据 2.1.3  读取模型与视频 2.1.4  定义分类 2.1.5  show_im ...

  3. 【车位检测】基于计算机视觉实现停车场空位识别附matlab代码

    1 简介 为便于汽车驾驶员在室外停车场中寻找可用空车位,基于以数据采集,图像处理和目标检测等过程的计算机视觉,开发了室外停车场车位检测实验.​ 2 部分代码 clc; close all; clear ...

  4. 如何使用JS来开发室内地图商场停车场车位管理系统

    在线体验到室内地图的功能后,手机对室内地图加载一个字,要显示"快",目前微信和电脑都可以打开室内地图的要求是3秒内打开,能有定位导航的功能最好,这样方便找到要去的地方. 对于经常逛 ...

  5. python例程:智能停车场车牌识别计费系统的程序

    目录 <智能停车场车牌识别计费系统>程序使用说明 主要代码演示 源码下载路径 <智能停车场车牌识别计费系统>程序使用说明 在PyCharm中运行<智能停车场车牌识别计费系 ...

  6. 智能停车场车牌识别系统(一)

    前段时间练习过的一个小项目,今天再看看,记录一下~ 开发工具准备: 开发工具:PyCharm Python内置模块:os.time.datetime 第三方模块:pygame.opencv-pytho ...

  7. 智能停车场车牌识别系统(二)

    在上一篇文章实现车牌识别功能的基础上,加入了实现收入统计功能和满预警功能 看这篇文章之前请先完成 智能停车场车牌识别系统(一) 的阅读 实现收入统计功能: 收入统计就是对停车场每个月的收入进行统计,然 ...

  8. 基于STM32单片机的智能停车场车位管理系统设计

    摘  要 通过调查发现,现有的许多公共场所的停车位管理落后,智能化程度不高.为顺应现代自动化狂潮的发展趋势,本项目以STM32单片机为主控芯片,基于RFID智能识别技术,设计了一个具有IC识别的智能停 ...

  9. pkr车牌识别系统服务器,JAT-PKR-交安通PKR停车场车牌识别管理系统

    交安通PKR停车场车牌识别管理系统特点: 识别系统对环境的依赖性降低至zui低程度,可实现全天候正常工作,且识别率保持较高水平. 可识别的zui小号牌宽度为70个像素 适应复杂的气候及光照条件,如阴天 ...

最新文章

  1. POJ-1129 Channel Allocation DFS搜索
  2. windows命令实验
  3. 一個傳統的C2C網站的用戶充值的过程
  4. 2013多校训练赛第三场 总结
  5. Lua 脚本获取 EVAL EVALSHA 命令的参数
  6. MySql 统计最近 6 个月内的数据,没有数据默认为显示为 0
  7. Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String 序列自动机
  8. [html] 如何在页面打开PDF文件?
  9. tensorflow中GPU的设置
  10. java web中整合mq_spring-web 集成 rabbitmq
  11. Mongo查询数据库及表占用磁盘大小
  12. iview表单校验上传图片成功后,提示文字不消失
  13. Atitit.如何选择技术职业方向
  14. 截图文字识别工具(OCR),图片上的文字也能轻松复制
  15. mysql生成数据字典
  16. Elasticsearch教程(31) es mapping参数doc_values enabled ignore_above norms store详解
  17. 数据分析报告怎么写(三)
  18. 怎么查看笔记本内存条型号_笔记本内存条型号简介以及查看方法【图文教程】...
  19. 八股文(Spring)
  20. Mybatis源码学习-MapperMethod

热门文章

  1. 基于matlab的时分复用实现,基于matlab的多路时分复用仿真.doc
  2. Java工具集-HMacSHA1加解密
  3. Linux忘记root密码如何找回
  4. macOS 12.3正式版(21E230)dmg官方原版镜像
  5. NB-IOT联网通信
  6. jdk8-时间API
  7. 检测鼠标上滑还是下滑
  8. Win 10 安装手机驱动
  9. 每周市场观察:多头转空头? | TokenInsight
  10. WEBRTC 对华为,宝利通硬件,SIP视频会议系统的互通互联,扩容方案分析