0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python]拡大表示のウィンドウを追加する

Last updated at Posted at 2023-06-20

キャプチャした画像の拡大

OpenCVでマウス操作する (2) でズームするとあったので、
これかと思ったけど違ったので作り直し

マウスで場所取って、その部分を切り抜いて拡大する。
画面が遠いので、ちょっと拡大したいと思って作成。

mouse_x = 0
mouse_y = 0
def set_mouse_pos(event, x, y, flags, param):
    global mouse_x, mouse_y
    if (event == cv2.EVENT_LBUTTONDOWN or
        event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON) > 0):
        # print(event, x, y, flags, param)
        mouse_x = x
        mouse_y = y
        

cv2.namedWindow('cap')
cv2.setMouseCallback('cap', set_mouse_pos)

zoom_size = 100 # halfサイズ
zoom_scale = 5

while True:
    img = WindowCapture('MonsterX U3.0R')

    h, w = img.shape[:2]
    x = max(zoom_size, min(w - zoom_size, mouse_x))
    y = max(zoom_size, min(h - zoom_size, mouse_y))
    l, r = x - zoom_size, x + zoom_size
    d, u = y - zoom_size, y + zoom_size
    
    cv2.rectangle(img, (l, d), (r, u), color=(255, 255, 255))
    cv2.imshow("cap", img)
    
    
    zoom_img = img[d:u, l:r, :]
    zoom_img = cv2.resize(zoom_img, None, fx=zoom_scale, fy=zoom_scale)
    
    cv2.imshow("zoom", zoom_img)
    
    key = cv2.waitKey(100)
    if key == ord('q'):
        break
    

cv2.destroyAllWindows()

WindowCaptureは、[Python]マルチモニター環境でのウィンドウキャプチャ のものを流用。

直接キャプチャしたかったけど、MonsterXをOpenCVで直接キャプチャすると
古いせいかよく止まる。純正アプリだと止まらないので、画面キャプチャする方針へ。
(WindowCaptureはいじって枠と書けしてるけど、数値ハードコーディングなので、非公開w)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?