欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

找不到 "姐姐 "这个词?一个游戏看你愁,用我的代码逻辑,不再被游戏困扰!

最编程 2024-03-10 22:42:25
...
import cv2 import numpy as np # 输入灰度图,返回hash def getHash(image): avreage = np.mean(image) # 计算像素平均值 hash = [] for i in range(image.shape[0]): for j in range(image.shape[1]): if image[i, j] > avreage: hash.append(1) else: hash.append(0) return hash # 计算汉明距离 计算相似度越大越不相似 def Hamming_distance(hash1, hash2): num = 0 for index in range(len(hash1)): if hash1[index] != hash2[index]: num += 1 return num img = cv2.imread("3.png") # 灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) h, w = gray.shape[:2] m = np.reshape(gray, [1, w * h]) # 降维 mean = m.sum() / (w * h) # 求均值 _, thresh1 = cv2.threshold(gray, mean, 255, cv2.THRESH_BINARY) # 利用均值进行图像二值化 # 查找轮廓 _, contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓 img2 = img.copy() cv2.drawContours(img2, contours, -1, (0, 0, 255), 0) # 记录裁剪的坐标 ,裁剪图像的时候使用 list_coor = [] # 计算轮廓面积 for i in range(len(contours)): if i == 0: continue area = cv2.contourArea(contours[i]) if area > 3000 and area < 5000: rect = cv2.minAreaRect(contours[i]) box = cv2.boxPoints(rect) box = np.int0(box) list_coor.append(box) # 要进行对比的图像 Image = cv2.imread('mei.jpg') # 灰度化 Image = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY) hash_1 = getHash(Image) # 随便设置一个大值 error = 100000.0 error_coor = [] # 计算分割好的图像hash for i in range(len(list_coor)): x, y = list_coor[i][1] x1, y1 = list_coor[i][3] img_temp = gray[min(x, x1):max(x, x1), min(y, y1):max((y, y1))] img_temp = cv2.resize(img_temp, (80, 80)) cv2.imwrite("./img/%s.jpg" % i, img_temp) hash_2 = getHash(img_temp) ret = Hamming_distance(hash_1, hash_2) # print(ret) if ret < error: error = ret error_coor.clear() error_coor.append([min(x, x1), max(x, x1)]) error_coor.append([min(y, y1), max(y, y1)]) print(ret) print(error_coor) # [array([146, 144]), array([215, 74])] x, y = error_coor[0] x1, y1 = error_coor[1] cv2.rectangle(img, (x, y), (x1, y1), (0, 0, 255), 3) cv2.imshow('paper', img) cv2.waitKey(0)