mirror of
https://github.com/sijiyo/projects.git
synced 2025-04-12 03:26:58 +08:00
92 lines
3.3 KiB
Python
92 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
import cv2,os
|
|
from PIL import Image
|
|
class SlideCrack(object):
|
|
def __init__(self, gap, bg, 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)
|
|
# 寻找矩阵(一维数组当作向量,用Mat定义) 中最小值和最大值的位置
|
|
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
|
|
tl = max_loc
|
|
br = (tl[0] + tw, tl[1] + th)
|
|
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)
|
|
# 输出横坐标, 即 滑块在图片上的位置
|
|
return x
|
|
def get_gap():
|
|
# 滑块图片
|
|
# image1 = "img/tukuai.png"
|
|
image1 = r'./img/slide.png'
|
|
# 背景图片
|
|
# image2 = "img/beijing.png"
|
|
image2 = r'./img/bg.png'
|
|
# 处理结果图片,用红线标注
|
|
#image3 = "img/show_image.png"
|
|
image3 = r'./img/show_image.png'
|
|
sc = SlideCrack(image1, image2, image3)
|
|
os.remove('./img/oldbg.png')
|
|
os.remove('./img/oldallbg.png')
|
|
os.remove('./img/newallbg.png')
|
|
return sc.discern()
|
|
def restore_picture():
|
|
img_list = ["./img/oldbg.png", "./img/oldallbg.png"]
|
|
for index, img in enumerate(img_list):
|
|
image = Image.open(img)
|
|
s = Image.new("RGBA", (260, 160))
|
|
ut = [39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43,42,12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17]
|
|
height_half = 80
|
|
for inx in range(52):
|
|
c = ut[inx] % 26 * 12 + 1
|
|
u = height_half if ut[inx] > 25 else 0
|
|
l_ = image.crop(box=(c, u, c + 10, u + 80))
|
|
s.paste(l_, box=(inx % 26 * 10, 80 if inx > 25 else 0))
|
|
if index == 0:
|
|
s.save("./img/bg.png")
|
|
else:
|
|
s.save("./img/newallbg.png") |