OpenCV Python implements puzzle games

Based on OpenCV to realize the puzzle version of the mini game, for your reference, the specific content is as follows

Show results

achieve

Ideas

  1. Segment the image into m*n sub-images
  2. Shuffle the order of subgraphs
  3. Reorganize the sub-picture into a new picture and display it
  4. Add mouse click response action, swap the position of the two pictures that the mouse clicks in turn
  5. After each exchange, judge whether it is consistent with the original picture

python code

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))returnint(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 inrange(0, row):for j inrange(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 inrange(0, row):for j inrange(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 inrange(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 inenumerate(tileList):
# cv.imshow("tile"+str(k), tile)

dst = numpy.zeros((h, w, c), numpy.uint8)for i inrange(0, row):for j inrange(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 inrange(1, row):
 src2[i*offset_h-1:i*offset_h]= numpy.zeros((1,w,3), numpy.uint8)for j inrange(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)

reference

90 The classic "mobile game" of the era-puzzle board game Opencv implementation

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

OpenCV Python implements puzzle games
Python implements minesweeper games
Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements man-machine gobang
Python implements Tetris game
Python implements image stitching
Python implements minesweeper game
Python implements scanning tools
Can python develop games
Python implements threshold regression
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python install OpenCV sample code
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
python opencv for image stitching
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python version of OpenCV installation
Python implements image stitching function
Python implements the brick-and-mortar game
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down