OpenCV计算机视觉实战(Python)| 14、项目实战:停车场车位识别

简介

本节为《OpenCV计算机视觉实战(Python)》版第14讲,项目实战:停车场车位识别,的总结。

总结

1、项目介绍

统计:

  1. 有多少停车位
  2. 哪些个停车位被占据,哪些个停车位没有被占据
    在这里插入图片描述

2、步骤

  1. 选取区域:人工选择停车场所在的位置,只保存停车场所在的区域图像
  2. 预处理:灰度、边缘、霍夫
  3. 微调:针对实际的项目,由于背景是没变的,当结果不正确时,可以人为给定数据对结果进行微调
  4. 分类:以簇的形式,将每一列的停车场数据保存,保存每一列矩阵的坐标点
  5. 分割:以实际项目选取合适的值,将每一列分割成一个个小块,每一个小块代表一个停车场的位置
  6. 深度训练:以空停车位做分类训练,判断停车场某个停车位上有没有车;

3、程序

Parking.py:

class Parking:

	def cv_show(name, image):
		cv2.imshow(name, image)
		cv2.waitKey(0)
		cv2.destroyAllWindows()
	
	def select_rgb_white_yellow(self, image):
		# 过滤掉背景
		lower = np.uint8([120, 120, 120])
		upper = np.uint8([255, 255, 255])
		# lower_red和高于upper_red的部分分别变成0,lower_red-upper_red之间的值变成255,相当于过滤背景
		white_mask = cv2.inRange(image, lower, upper)
		self.cv_show('white_mask', white_mask)
		
		masked = cv2.bitwise_and(image, image, mask=white_mask)
		self.cv_show('masked', masked)
		return masked
	
	def convert_gray_scale(self, image):
		return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
	def detect_edges(self, image, low_threshold=50, high_threshold=200):
		return cv2.Canny(image, low_threshold, high_threshold)
	
	def filter_region(self, image, vertices):
		 """
		 剔除掉不需要的地方
		 """
		 mask = np.zeros_like(image)
		 if len(mask.shape)==2:
		 	cv2.fillPoly(mask, vertices, 255)
		 	self.cv_show('mask', mask)
		 return cv2.bitwise_and(image, mask)
		
	def select_region(self, image):
		"""
		手动选择区域
		"""	
		# first, define the polygon by vertices
		rows, cols = image.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]
	
		vertices = np.array([[pt_1, pt_2, pt_3, pt_4, pt_5, pt_6]], dtype=np.int32)
		point_img = img.copy()
		point_img = cv2.cvtcolor(point_img, cv2.COLOR_GRAY2RGB)
		for point in vertices[0]:
			cv2.circle(point_img, (point[0], point[1]), 10, (0,0.255), 4)
		self.cv_show('point_img', point_img)
		return self.filter_region(image, vertices)
	
	def hough_lines(self, image):
		#阈值越大,直线越少
		return cv2.HoughLinesP(image, rho=0.1, theta=np.pi/10, threshold=15, minLineLength=0, maxLineGap=4)
	def draw_lines(self, image, lines, color=[255
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值