import cv2
import numpy as np
img = cv2.imread(‘img/1.jpg’)
lower = np.array([210, 210, 210])
upper = np.array([255, 255, 255])
thresh = cv2.inRange(img, lower, upper)
修改lower 或者upper 可以调整保留的颜色 目前保留的是白色的滑块边框
ret, binary = cv2.threshold(thresh, 127, 255, cv2.THRESH_BINARY)
k = cv2.getStructuringElement(cv2.MORPH_RECT,(20, 20))
binary = cv2.dilate(binary,k)
k2 = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
binary = cv2.erode(binary, k2)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
area = []
for k in range(len(contours)):
# if len((contours[k])) < 111111 and cv2.contourArea(contours[k])<20000 :
# print(cv2.contourArea(contours[k]))
area.append((cv2.contourArea(contours[k]),k))
num_ary = area
i = 0
while i < len(num_ary) - 1:
i += 1
n = 0
while n < len(num_ary) - 1:
if num_ary[n] > num_ary[n + 1]:
num_ary[n], num_ary[n + 1] = num_ary[n + 1], num_ary[n]
n += 1
print(num_ary)
print("最大边缘:",(num_ary[-1])[0])
滑块碰撞和腐蚀后白边会增大因此可以确定滑块中一定会存在另外一个内边缘
i[0] 是边缘的面积,边缘的面积在给顶范围之内
# 最适合内边缘
max_idx = 0 # 滑块在列表中的编号
cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
time_mun = 0 # 如果最大的边缘之内没有最适合边缘time_mun+1
while time_mun < 10: # 收索最大的10个边缘内是否存在最适合边缘
up_n = (num_ary[-1-time_mun])[1] # 最大边缘的编号
for i in area:
if 4000 > i[0] > 2000:
print('符合条件的轮廓:',i[1])
for j in contours[i[1]]:
x = int((j[0])[0])
y = int((j[0])[1])
dist = None
dist = cv2.pointPolygonTest(contours[up_n], (x, y), True) # 如果在边缘内dist数值为正数否则负数
if dist>0:
max_idx = i[1]
# 如果指定边缘面积内查找到被包含的边缘则退出循环
time_mun = 11
break
time_mun = time_mun+1
M = cv2.moments(contours[max_idx])
center_x = int(M["m10"] / M["m00"])
center_y = int(M["m01"] / M["m00"])
print("x轴位置:",center_x)
源码:
import cv2
import numpy as np
img = cv2.imread('img/1.jpg')
lower = np.array([210, 210, 210])
upper = np.array([255, 255, 255])
thresh = cv2.inRange(img, lower, upper)
ret, binary = cv2.threshold(thresh, 127, 255, cv2.THRESH_BINARY)
k = cv2.getStructuringElement(cv2.MORPH_RECT,(20, 20))
binary = cv2.dilate(binary,k)
cv2.imshow("img", binary)
cv2.waitKey(0)
k2 = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
binary = cv2.erode(binary, k2)
cv2.imshow("img", binary)
cv2.waitKey(0)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
area = []
# 找到最大的轮廓
for k in range(len(contours)):
# if len((contours[k])) < 111111 and cv2.contourArea(contours[k])<20000 :
# print(cv2.contourArea(contours[k]))
area.append((cv2.contourArea(contours[k]),k))
print(area)
# 列表排序
num_ary = area
i = 0
while i < len(num_ary) - 1:
i += 1
n = 0
while n < len(num_ary) - 1:
if num_ary[n] > num_ary[n + 1]:
num_ary[n], num_ary[n + 1] = num_ary[n + 1], num_ary[n]
n += 1
print(num_ary)
print("最大边缘:",(num_ary[-1])[0])
# 最适合内边缘
max_idx = 0
cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
time_mun = 0
while time_mun < 10:
up_n = (num_ary[-1-time_mun])[1]
for i in area:
if 4000 > i[0] > 2000:
print('符合条件的轮廓:',i[1])
for j in contours[i[1]]:
x = int((j[0])[0])
y = int((j[0])[1])
dist = None
dist = cv2.pointPolygonTest(contours[up_n], (x, y), True)
if dist>0:
max_idx = i[1]
time_mun = 11
break
time_mun = time_mun+1
print("最可能是滑块的是:",max_idx)
print("边角数:",len(contours[max_idx]))
M = cv2.moments(contours[max_idx]) # 计算第一条轮廓的各阶矩,字典形式
center_x = int(M["m10"] / M["m00"])
center_y = int(M["m01"] / M["m00"])
print("X轴位置:",center_x)
cv2.circle(img, (center_x, center_y), 60, (0, 0, 255), 2)
cv2.imshow("img", img)
cv2.waitKey(0)