python dtype不情愿地更改

时间:2018-08-03 17:35:53

标签: python-3.x variables

我有一个问题,我正在使用opencv,我想在“实时”中使用BFM在屏幕的一部分中寻找对象,但是不幸的是,我的ndarray之一奇怪地切换为str tytybe并导致错误

import numpy as np
from PIL import ImageGrab
import cv2
import time
from matplotlib import pyplot as plt

global n
global image_memory
global matches
global original_img
global recorded_img

n = 0
image_memory = []
matches = ""
original_img = ""
recorded_img = ""

def BGR_to_grey(recorded_img):
    recorded_img = cv2.cvtColor(recorded_img, cv2.COLOR_BGR2GRAY)
    return recorded_img

def canny_img(recorded_img):
    recorded_img = cv2.Canny(recorded_img, threshold1=150, threshold2 =250)
    return recorded_img

def substract_img(recorded_img):

    image_memory.append(recorded_img)
    if n != 0 :
        recorded_img = image_memory[n] - image_memory[n-1]
    n += 1
    return recorded_img

def erode_img(recorded_img):
    kernel = np.ones((10,10),np.uint8)
    recorded_img = cv2.erode(recorded_img, kernel, iterations = 1)
    return recorded_img

def dilate_img(recorded_img):
    kernel = np.ones((5,5),np.uint8)
    recorded_img = cv2.dilate(recorded_img, kernel, iterations = 1)
    return recorded_img

def BFmatcher_img():

    orb = cv2.ORB_create()
    img_ref = cv2.imread('fishing head 2.png',0)
    original_img = cv2.imread('fishing head 3.png',0)
    #original_img = image

    #print (type (original_img))
    print (type (recorded_img))
    #print (type (img_ref))
    #print (image)

    kp1, des1 = orb.detectAndCompute(img_ref,None)
    kp2, des2 = orb.detectAndCompute(recorded_img,None)
    bfm = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)
    matches = bfm.match(des1,des2)
    matches = sorted(matches, key = lambda x:x.distance)
    result_img = cv2.drawMatches(img_ref,kp1,recorded_img,kp2,matches[:10],None,flags = 2)
    new_screen2 = result_img
    cv2.imshow('window2', new_screen2)



def screen_record(): 



    last_time = time.time()
    while(True):
        # 800x600 windowed mode, at the top left position of your main screen.
        # 40 px accounts for title bar. 
        recorded_img = np.array(ImageGrab.grab(bbox=(200,40,500,100)))
        print(type(recorded_img))
        BFmatcher_img()
        new_screen = dilate_img(canny_img(erode_img(BGR_to_grey(recorded_img))))
        print('loop took {} seconds'.format(time.time()-last_time))
        last_time = time.time()
        cv2.imshow('window', new_screen)
        #cv2.imshow('window3',cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
        if cv2.waitKey(5025) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break




screen_record()

我得到这份报告

类'numpy.ndarray' 'str'类 追溯(最近一次通话):   文件“ Z:\ python encoding \ part-1-Getting-Visuals test5 debug test.py”,第96行,在     屏幕记录()   screen_record中的文件“ Z:\ python encoding \ part-1-Getting-Visuals test5 debug test.py”,第79行     BFmatcher_img()   BFmatcher_img中的文件“ Z:\ python encoding \ part-1-Getting-Visuals test5 debug test.py”,第59行     kp2,des2 = orb.detectAndCompute(recorded_img,无) TypeError:图像不是numpy数组,也不是标量 [在0.9秒内完成]

同一变量的dtype变化很大,我真的不知道如果有人对此有什么了解,对我真的很有帮助。

0 个答案:

没有答案