OpenCV Python實(shí)現(xiàn)拼圖小游戲
基于OpenCV實(shí)現(xiàn)拼圖版小游戲,供大家參考,具體內(nèi)容如下
效果展示

實(shí)現(xiàn)
思路
1.對(duì)圖像進(jìn)行分割,分割成m*n個(gè)子圖
2.打亂子圖的順序
3.將子圖重新組成一幅新的圖片并顯示
4.添加鼠標(biāo)點(diǎn)擊響應(yīng)動(dòng)作,交換鼠標(biāo)依次點(diǎn)擊的兩張圖的位置
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年代經(jīng)典“手游”—拼圖板小游戲Opencv實(shí)現(xiàn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python中的pandas庫(kù)對(duì)cdn日志進(jìn)行分析詳解
這篇文章主要介紹了利用Python中的pandas庫(kù)進(jìn)行cdn日志分析的相關(guān)資料,文中分享了pandas對(duì)cdn日志分析的完整示例代碼,然后詳細(xì)介紹了關(guān)于pandas庫(kù)的相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03
Python安裝Scrapy庫(kù)的常見(jiàn)報(bào)錯(cuò)解決
本文主要介紹了Python安裝Scrapy庫(kù)的常見(jiàn)報(bào)錯(cuò)解決,文中通過(guò)圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
python爬取百度貼吧前1000頁(yè)內(nèi)容(requests庫(kù)面向?qū)ο笏枷雽?shí)現(xiàn))
這篇文章主要介紹了python爬取百度貼吧前1000頁(yè)內(nèi)容(requests庫(kù)面向?qū)ο笏枷雽?shí)現(xiàn)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
如何利用Playwright庫(kù)進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取
playwright庫(kù)是微軟開(kāi)源的一個(gè)庫(kù),這個(gè)庫(kù)的功能更加的強(qiáng)大,除了可以實(shí)現(xiàn)同步操作,同樣也可以實(shí)現(xiàn)異步的操作,這篇文章主要介紹了如何利用Playwright庫(kù)進(jìn)行電影網(wǎng)站數(shù)據(jù)的獲取,需要的朋友可以參考下2023-05-05
檢測(cè)tensorflow是否使用gpu進(jìn)行計(jì)算的方式
今天小編就為大家分享一篇檢測(cè)tensorflow是否使用gpu進(jìn)行計(jì)算的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
用Python的Tornado框架結(jié)合memcached頁(yè)面改善博客性能
這篇文章主要介紹了用Python的Tornado框架結(jié)合memcached頁(yè)面改善vLog性能,主要使用到了緩存來(lái)提升性能,需要的朋友可以參考下2015-04-04
Python使用QQ郵箱發(fā)送Email的方法實(shí)例
實(shí)際開(kāi)發(fā)過(guò)程中使用到郵箱的概率很高,那么如何借助python使用qq郵箱發(fā)送郵件呢?正好最近工作遇到這個(gè)需求,所以想著把方法分享出來(lái)方便大家,所以這篇文章主要介紹了Python使用QQ郵箱發(fā)送Email的實(shí)現(xiàn)方法,需要的朋友可以參考。2017-02-02
python django model聯(lián)合主鍵的例子
今天小編就為大家分享一篇python django model聯(lián)合主鍵的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python中的單下劃線和雙下劃線使用場(chǎng)景詳解
這篇文章主要介紹了Python中的單下劃線和雙下劃線使用場(chǎng)景詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

