滑块验证码缺口距离获取python代码

# -*- coding: utf-8 -*-

import cv2

class SlideCrack(object):
    def __init__(self, gap, bg, out):
"""
        init code
        :param gap: 缺口图片
        :param bg: 背景图片
        :param out: 输出图片
"""
        self.gap = gap
        self.bg = bg
        self.out = out

    @staticmethod
    def clear_white(img):
        # 清除图片的空白区域,这里主要清除滑块的空白
        img = cv2.imread(img)
        rows, cols, channel = img.shape
        min_x = 255
        min_y = 255
        max_x = 0
        max_y = 0
        for x in range(1, rows):
            for y in range(1, cols):
                t = set(img[x, y])
                if len(t) >= 2:
                    if x <= min_x: min_x="x" elif x>= max_x:
                        max_x = x

                    if y <= min_y: min_y="y" elif y>= max_y:
                        max_y = y
        img1 = img[min_x:max_x, min_y: max_y]
        return img1

    def template_match(self, tpl, target):
        th, tw = tpl.shape[:2]
        result = cv2.matchTemplate(target, tpl, cv2.TM_CCOEFF_NORMED)
        # &#x5BFB;&#x627E;&#x77E9;&#x9635;(&#x4E00;&#x7EF4;&#x6570;&#x7EC4;&#x5F53;&#x4F5C;&#x5411;&#x91CF;,&#x7528;Mat&#x5B9A;&#x4E49;) &#x4E2D;&#x6700;&#x5C0F;&#x503C;&#x548C;&#x6700;&#x5927;&#x503C;&#x7684;&#x4F4D;&#x7F6E;
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
        tl = max_loc
        br = (tl[0] + tw, tl[1] + th)
        # &#x7ED8;&#x5236;&#x77E9;&#x5F62;&#x8FB9;&#x6846;&#xFF0C;&#x5C06;&#x5339;&#x914D;&#x533A;&#x57DF;&#x6807;&#x6CE8;&#x51FA;&#x6765;
        # target&#xFF1A;&#x76EE;&#x6807;&#x56FE;&#x50CF;
        # tl&#xFF1A;&#x77E9;&#x5F62;&#x5B9A;&#x70B9;
        # br&#xFF1A;&#x77E9;&#x5F62;&#x7684;&#x5BBD;&#x9AD8;
        # (0,0,255)&#xFF1A;&#x77E9;&#x5F62;&#x8FB9;&#x6846;&#x989C;&#x8272;
        # 1&#xFF1A;&#x77E9;&#x5F62;&#x8FB9;&#x6846;&#x5927;&#x5C0F;
        cv2.rectangle(target, tl, br, (0, 0, 255), 2)
        cv2.imwrite(self.out, target)
        return tl[0]

    @staticmethod
    def image_edge_detection(img):
        edges = cv2.Canny(img, 100, 200)
        return edges

    def discern(self):
        img1 = self.clear_white(self.gap)
        img1 = cv2.cvtColor(img1, cv2.COLOR_RGB2GRAY)
        slide = self.image_edge_detection(img1)

        back = cv2.imread(self.bg, 0)
        back = self.image_edge_detection(back)

        slide_pic = cv2.cvtColor(slide, cv2.COLOR_GRAY2RGB)
        back_pic = cv2.cvtColor(back, cv2.COLOR_GRAY2RGB)
        x = self.template_match(slide_pic, back_pic)
        # &#x8F93;&#x51FA;&#x6A2A;&#x5750;&#x6807;, &#x5373; &#x6ED1;&#x5757;&#x5728;&#x56FE;&#x7247;&#x4E0A;&#x7684;&#x4F4D;&#x7F6E;
        print(x)

if __name__ == "__main__":
    # &#x6ED1;&#x5757;&#x56FE;&#x7247;
    image1 = "img/douyin_2.png"
    # &#x80CC;&#x666F;&#x56FE;&#x7247;
    image2 = "img/douyin_1.png"

    # &#x5904;&#x7406;&#x7ED3;&#x679C;&#x56FE;&#x7247;,&#x7528;&#x7EA2;&#x7EBF;&#x6807;&#x6CE8;
    image3 = "img/douyin_3.png"
    sc = SlideCrack(image1, image2, image3)
    sc.discern()

</=></=>

Original: https://www.cnblogs.com/c-x-a/p/16417874.html
Author: 公众号python学习开发
Title: 滑块验证码缺口距离获取python代码

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/7340/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部
最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总