手动辅助摆扑克牌

This is a sample Python script.

Press Shift+F10 to execute it or replace it with your code.

Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import cv2
import numpy as np
import os
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f’Hi, {name}’) # Press Ctrl+F8 to toggle the breakpoint.

Press the green button in the gutter to run the script.

def onmouse(event,x,y,flag,param):
if event== cv2.EVENT_LBUTTONDOWN:
print(x,"–",y)

def stackImages(scale,imgArray):

rows = len(imgArray)

cols= len(imgArray[0])

rowsAvailable =isinstance(imgArray[0],list)

width = imgArray[0][0].shape[1]

height = imgArray[0][0].shape[0]

if rowsAvailable:

for x in range(0,rows):

for y in range(0,cols):

if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:

imgArray[x][y] = cv2.resize(imgArray[x][y],(0,0),None,scale,scale)

else:

imgArray[x][y] = cv2.resize(imgArray[x][y],(imgArray[0][0].shape[1],imgArray[0][0].shape[0]),None,scale,scale)

if len(imgArray[x][y].shape)==2: imgArray[x][y]=cv2.cvtColor(imgArray[x][y],cv2.COLOR_BGR2GRAY)

imageBlank = np.zeros(((height,width,3),np.uint8))

hor = [imageBlank]*rows

hor_con = [imageBlank]*rows

for x in range(0,rows):

hor[x] = np.hstack(imgArray[x])

ver = np.vstack(hor)

else:

for x in range(0,rows):

if imgArray[x].shape[:2]==imgArray[0].shape[:2]:

imgArray[x]=cv2.resize(imgArray[x],(0,0),None,scale,scale)

else:

imgArray[x]=cv2.resize(imgArray[x],(imgArray(0).shape[1],imgArray[0].shape[0]),None,scale,scale)

if len(imgArray[x].shape)==2: imgArray[x]=cv2.cvtColor(imgArray[x],cv2.COLOR_BGR2GRAY)

hor = np.hstack(imgArray)

ver=hor

return ver

def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
# & 输出一个 rows * cols 的矩阵(imgArray)
print(rows,cols)
# & 判断imgArray[0] 是不是一个list
rowsAvailable = isinstance(imgArray[0], list)
# & imgArray[][] 是什么意思呢?
# & imgArray[0][0]就是指[0,0]的那个图片(我们把图片集分为二维矩阵,第一行、第一列的那个就是第一个图片)
# & 而shape[1]就是width,shape[0]是height,shape[2]是
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]

# & 例如,我们可以展示一下是什么含义
# cv2.imshow("img", imgArray[0][1])

if rowsAvailable:
    for x in range (0, rows):
        for y in range(0, cols):
            # & 判断图像与后面那个图像的形状是否一致,若一致则进行等比例放缩;否则,先resize为一致,后进行放缩
            if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
                imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
            else:
                imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
            # & 如果是灰度图,则变成RGB图像(为了弄成一样的图像)
            if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
    # & 设置零矩阵
    imageBlank = np.zeros((height, width, 3), np.uint8)
    hor = [imageBlank]*rows
    hor_con = [imageBlank]*rows
    for x in range(0, rows):
        hor[x] = np.hstack(imgArray[x])
    ver = np.vstack(hor)
# & 如果不是一组照片,则仅仅进行放缩 or 灰度转化为RGB
else:
    for x in range(0, rows):
        if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
            imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
        else:
            imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
        if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
    hor= np.hstack(imgArray)
    ver = hor
return ver

if name == ‘main’:
width,height = 250,350

src=cv2.imread("card.jpg")
src=cv2.resize(src,(width,height))
pts1 = np.float32([[127,257],[332,218],[176,571],[416,524]])
#cv2.imwrite("card.jpg",src)
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
hor = np.hstack((src,src))
ver = np.vstack((hor,hor))
#pts1 = np.float32([[0, 0], [width, 0], [height, 0], [width, height]])
#src = src[0:height,0:width]
matrix = cv2.getPerspectiveTransform(pts1,pts2)
img_out = cv2.warpPerspective(src,matrix,(width,height))
imgstack = stackImages(0.5,[src,src,src])

# gaus_img = cv2.GaussianBlur(src,(7,7),0,0)
# # src = cv2.resize(src,(200,400))
# canny_img = cv2.Canny(src,100,200)
# cv2.line(src,(0,0),(src.shape[1],src.shape[0]),(255,255,0),2)
# cv2.rectangle(src,(0,0),(200,300),(0,0,255),cv2.FILLED)
# cv2.circle(src,(400,50),30,(255,0,255),2)
# cv2.putText(src,"OpenCV",(20,30),cv2.FONT_HERSHEY_SCRIPT_COMPLEX,2,(255,0,0),2)
cv2.imshow("origin",src)
cv2.setMouseCallback("origin", onmouse)
cv2.imshow("output", img_out)
cv2.imshow("Array Images",imgstack)

cv2.waitKey(0)

See PyCharm help at https://www.jetbrains.com/help/pycharm/

猜你喜欢

转载自blog.csdn.net/tel_1392/article/details/113785312