OpenCV Python實現拼圖小遊戲
阿新 • • 發佈:2020-03-24
基於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,-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,numpy.uint8) for i 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,3),numpy.uint8) for j in range(1,col): src2[0:h,j*offset_w-1:j*offset_w]= numpy.zeros((h,1,numpy.uint8) # cv.imshow("src2",src2) cv.waitKey(0)
參考
90年代經典“手遊”—拼圖板小遊戲Opencv實現
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。