前提・実現したいこと
pythonのopencvで色付きの線を書きたいです
直接値を入れるとうまくいくのですが、ほかの画像から読み取った値を代入しようと線が表示されません。
cv2.rectangleを使用したいと考えています。やり方をご教授お願いします。
修正
2018/12/06 指摘されたmy_colorのprint文とログを追記しました。
該当のソースコード
python
1def paint_block_on_result_canvas(img, y, x): 2 my_color = (int(img[y][x][0]), int(img[y][x][1]), int(img[y][x][2])) 3 4 # 認識結果のウインドウ 5 h, w, c = img.shape 6 img = np.ones((h, w, 3))*255 7 8 # -----------ご指摘のprint文、を挿入しました----------- 9 print('paint_block_on_result_canvas: my_color={}'.format(my_color)) 10 11 cv2.rectangle(img, (150, 25), (300, 50), my_color, -1) 12 # cv2.rectangle(img, (150, 25), (300, 50), (0, 0, 255), -1) # これだとうまくいく 13 14 cv2.imshow("result_img", img) 15
ログ
ご指摘のmy_colorのprintを追加しました。
Connected to pydev debugger (build 183.4284.139)
mouse_call_back_test: img.shape=(589, 960, 3)
-------click event start-------
print_position: (x,y)=(157, 238)
print_position: gbr:[188 146 101]
paint_block_on_result_canvas: my_color=(188, 146, 101)
-------click event end-------
-------click event start-------
print_position: (x,y)=(343, 384)
print_position: gbr:[105 77 47]
paint_block_on_result_canvas: my_color=(105, 77, 47)
-------click event end-------
-------click event start-------
print_position: (x,y)=(400, 262)
print_position: gbr:[46 33 19]
paint_block_on_result_canvas: my_color=(46, 33, 19)
-------click event end-------
補足:全ソース
python
1import cv2 2import numpy as np 3 4 5def paint_block_on_result_canvas(img, y, x): 6 my_color = (int(img[y][x][0]), int(img[y][x][1]), int(img[y][x][2])) 7 8 # 認識結果のウインドウ 9 h, w, c = img.shape 10 img = np.ones((h, w, 3))*255 11 12 # -----------ご指摘のprint文、を挿入しました----------- 13 print('paint_block_on_result_canvas: my_color={}'.format(my_color)) 14 15 cv2.rectangle(img, (150, 25), (300, 50), my_color, -1) 16 # cv2.rectangle(img, (150, 25), (300, 50), (0, 0, 255), -1) # これだとうまくいく 17 18 cv2.imshow("result_img", img) 19 20 21def print_position(event, x, y, flags, img): 22 if event == cv2.EVENT_LBUTTONDBLCLK: 23 print('-------click event start-------') 24 print('print_position: (x,y)=({x}, {y})'.format(x=x, y=y)) 25 print('print_position: gbr:{gbr_value}'.format(gbr_value=(img[y][x]))) 26 27 paint_block_on_result_canvas(img, y, x) 28 print('-------click event end-------') 29 30 31def mouse_call_back_test(): 32 img = cv2.imread("data/src/Berry.jpg") 33 34 print('mouse_call_back_test: img.shape={}'.format(img.shape)) 35 cv2.imshow("img", img) 36 cv2.setMouseCallback("img", print_position, img) 37 cv2.waitKey(0) 38 cv2.destroyAllWindows() 39 40 41def main(): 42 mouse_call_back_test() 43 44 45if __name__ == '__main__': 46 main() 47
回答1件
あなたの回答
tips
プレビュー