简体   繁体   中英

How to Convert the image into an array?

I'm making a program that would get the images of the board and turn it into an array. My idea is that since I could already get per tiles of the board, I'll use the pkl file to recognize it and convert it into an array but I always get this error. Error picture . Here is the code that I have so far. My goal is to make an array of the board that has been captured which will look like this Output This is my starting image Image

import cv2
import numpy as np
import copy
import os
import conf
from sklearn.externals import joblib

class ImageProcess:
    param1 = 70
    param2 = 15
    fields1 = fields2 = []
    FieldTable = [[1 for j in range(15)] for i in range(15)]

    def mouseEvent(self, event, x, y, flags, param):                                
        if event == cv2.EVENT_MOUSEMOVE:                                            
            self.ImgCopy2 = copy.deepcopy(self.ImgCopy)                             
            cv2.circle(self.ImgCopy2, (x, y), 3, (0, 255, 0), -1)                   

        if event == cv2.EVENT_LBUTTONUP:                                            
            self.splitPointsTemp.append([x, y])                                     
            cv2.circle(self.ImgCopy, (x, y), 3, (255, 255, 255), -1)                
            self.ImgCopy2 = copy.deepcopy(self.ImgCopy)                             

    def calibrate_board(self,img):                                                                                                                
        self.img = img                                                              
        self.ImgCopy = copy.deepcopy(self.img)                                      
        self.ImgCopy2 = copy.deepcopy(self.img)                                     
        cv2.namedWindow('image')                                                    
        cv2.setMouseCallback('image', self.mouseEvent)                              
        self.splitPointsTemp = []                                                   
        while True:                                                                 
            cv2.imshow('image', self.ImgCopy2)                                      
            if len(self.splitPointsTemp) == 4 or cv2.waitKey(1) & 0xFF == 27:      
                break                                                             
        if len(self.splitPointsTemp) == 4:                                 
            self.splitPoints = self.splitPointsTemp                          

        conf.set('splitPoints', self.splitPoints)                          
        cv2.destroyAllWindows()                                    

    def imageSplit(self):
        self.calibrate_board(self.img)
        majorCorners = conf.get('splitPoints')                                     

        self.splitPoints = eval(str(majorCorners))
        if len(self.splitPoints) != 4:
            print("Board not detected!")
        rows, cols, ch = self.img.shape
        pts1 = np.float32(self.splitPoints)
        pts2 = np.float32([[0, 0], [600, 0], [0, 600], [600, 600]])

        M = cv2.getPerspectiveTransform(pts1, pts2)
        self.trimmed = cv2.warpPerspective(self.img, M, (600, 600))

        #~~~~~~~~~~~~~~IMAGE ROTATION~~~~~~~~~~~~

        rows, cols, depth = self.trimmed.shape  # Without this code the image will be upside down

        matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45 * int(conf.get('rotate')), 1)
        self.trimmed = cv2.warpAffine(self.trimmed, matrix, (cols, rows))

        #~~~~~~~~~~~~~~~generating edges image~~~~~~~~~~~~
        self.edgesImage = cv2.cvtColor(self.trimmed, cv2.COLOR_BGR2GRAY)
        self.edgesImage = cv2.medianBlur(self.edgesImage, 5)                # CHANGE
        self.edgesImage = cv2.GaussianBlur(self.edgesImage, (3, 3), 0)      # CHANGE
        #self.edgesImage = copy.deepcopy(image)
        self.edgesImage = cv2.Canny(self.edgesImage, float(conf.get('param1')), float(conf.get('param2'))) #90, 200
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.normalImage = copy.deepcopy(self.trimmed)

        #~~~~~ROTATING FOR ALGORITHM:~~~~~~~~~~~~~~~~~~~~~~
        rows, cols, depth = self.trimmed.shape
        matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), -180, 1)
        self.trimmed = cv2.warpAffine(self.trimmed, matrix, (cols, rows))

        points = []
        for i in range(1, 602, 40):
            for j in range(1, 602, 40):
                points.append([j, i])

        dic = {}
        for i in range(0, 16):
            for j in range(0, 16):
                dic[i, j] = points[(16 * i) + j]

        self.trimmed = cv2.cvtColor(self.trimmed, cv2.COLOR_BGR2GRAY)
        for i in range(15):
            for j in range(15):
                x1 = dic[i, j][0]
                y1 = dic[i, j][1]
                x2 = dic[i + 1, j + 1][0]
                y2 = dic[i + 1, j + 1][1]
                self.FieldTable[i][j] = self.trimmed[y1:y2, x1:x2]


        for i in range(15):
            for j in range(15):
                x1 = dic[i, j][0]
                y1 = dic[i, j][1]
                x2 = dic[i + 1, j + 1][0]
                y2 = dic[i + 1, j + 1][1]
                cv2.circle(self.trimmed, (x1, y1), 2, (0, 0, 255))
                cv2.circle(self.trimmed, (x2, y2), 2, (0, 0, 255))

        cv2.imshow('result', self.trimmed) ###
        cv2.waitKey(0) ###

    def frame_table(self, image):
        Trydetect = joblib.load('Tilesdetect.pkl')
        self.img = copy.deepcopy(image)
        self.imageSplit()
        result = [[0 for i in range(15)] for j in range(15)] #This is where the column and rows of the board are being discovered
        num=0
        #path = "C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python37-32\\Thesis\\scrabble-draft\\Storing"
        while True:   # This is the part where it will get all the extracted tiles and save it in a folder
            for i in range(15):
                for j in range(15):
                        cv2.imshow('tile', self.FieldTable[i][j])
                        cv2.waitKey(0)
                        Preditcion = Trydetect.predict(self.FieldTable[i][j].reshape(1,-1))
                        #cv2.imwrite(os.path.join(path, "Image {0}.jpg".format(num)),self.FieldTable[i][j]) #saved each file
                        #if cv2.waitKey(0) & 0xFF == ord('q'): Uncomment if you want manually
                           # break
                        num += 1
                        #result[i][j] = self.searchForPiece(self.FieldTable[i][j])
            return result

if __name__ == '__main__':

    proc = ImageProcess()
    img = cv2.imread("Scrabble_pic.jpg")
    #img = cv2.resize(img, (600, 600))

    print(proc.frame_table(img))
import numpy as np
import cv2
from PIL import ImageGrab

def convert_image_to_array():
    img = ImageGrab.grab()
    img_np = np.array(img)

Output

[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 ...

 [[ 20  20  20]
  [ 16  16  16]
  [ 17  17  17]
  ...
  [ 17  17  17]
  [ 18  18  18]
  [ 18  18  18]]

 [[ 17  17  17]
  [ 16  16  16]
  [ 17  17  17]
  ...
  [ 17  17  17]
  [ 18  18  18]
  [ 18  18  18]]

 [[ 17  17  17]
  [ 17  17  17]
  [ 16  16  16]
  ...
  [ 17  17  17]
  [ 16  16  16]
  [ 18  18  18]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM