opencv+python-常见矩形物体定位

opencv+python-常见矩形物体定位

在视觉抓取中,许多常见标准物体需要被视觉定位,确定该物体在二维平面内的位置和角度。
本次主要使用轮廓识别的方式完成矩形物体的视觉定位。
在这里插入图片描述

实施步骤

1.读取图片(灰度图);
2.二值化,将定位物体与周围环境区分开(在抓取位置设计的时候要考虑到);
3.高斯滤波调参(这里需要通过调整参数使定位物体与周围环境区分更细致);
在这里插入图片描述

4.通过轮廓查找算法查找出定位物体的轮廓位置;
5.通过计算轮廓的面积和周长滤除一些干扰的轮廓信息;
6.通过轮廓查找的角点位置拟合出最小内接矩形,该矩形即为定位物体的轮廓区域;
7.根据轮廓矩形四个角点的坐标计算矩形中心坐标和矩形边的倾斜角度;

代码如下

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import math 
PI =3.1415926

def coorxy(image,p,theta,x0,y0):
	arrow_length = p  
	angle = theta  # 旋转角度(单位:度)  
	angle_rad = np.deg2rad(angle)  
	dx = arrow_length * np.cos(angle_rad)  # x方向上的位移  
	dy = arrow_length * np.sin(angle_rad)  # y方向上的
	dx = int(dx)
	dy = int(dy)
	# 绘制x和y方向箭头  
	cv2.arrowedLine(image, (x0 , y0), (x0+dx, y0+dy), (0, 0, 255), 2)  
	cv2.arrowedLine(image, (x0 , y0), (x0-dy, y0+dx), (0, 0, 255), 2)  

image=cv2.imread('img3.bmp',cv2.IMREAD_COLOR)
print(image.shape)
image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# 使用Canny边缘检测来识别图像中的边缘 
# main_img = cv2.medianBlur(main_img,3)
ret,main_img = cv2.threshold(image_gray, 100, 180, cv2.THRESH_BINARY)
main_img = cv2.medianBlur(main_img,3)


#根据二值图找到轮廓
contours, hierarchy = cv2.findContours(main_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
	
count = 0
coordinates=[]
print(len(contours))
# 画出轮廓
for i in range(0, len(contours)):
#提取每一个轮廓数据
	cnt = contours[i]
	# #drew contours
	# cv2.drawContours(image,contours,-1,(0,255,0),5) 
	#对轮廓计算面积
	area = cv2.contourArea(cnt)
	if area > 150000:
		count=count+1
		print("23")
		# cv2.drawContours(image,cnt,-1,(0,255,0),5)
		# epsilon = 0.05 * cv2.arcLength(cnt, True)
		epsilon = 100
		print("epsilon",epsilon)
		#approx 为多个矩形的4个拐点坐标
		approx = cv2.approxPolyDP(cnt, epsilon, True)
		good_approx = []
		# print(approx)
		print(len(approx))

		for single_approx in approx:
			# print(single_approx[0][0])
			if single_approx[0][0]>10 and single_approx[0][0]<1950 and single_approx[0][1]>10 and single_approx[0][1]<1190: 
				good_approx.append(single_approx)
		good_approx = np.array(good_approx)
		print(good_approx)
		print("len is ",len(good_approx))


		rect = cv2.minAreaRect(good_approx) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度)
		print("rect",rect)
		box = cv2.boxPoints(rect) # 获取最小外接矩形的4个顶点坐标(ps: cv2.boxPoints(rect) for OpenCV 3.x)
		box = np.int0(box)
		# cv2.drawContours(image, [good_approx], -1, (0, 0,255), 3)
		cv2.drawContours(image, [box], -1, (255,0, 0), 3)
		circle_x = int(rect[0][0])
		circle_y = int(rect[0][1])
		cv2.circle(image,(circle_x,circle_y),30,(0,0,255),-1)
		theta = rect[2]
		coorxy(image,400,theta,circle_x,circle_y)
		data1 = "x :"+str(circle_x)
		data2 = "y :"+str(circle_y) 
		data3 = "theta :"+str(round(theta,2))
		cv2.putText(image, data1, (circle_x,circle_y+100), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 2)
		cv2.putText(image, data2, (circle_x,circle_y+180), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 2)
		cv2.putText(image, data3, (circle_x,circle_y+260), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 2)

print(count)
main_img = cv2.resize(main_img,dsize=None,fx=0.25,fy=0.25,interpolation=cv2.INTER_LINEAR)
cv2.imshow('main_img', main_img)

image = cv2.resize(image,dsize=None,fx=0.5,fy=0.5,interpolation=cv2.INTER_LINEAR)
cv2.imshow('image', image)

image_gray = cv2.resize(image_gray,dsize=None,fx=0.25,fy=0.25,interpolation=cv2.INTER_LINEAR)
cv2.imshow('image_gray', image_gray)
cv2.waitKey(0)  

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenCV3是一个开源的计算机视觉库,它提供了许多现代计算机视觉技术的实现,包括人脸识别。在Python中使用OpenCV3进行视频人脸识别非常方便。 首先,我们需要安装OpenCV3库。可以通过pip来安装: ```python pip install opencv-python ``` 然后我们需要一个视频文件来进行人脸识别。我们可以使用OpenCV3的VideoCapture类来读取视频文件,然后使用Haar级联分类器来检测人脸。Haar级联分类器是一种基于机器学习的方法,可以用来检测图像中的目标物体,比如人脸。 ```python import cv2 # 打开视频文件 cap = cv2.VideoCapture('video.mp4') # 加载人脸识别模型 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') while True: # 读取视频帧 ret, frame = cap.read() # 将帧转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) # 在视频帧上标记人脸 for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) # 显示标记后的视频帧 cv2.imshow('Video', frame) # 按下q键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头 cap.release() cv2.destroyAllWindows() ``` 通过以上代码,我们可以实现使用OpenCV3在Python中进行视频人脸识别。此实现通过Haar级联分类器检测视频帧中的人脸,然后在人脸周围绘制矩形框进行标记。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值