0

みなさん、opencv 3.x と python 3.x の使用に問題があります。私がやりたいのは、写真に基本的な長方形を描くことですが、長方形は決して描かれません。この同様のスレッドを読みましたが、私のせいでは役に立ちませんでした。 Python OpenCV: 長方形を描画するためのマウス コールバック

どなたかヒントを頂ければ幸いです。

#!/usr/bin/env python3
import cv2
import numpy as np
Path = 'picture.jpg'
image_float_size = 400.0
image_int_size = int(image_float_size)
color = [0,255,0]
rectangle = False

def on_event(event,x,y,flags,param):
    global startpointx,startpointy,rectangle
    if event == cv2.EVENT_LBUTTONDOWN:
        rectangle = True
        startpointx = x
        startpointy = y
        print('Down',x,y) #debugging
        cv2.rectangle(resized,(x,y),(x,y),(0,255,0),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        rectangle = False
        print('Up',x,y)
        cv2.rectangle(resized,(startpointx,startpointy),(x,y),(0,255,0),-1)

    elif event == cv2.EVENT_MOUSEMOVE:
        if rectangle:
            print('Move',startpointx,startpointy,x,y)#debugging
            cv2.rectangle(resized,(startpointx,startpointy),(x,y),(0,255,0),-1)

# Read the image and convert it into gray
image = cv2.imread(Path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# resize the image
ration = image_float_size / gray_image.shape[1]
dim = (image_int_size,int(gray_image.shape[0]*ration))
resized = cv2.resize(gray_image, dim, interpolation = cv2.INTER_AREA)

# set window for the image
cv2.namedWindow('window')

# mouse callback
cv2.setMouseCallback('window',on_event)

# wait forever for user to press any key, after key pressed close all windows
while True:
    cv2.imshow('window',resized)
    if cv2.waitKey(0):
        break
        cv2.destroyAllWindows()
4

1 に答える 1

1

cv2.waitKey(0) は無期限に待機するため、描画 (cv2.imshow を使用した画像の表示) は 1 回だけです。ゼロ以外の引数を使用すると、そのミリ秒数だけ待機します。ただし、画像を常に書き換え/変更していることに注意してください。これはおそらくあなたが望むものではありません。最初に画像の一時的な(描画)コピーを作成し、新しい描画(長方形)の前に元の画像から毎回復元する必要があると思います。

#!/usr/bin/env python3
import cv2
import numpy as np
Path = 'data/lena.jpg'
image_float_size = 400.0
image_int_size = int(image_float_size)
color = [0,255,0]
rectangle = False

def on_event(event,x,y,flags,param):
    global draw_image
    global startpointx,startpointy,rectangle
    if event == cv2.EVENT_LBUTTONDOWN:
        rectangle = True
        startpointx = x
        startpointy = y
        print('Down',x,y) #debugging
        draw_image = resized.copy()
        cv2.rectangle(draw_image,(x,y),(x,y),(0,255,0))

    elif event == cv2.EVENT_LBUTTONUP:
        rectangle = False
        print('Up',x,y)
        draw_image = resized.copy()
        cv2.rectangle(draw_image,(startpointx,startpointy),(x,y),(0,255,0))

    elif event == cv2.EVENT_MOUSEMOVE:
        if rectangle:
            print('Move',startpointx,startpointy,x,y)#debugging
            draw_image = resized.copy()
            cv2.rectangle(draw_image,(startpointx,startpointy),(x,y),(0,255,0))

# Read the image and convert it into gray
image = cv2.imread(Path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# resize the image
ration = image_float_size / gray_image.shape[1]
dim = (image_int_size,int(gray_image.shape[0]*ration))
resized = cv2.resize(gray_image, dim, interpolation = cv2.INTER_AREA)
draw_image = resized.copy()

# set window for the image
cv2.namedWindow('window')

# mouse callback
cv2.setMouseCallback('window',on_event)

while True:
    cv2.imshow('window', draw_image)
    ch = 0xFF & cv2.waitKey(1)
    if ch == 27:
        break

cv2.destroyAllWindows()
于 2015-05-20T08:08:00.577 に答える