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中enumerate函数用法实例分析
May 20 Python
在Python中用keys()方法返回字典键的教程
May 21 Python
Python使用multiprocessing创建进程的方法
Jun 04 Python
Python中函数参数设置及使用的学习笔记
May 03 Python
python清除字符串中间空格的实例讲解
May 11 Python
python判断数字是否是超级素数幂
Sep 27 Python
python调用staf自动化框架的方法
Dec 26 Python
python shutil文件操作工具使用实例分析
Dec 25 Python
python3 sorted 如何实现自定义排序标准
Mar 12 Python
Python内置函数property()如何使用
Sep 01 Python
python MD5加密的示例
Oct 19 Python
python实现一个简单的贪吃蛇游戏附代码
Jun 28 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记录代码执行时间(实现代码)
2013/07/05 PHP
php使用session二维数组实例
2014/11/06 PHP
ThinkPHP框架安全实现分析
2016/03/14 PHP
JavaScript取得鼠标绝对位置程序代码介绍
2012/09/16 Javascript
jquery的父子兄弟节点查找示例代码
2014/03/03 Javascript
javascript的switch用法注意事项分析
2015/02/02 Javascript
JavaScript事件学习小结(五)js中事件类型之鼠标事件
2016/06/09 Javascript
Nodejs下用submit提交表单提示cannot post错误的解决方法
2016/11/21 NodeJs
JavaScript实现事件的中断传播和行为阻止方法示例
2017/01/20 Javascript
JavaScript禁止微信浏览器下拉回弹效果
2017/05/16 Javascript
浅谈Vue-cli 命令行工具分析
2017/11/22 Javascript
echarts鼠标覆盖高亮显示节点及关系名称详解
2018/03/17 Javascript
jQuery实现基本隐藏与显示效果的方法详解
2018/09/05 jQuery
[02:31]2014DOTA2国际邀请赛2009专访:干爹表现出乎意料 看好DK杀回决赛
2014/07/20 DOTA
[03:00]DOTA2-DPC中国联赛1月18日Recap集锦
2021/03/11 DOTA
Python排序算法之选择排序定义与用法示例
2018/04/29 Python
pygame游戏之旅 载入小车图片、更新窗口
2018/11/20 Python
pytorch 实现查看网络中的参数
2020/01/06 Python
解决Keras 与 Tensorflow 版本之间的兼容性问题
2020/02/07 Python
Python集合操作方法详解
2020/02/09 Python
django修改models重建数据库的操作
2020/03/31 Python
使用jupyter notebook直接打开.md格式的文件
2020/04/10 Python
python 使用OpenCV进行简单的人像分割与合成
2021/02/02 Python
python自动化办公操作PPT的实现
2021/02/05 Python
html5 touch事件实现页面上下滑动效果【附代码】
2016/03/10 HTML / CSS
HTML5调用手机发短信和打电话功能
2020/04/29 HTML / CSS
巴西葡萄酒商店:Divvino
2020/02/22 全球购物
物流专业大学生求职信范文
2013/10/28 职场文书
小学少先队活动方案
2014/02/18 职场文书
平面设计求职信
2014/03/10 职场文书
学习雷锋演讲稿
2014/05/10 职场文书
党支部书记四风问题整改措施
2014/09/24 职场文书
大学生违纪检讨书300字
2014/10/25 职场文书
消防隐患整改通知书
2015/04/22 职场文书
Win7/8.1用户可以免费升级到Windows 11系统吗?
2021/11/21 数码科技
Ubuntu Server 安装Tomcat并配置systemctl
2022/04/28 Servers