python目标定位(借鉴csdn上大神)

时间:2023-03-08 16:25:41

写博客是为了记录下来,毕竟好多东西记不住,看过就忘了,收藏又太多,还不如搬运到自己博客下面,随时可翻~~~

近期再学目标识别与定位,看着原理都很简单,但是真自己做,又觉得困难重重。

****上一个大神发了一个虫子的定位切割程序,跑了一下效果不错,因此记录下来,可以在此基础上改进。

import cv2
import numpy as np def get_image(path):
#获取图片
img=cv2.imread(path)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) return img, gray def Gaussian_Blur(gray):
# 高斯去噪
blurred = cv2.GaussianBlur(gray, (9, 9),0) return blurred def Sobel_gradient(blurred):
# 索比尔算子来计算x、y方向梯度
gradX = cv2.Sobel(blurred, ddepth=cv2.CV_32F, dx=1, dy=0)
gradY = cv2.Sobel(blurred, ddepth=cv2.CV_32F, dx=0, dy=1) gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient) return gradX, gradY, gradient def Thresh_and_blur(gradient): #滤波,二值化 blurred = cv2.GaussianBlur(gradient, (9, 9),0)
(_, thresh) = cv2.threshold(blurred, 90, 255, cv2.THRESH_BINARY) return thresh def image_morphology(thresh): #形态学,补齐边缘
# 建立一个椭圆核函数
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25, 25)) #返回指定形状和尺寸的结构元素,
# 定义一个25x25的椭圆形内核
# 执行图像形态学, 细节直接查文档,很简单
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel) #开运算,开运算即为先腐蚀再膨胀,目的是消除白色点点
closed = cv2.erode(closed, None, iterations=4) #腐蚀
closed = cv2.dilate(closed, None, iterations=4) #膨胀 return closed def findcnts_and_box_point(closed): #寻找目标轮廓和中心点
# 这里opencv3返回的是三个参数
(_, cnts, _) = cv2.findContours(closed.copy(),
cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0] #倒序排列
#cv2.contourArea是轮廓的面积,因此是按照面积由大到小的顺序排序
# compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
#旋转的边界矩形,面积最小,返回值为Box2D结构,分别为左上角坐标、宽和高,旋转角度
box = np.int0(cv2.boxPoints(rect))
#搭配cv2.minAreaRect函数,用于绘制旋转边界矩形
return box def drawcnts_and_cut(original_img, box): #画轮廓
# 因为这个函数有极强的破坏性,所有需要在img.copy()上画
# draw a bounding box arounded the detected barcode and display the image
draw_img = cv2.drawContours(original_img.copy(), [box], -1, (0, 0, 255), 3) #绘制所有轮廓 Xs = [i[0] for i in box]
Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
y1 = min(Ys)
y2 = max(Ys)
hight = y2 - y1
width = x2 - x1
crop_img = original_img[y1:y1+hight, x1:x1+width] return draw_img, crop_img def walk(): img_path = r'F:\pycharm\test\iterable\cz.png'
save_path = r'F:\pycharm\test\iterable\cz_save.png'
original_img, gray = get_image(img_path)
blurred = Gaussian_Blur(gray)
gradX, gradY, gradient = Sobel_gradient(blurred)
thresh = Thresh_and_blur(gradient)
closed = image_morphology(thresh)
box = findcnts_and_box_point(closed)
draw_img, crop_img = drawcnts_and_cut(original_img,box)
# 暴力一点,把它们都显示出来看看
cv2.imshow('original_img', original_img)
cv2.imshow('blurred', blurred)
cv2.imshow('gradX', gradX)
cv2.imshow('gradY', gradY)
cv2.imshow('final', gradient)
cv2.imshow('thresh', thresh)
cv2.imshow('closed', closed)
cv2.imshow('draw_img', draw_img)
cv2.imshow('crop_img', crop_img)
cv2.waitKey(20171219)
cv2.imwrite(save_path, crop_img) walk()

希望自己早日写出实现功能的代码。

原作网址为:https://blog.****.net/sinat_36458870/article/details/78825571。

感谢。