OpenCV Python实现拼图小游戏
作者:v_xchen_v 发布时间:2021-07-30 18:10:34
标签:opencv,python,拼图
基于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


猜你喜欢
- 今天照着样例搞了下tensorboard,发现自己无法显示scalar,而graph却可以正常显示。出现这种情况就说明,tensorfboa
- 版权所有:Copyright 1997 Netscape Communications Corporation原文链接:Object Hie
- 全局事件总线,是组件间的一种通信方式,适用于任何组件间通信。看下面具体的例子。父组件:App<template>
- ScrapyScrapy是纯python实现的一个为了爬取网站数据、提取结构性数据而编写的应用框架。Scrapy使用了Twisted异步网络
- 标题:按某字段合并字符串之一(简单合并)描述:将如下形式的数据按id字段合并value字段。id val
- 这里是说watch调用methods里方法的时候,页面经常会报找不到方法这个时候一定要在watch里去输出一下this,看看this包裹的壳
- uni-simple-router专为uniapp打造的路由器,和uniapp深度集成通配小程序、App和H5端H5能完全使用vue-rou
- 前言在学习过程中发现有时候交换字典的键和值,会使得我们最后的输出结果更加直观明了,整理出以下四种交换方式(data是原字典,new_data
- 目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,
- 1.语音信号的产生与特性我们要对语音进行分析,首先要提取能够表示该语音的特征参数,有了特征参数才可能利用这些参数进行有效的处理,在对语音信号
- 最近我看到看到使用python实现火车票查询,我自己也实现了,感觉收获蛮多的,下面我就把每一步骤都详细给分享出来。(注意使用的是python
- 首先创建一个表Use Test;Create Table TableTest(`ID` mediumint(8) default '
- Jinja是组成Flask的模板引擎。可能你还不太了解它是干嘛的,但你对下面这些百分号和大括号肯定不陌生:{% block body %}
- 导言在前面三节的示例中,GridView和DetailsView控件使用的是绑定列和CheckBoxField(绑定GridView和Det
- 本文实例讲述了thinkPHP框架实现类似java过滤器的简单方法。分享给大家供大家参考,具体如下:写java web代码的时候,可以定义过
- 任务:用python时间简单的统计任务-统计男性和女性分别有多少人。用到的物料:xlrd 它的作用-读取excel表数据代码:import
- 先看一个示例代码如下<template> <div align="center" styl
- 先推荐一个学习python的好网址简明 Python 教程 Swaroop, C. H. 著 沈洁元 译在线教程的网址
- 一.图像掩膜直方图如果要统计图像的某一部分直方图,就需要使用掩码(蒙板)来进行计算。假设将要统计的部分设置为白色,其余部分设置为黑色,然后使
- 步骤用opencv打开摄像头并拍照保存照片到本地获取邮箱(如qq邮箱)的授权码,方法可自行百度将照片以附件的形式发送到指定邮箱删除本地照片代