opencv+python-常见矩形物体定位
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
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)
GitHub 加速计划 / opencv31 / opencv
77.38 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
48668119
dnn: use dispatching for Winograd optimizations 6 天前
3dace76c
flann: remove unused hdf5 header 6 天前
更多推荐
已为社区贡献2条内容
所有评论(0)