python 实现拖拽验类型证码的图片定位

使用库

import cv2
import numpy as np

加载图片

img = cv2.imread(‘img/1.jpg’)

python 实现拖拽验类型证码的图片定位_第1张图片

仅保留高光的颜色

lower = np.array([210, 210, 210])
upper = np.array([255, 255, 255])
thresh = cv2.inRange(img, lower, upper)
修改lower 或者upper 可以调整保留的颜色 目前保留的是白色的滑块边框

python 实现拖拽验类型证码的图片定位_第2张图片

图片二值化

ret, binary = cv2.threshold(thresh, 127, 255, cv2.THRESH_BINARY)

膨胀图片

k = cv2.getStructuringElement(cv2.MORPH_RECT,(20, 20))
binary = cv2.dilate(binary,k)
python 实现拖拽验类型证码的图片定位_第3张图片

腐蚀图片

k2 = cv2.getStructuringElement(cv2.MORPH_RECT,(5, 5))
binary = cv2.erode(binary, k2)
python 实现拖拽验类型证码的图片定位_第4张图片

查找轮廓

contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)

轮廓信息添加到area 列表

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))

列表排序打印最大边缘

python 实现拖拽验类型证码的图片定位_第5张图片

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])

发现被包含的边缘且边缘的面积等于给定范围

滑块碰撞和腐蚀后白边会增大因此可以确定滑块中一定会存在另外一个内边缘
python 实现拖拽验类型证码的图片定位_第6张图片
i[0] 是边缘的面积,边缘的面积在给顶范围之内
python 实现拖拽验类型证码的图片定位_第7张图片

# 最适合内边缘
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)

python 实现拖拽验类型证码的图片定位_第8张图片
最后返回中心点离开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)



你可能感兴趣的:(python,windows,python,百度云)