OpenCV Python实现拼图小游戏


Posted in Python onMarch 23, 2020

基于OpenCV实现拼图版小游戏,供大家参考,具体内容如下

效果展示

OpenCV Python实现拼图小游戏

实现

思路

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)

参考

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Cpy和Python的效率对比
Mar 20 Python
Python中的rjust()方法使用详解
May 19 Python
python去掉行尾的换行符方法
Jan 04 Python
Python使用pickle模块存储数据报错解决示例代码
Jan 26 Python
python 自定义异常和异常捕捉的方法
Oct 18 Python
python开启debug模式的方法
Jun 27 Python
django自带serializers序列化返回指定字段的方法
Aug 21 Python
Python3 使用pillow库生成随机验证码
Aug 26 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
Jan 14 Python
Python中Yield的基本用法
Oct 18 Python
python爬虫分布式获取数据的实例方法
Nov 26 Python
Django+Django-Celery+Celery的整合实战
Jan 20 Python
PYcharm 激活方法(推荐)
Mar 23 #Python
利用 PyCharm 实现本地代码和远端的实时同步功能
Mar 23 #Python
Python面向对象程序设计之私有变量,私有方法原理与用法分析
Mar 23 #Python
Python常用编译器原理及特点解析
Mar 23 #Python
Python3.7.0 Shell添加清屏快捷键的实现示例
Mar 23 #Python
Python面向对象程序设计之继承、多态原理与用法详解
Mar 23 #Python
python实现图像拼接功能
Mar 23 #Python
You might like
php约瑟夫问题解决关于处死犯人的算法
2015/03/23 PHP
PHP微信红包API接口
2015/12/05 PHP
深入解析PHP的Yii框架中的缓存功能
2016/03/29 PHP
Yii框架创建cronjob定时任务的方法分析
2017/05/23 PHP
javascript 实现父窗口引用弹出窗口的值的脚本
2007/08/07 Javascript
10款非常有用的 Ajax 插件分享
2012/03/14 Javascript
js加入收藏以及使用Jquery更改透明度
2014/01/26 Javascript
网页中右键功能的实现方法之contextMenu的使用
2017/02/20 Javascript
js is_valid_filename验证文件名的函数
2017/07/19 Javascript
JavaScript捕捉事件和阻止冒泡事件实例分析
2018/08/03 Javascript
Vue keepAlive 数据缓存工具实现返回上一个页面浏览的位置
2019/05/10 Javascript
vue中created和mounted的区别浅析
2019/08/13 Javascript
React倒计时功能实现代码——解耦通用
2020/09/18 Javascript
解决vue初始化项目一直停在downloading template的问题
2020/11/09 Javascript
[02:35]DOTA2超级联赛专访XB 难忘一年九冠称王
2013/06/20 DOTA
python 每天如何定时启动爬虫任务(实现方法分享)
2018/05/21 Python
Pandas GroupBy对象 索引与迭代方法
2018/11/16 Python
python实现停车管理系统
2018/11/30 Python
pip install python 快速安装模块的教程图解
2019/10/08 Python
浅谈keras的深度模型训练过程及结果记录方式
2020/01/24 Python
Selenium启动Chrome时配置选项详解
2020/03/18 Python
解决keras使用cov1D函数的输入问题
2020/06/29 Python
基于DOM+CSS3实现OrgChart组织结构图插件
2016/03/02 HTML / CSS
美国大尺码女装零售商:TORRID
2016/10/01 全球购物
英国内衣连锁店:Boux Avenue
2018/01/24 全球购物
将时尚融入珠宝:Adornmonde
2019/10/17 全球购物
学习雷锋寄语大全
2014/04/11 职场文书
体育系毕业生求职自荐信
2014/04/16 职场文书
合作协议书范本
2014/04/17 职场文书
员工考核评语大全
2014/04/26 职场文书
离婚协议书范本样本
2014/08/19 职场文书
2014年医院个人工作总结
2014/12/09 职场文书
检讨书格式范文
2015/05/07 职场文书
2015年教研员工作总结
2015/05/26 职场文书
mysql 如何获取两个集合的交集/差集/并集
2021/06/08 MySQL
在项目中使用redis做缓存的一些思路
2021/09/14 Redis