OpenCV Python实现拼图小游戏
作者:v_xchen_v 时间:2021-07-30 18:10:34
基于OpenCV实现拼图版小游戏,供大家参考,具体内容如下
效果展示
实现
思路
1.对图像进行分割,分割成m*n个子图
2.打乱子图的顺序
3.将子图重新组成一幅新的图片并显示
4.添加鼠标点击响应动作,交换鼠标依次点击的两张图的位置
5.每次交换后,判断是否与原图是否一致
python代码
import cv2 as cv
import numpy
import random
import math
src = cv.imread("D:\\CvPic\\1.jpg")
print(src.shape)
h = src.shape[0]
w = src.shape[1]
c = src.shape[2]
row = 3
col = 3
offset_h = h/row
offset_w = w/col
firstClick = False
clickIdx = [0,0]
tileList = []
def calPicIdx(x, y):
print(str(y)+" "+str(h/col))
i = y//(offset_h)
print(str(y%offset_h)+" "+str(offset_w))
j = math.ceil((x%w)/offset_w)
idx = i*row+j
print("i:"+str(i)+" j:"+str(j)+" idx:"+str(idx))
return int(idx)
def onMouse(event, x, y, flag ,params):
if event==cv.EVENT_LBUTTONDOWN:
print("left button down:"+str(x)+" "+str(y))
idx = calPicIdx(x, y)
global firstClick
firstClick = not firstClick
print(firstClick)
if firstClick:
clickIdx[0] = idx
else:
clickIdx[1] = idx
tileList[clickIdx[0]], tileList[clickIdx[1]] = tileList[clickIdx[1]], tileList[clickIdx[0]]
for i in range(0, row):
for j in range (0, col):
dst[i*offset_h:(i+1)*offset_h-1, j*offset_w:(j+1)*offset_w-1] = tileList[i*row+j]
cv.imshow("dst", dst)
difference = cv.subtract(dst, src2)
result = not numpy.any(difference) #if difference is all zeros it will return False
print("result:"+str(result))
print(clickIdx)
# --------------splite image into n*n tile--------------
tile = numpy.zeros((offset_h-1, offset_w-1, c),numpy.uint8)
for i in range(0, row):
for j in range (0, col):
tile = src[i*offset_h:(i+1)*offset_h-1, j*offset_w:(j+1)*offset_w-1]
tileList.append(tile)
# cv.imshow("tile", tile)
# --------------ramdom the tiles--------------------
print(len(tileList))
for i in range(len(tileList)-1,0,-1):
randomIdx = random.randint(0,i-1)
print("swap:"+str(random.randint(0,i-1))+" "+str(i))
tileList[i], tileList[randomIdx] = tileList[randomIdx], tileList[i]
# debug show every tile
# for k,tile in enumerate(tileList):
# cv.imshow("tile"+str(k), tile)
dst = numpy.zeros((h, w, c), numpy.uint8)
for i in range(0, row):
for j in range (0, col):
dst[i*offset_h:(i+1)*offset_h-1, j*offset_w:(j+1)*offset_w-1] = tileList[i*row+j]
cv.namedWindow("dst")
cv.setMouseCallback("dst", onMouse)
cv.imshow("dst", dst)
# -------------match the origin image and now--------------
src2 = src.copy()
for i in range(1, row):
src2[i*offset_h-1:i*offset_h]= numpy.zeros((1,w,3), numpy.uint8)
for j in range(1, col):
src2[0:h,j*offset_w-1:j*offset_w]= numpy.zeros((h,1,3), numpy.uint8)
# cv.imshow("src2", src2)
cv.waitKey(0)
参考
90年代经典“手游”—拼图板小游戏Opencv实现
来源:https://blog.csdn.net/v_xchen_v/article/details/79828728
标签:opencv,python,拼图
0
投稿
猜你喜欢
Python pkg_resources模块动态加载插件实例分析
2023-10-31 16:48:39
python监控linux内存并写入mongodb(推荐)
2022-02-04 11:28:01
Python 作为小程序后端的三种实现方法(推荐)
2023-03-30 09:26:05
springboot配置文件抽离 git管理统 配置中心详解
2021-05-24 08:17:18
使用 Python 遍历目录树的方法
2021-09-21 22:19:32
js序列化和反序列化的使用讲解
2023-08-05 08:13:24
使用Go语言创建静态文件服务器问题
2024-04-26 17:17:55
python异常处理try的实例小结
2022-01-25 06:06:51
python实现从ftp服务器下载文件
2022-04-30 18:54:53
后工业时代的后规范思考
2009-06-03 20:30:00
Win10下配置tensorflow-gpu的详细教程(无VS2015/2017)
2023-04-27 13:37:46
python 日期排序的实例代码
2023-12-02 16:09:35
Anaconda下安装mysql-python的包实例
2024-01-25 08:04:29
Python爬取求职网requests库和BeautifulSoup库使用详解
2021-12-29 09:07:49
安装PyTorch的详细过程记录
2023-05-12 07:50:12
Javascript非构造函数的继承
2024-04-10 10:54:14
利用Python Matlab绘制曲线图的简单实例
2021-05-16 07:21:38
不受欢迎的“欢迎页”
2008-04-20 16:41:00
触手生春【4.14】CSS与HTML结构
2008-12-09 18:10:00
PHP在线打包下载功能示例
2024-06-05 09:40:17