[DIP] opencv : Region of Interest (ROI) : cv2.selectROI

2022. 10. 3. 20:52·Programming/DIP
728x90
728x90

cv2에서 사각형의 ROI를 선택하는 가장 쉬운 방법.

 

Signature

ret_val = cv2.selectROI(
    [window_name], 
    img 
    [, showCrossHair=True, fromCenter=False]
    )
  • window_name : ROI 선택을 수행할 window이름. str
  • img : 보여질 이미지.
  • showCrossHair : ROI 중심에 십자모양 표시 여부
  • fromCenter : 마우스 시작지점을 영역의 중심으로 간주
  • ret_val = (x,y,w,h) of ROI

Sample code

import cv2
import numpy as np
import os

d_path = os.path.dirname(__file__)
img_path = f'{d_path}/lena.png'

img = cv2.imread(img_path)
cv2.imshow('img', img)
x,y,w,h = cv2.selectROI('img', img, False)

print(cv2.getWindowProperty('img', cv2.WND_PROP_VISIBLE))
if w and h: #선택시 0이 아닌 수로 w,h가 설정됨.
    roi = img[y:y+h, x:x+w]
    cv2.imshow('roi', roi)
    cv2.moveWindow('roi',0,0)
    cv2.imwrite(f'{d_path}/roi2.png', roi)
    print('Enter ESC to quit!')

while cv2.getWindowProperty('img', cv2.WND_PROP_VISIBLE) >=1: # check whether x button is clicked.
    # c = input('Quit? (enter q)')
    # if c == 'q':
    #     break
    key_code = cv2.waitKey(50)&0xff #50msec
    if key_code == 27: #check whether ESC key is entered.
        break


cv2.destroyAllWindows()
  • 선택시 space or Enter 를 누르면 됨.
  • 선택을 취소하고 싶을 경우 c키 누름 : 이 경우, ret_val는 모두 0이 됨.

아래 그림에서 푸른색의 사각형이 선택한 ROI임.

728x90

'Programming > DIP' 카테고리의 다른 글

[NumPy] Fancy Indexing & Combined Indexing  (1) 2022.10.03
[DIP] OpenCV : Region of Interest (ROI) : Callback으로 구현.  (0) 2022.10.03
[NumPy] Broadcasting  (0) 2022.09.27
[DIP] Signal to Noise : Amplitude, Power, and Differential SNR  (0) 2022.09.26
[DIP] Line pairs per millimeters and Bar phantom  (0) 2022.09.26
'Programming/DIP' 카테고리의 다른 글
  • [NumPy] Fancy Indexing & Combined Indexing
  • [DIP] OpenCV : Region of Interest (ROI) : Callback으로 구현.
  • [NumPy] Broadcasting
  • [DIP] Signal to Noise : Amplitude, Power, and Differential SNR
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (787)
      • Private Life (15)
      • Programming (206)
        • DIP (116)
        • ML (35)
      • Computer (120)
        • CE (54)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (368)
        • Signals and Systems (115)
        • Math (176)
        • Linear Algebra (33)
        • Physics (43)
        • 인성세미나 (1)
      • 정리필요. (61)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (7)
        • PET Study 2009 (1)
        • 방사선 장해방호 (5)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    cv2
    opencv
    Term
    SIGNAL
    Optimization
    SS
    math
    인허가제도
    fourier transform
    numpy
    random
    function
    ML
    Python
    Probability
    linear algebra
    signals_and_systems
    signal_and_system
    Programming
    Vector
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
[DIP] opencv : Region of Interest (ROI) : cv2.selectROI
상단으로

티스토리툴바