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 相关文章推荐
Python编程中的反模式实例分析
Dec 08 Python
Python中元组,列表,字典的区别
May 21 Python
用Eclipse写python程序
Feb 10 Python
Python3多线程操作简单示例
May 22 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
Dec 10 Python
python实现月食效果实例代码
Jun 18 Python
python中的句柄操作的方法示例
Jun 20 Python
基于Python函数和变量名解析
Jul 19 Python
python+selenium+Chrome options参数的使用
Mar 18 Python
Python定义一个函数的方法
Jun 15 Python
python入门:argparse浅析 nargs='+'作用
Jul 12 Python
Python实现快速大文件比较代码解析
Sep 04 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
Windows下利用Gvim写PHP产生中文乱码问题解决方法
2011/04/20 PHP
php安全之直接用$获取值而不$_GET 字符转义
2012/06/03 PHP
深入解析Session是否必须依赖Cookie
2013/08/02 PHP
php实现获取文件mime类型的方法
2015/02/11 PHP
php封装json通信接口详解及实例
2017/03/07 PHP
php数据库的增删改查 php与javascript之间的交互
2017/08/31 PHP
PHP实现验证码校验功能
2017/11/16 PHP
php实现映射操作实例详解
2019/10/02 PHP
Docker 安装 PHP并与Nginx的部署实例讲解
2021/02/27 PHP
JQuery 动画卷页 返回顶部 动画特效(兼容Chrome)
2010/02/15 Javascript
在JavaScript并非所有的一切都是对象
2013/04/11 Javascript
JavaScript利用append添加元素报错的解决方法
2014/07/01 Javascript
Javascript页面跳转常见实现方式汇总
2015/11/28 Javascript
Bootstrap编写一个在当前网页弹出可关闭的对话框 非弹窗
2016/06/30 Javascript
json与jsonp知识小结(推荐)
2016/08/16 Javascript
js实现自动播放匀速轮播图
2020/02/06 Javascript
python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
2016/09/19 Python
Python实现绘制双柱状图并显示数值功能示例
2018/06/23 Python
Python实现基于POS算法的区块链
2018/08/07 Python
python实现合并多个list及合并多个django QuerySet的方法示例
2019/06/11 Python
浅谈Python中函数的定义及其调用方法
2019/07/19 Python
pycharm设置当前工作目录的操作(working directory)
2020/02/14 Python
Python生成器实现简单"生产者消费者"模型代码实例
2020/03/27 Python
怎么解决pycharm license Acti的方法
2020/10/28 Python
巴西购物网站:Submarino
2020/01/19 全球购物
毕业生优秀推荐信
2013/11/26 职场文书
服务员岗位职责
2014/01/29 职场文书
高中生物教学反思
2014/02/05 职场文书
党支部三会一课计划
2014/09/24 职场文书
个人向公司借款协议书
2014/10/09 职场文书
小学科学教学计划
2015/01/21 职场文书
工作检讨书大全
2015/01/26 职场文书
武夷山导游词
2015/02/03 职场文书
交通安全宣传标语(100条)
2019/08/22 职场文书
Nginx Rewrite使用场景及配置方法解析
2021/04/01 Servers
python基础之类方法和静态方法
2021/10/24 Python