728x90
728x90
cv2에서 사각형의 ROI를 선택하는 가장 쉬운 방법.
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
- 선택시 `space` or `Enter` 를 누르면 됨.
- 선택을 취소하고 싶을 경우 c키 누름 : ret_val는 모두 0으로
import cv2
import numpy as np
img_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('roi2.png', roi)
while cv2.getWindowProperty('img', cv2.WND_PROP_VISIBLE) >=1: # check whether x button is clicked.
key_code = cv2.waitKey(50) #50msec
print(key_code)
if key_code == 27: #check whether ESC key is entened.
break
cv2.destroyAllWindows()
아래 그림에서 푸른색의 사각형이 선택한 ROI임.
'Programming > DIP' 카테고리의 다른 글
[NumPy] Fancy Indexing & Combined Indexing (1) | 2022.10.03 |
---|---|
[DIP] opencv : Region of Interest (ROI) : (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 |