OpenCV框架与numpy切片不一致

时间:2019-06-25 09:33:27

标签: python numpy opencv

我试图将检测到的斑点打印在黑色NumPy阵列框架上,该阵列框架与序数框架的大小相同,即(640,1280)

操作系统     的Ubuntu 架构(例如x86)     x64 opencv4.1.0-python版本3.6.8

import cv2
import numpy as np


cap = cv2.VideoCapture('1.mp4')

e1_x1 = 760
e1_y1 = 150
e1_x2 = 845
e1_y2 = 195

e2_x1 = 800
e2_y1 = 150
e2_x2 = 880
e2_y2 = 200


black = np.zeros((640, 1280, 3), dtype = "uint8")
def blobSelection(eye,black):
    keypoints=blob_detector.detect(eye)
    for ma in keypoints:
        print(x,y)
        black = cv2.drawMarker(black, tuple(int(i) for i in ma.pt), color=(0, 255, 0))
    return black

while True:
    _, frame = cap.read()

    e1 = frame[e1_y1:e1_y2, e1_x1:e1_x2]
    cv2.rectangle(black, (e1_x1,e1_y1), (e1_x2,e1_y2), (255,0,0), 1, 8, 0)

    cv2.rectangle(frame, (e1_x1,e1_y1), (e1_x2,e1_y2), (255,0,0), 1, 8, 0)
    cv2.putText(frame, "e1", (e1_x1, e1_y2), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)
    cv2.rectangle(black, (e2_x1,e2_y1), (e2_x2,e2_y2), (255,0,0), 1, 8, 0)

    cv2.rectangle(frame, (e2_x1,e2_y1), (e2_x2,e2_y2), (255,0,0), 1, 8, 0)
    cv2.putText(frame, "e2", (e2_x1, e2_y2), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)
    e2 = frame[e2_y1:e2_y2, e2_x1:e2_x2]

    e1 = eyeDetection(e1, black)
    e2 = eyeDetection(e2,black)

    cv2.imshow('Orginal', frame)
    cv2.imshow('Detected eye', black)

    key = cv2.waitKey(1)
    if key == 27:
        break
cap.release()
cv2.destroyAllWindows()

enter image description here 我希望将源图像中该名男子的两只眼睛同时粘贴到黑色背景上。

1 个答案:

答案 0 :(得分:0)

猜到了

e1 = eyeDetection(e1, black)

实际上将称为

def blobSelection(eye,black):

您需要传递偏移以将其绘制在正确的位置,因为您要对作为框架一部分的眼睛进行检测,并且可以将关键点绘制在眼睛内部而不是黑色。

keypoints=blob_detector.detect(eye)

我的建议是做这样的事情:

def blobSelection(eye,black, offset):
    keypoints=blob_detector.detect(eye)
    for ma in keypoints:
        pos = ma.pt
        black = cv2.drawMarker(black, (ma.pt[0] + offset[0], ma.pt[1] + offset[1]), color=(0, 255, 0))
    return black

您这样称呼:

e1 = blobSelection(e1, black, (e1_x1, e1_y1))

假设(e1_x1,e1_y1)是e1的左上角