【OpenCV】원 그리기, 마우스 이벤트로 이동과 확대 【Python】
OpenCV에서 원을 그리며 마우스 이벤트로 이동 및 확대를 구현했습니다.
왼쪽 클릭한 첫 번째 위치에 원을 배치합니다
원주 내에서 왼쪽 클릭한 상태로 드래그하면 원의 위치가 이동합니다. 
원주에서 왼쪽 클릭한 상태로 드래그하면 원의 반경이 변화합니다. 
왼쪽 클릭 판정
ON : isGrabbed = True 
OFF: isGrabbed = False 
포인터 위치 결정
 
( isInside 및 isOntheLine 모두 True 모두 존재하지 않음)
아래 샘플 코드
※ testIm = cv2.imread(r'C:\****\sample_pic.jpg') 로 이미지를 불러옵니다. 사용하는 이미지의 주소로 다시 작성하여 사용하십시오. 
전역 변수를 삭제한 개정판은 여기 
import cv2
import numpy as np
import math
def draw_circle(event,x,y,flags,params):
    global cnt,windowName,testIm,centerX,centerY,r,color,distX,distY,isGrabbed,isInside,isOntheLine,posState
    distR = math.ceil(math.sqrt((x-centerX)**2 + (y-centerY)**2))
    if event == cv2.EVENT_LBUTTONDOWN:
        cnt += 1
        isGrabbed = True
        if cnt == 1:
            [centerX,centerY] = [x,y]
            print([centerX,centerY])
        if cnt>1:
            if posState == 0: # inside
                isInside = True
                isOntheLine = False
                distX = centerX - x
                distY = centerY - y
            elif posState == 1: # on the line
                isInside = False
                isOntheLine = True
            elif posState == 2:
                isInside = False
                isOntheLine = False
    if event == cv2.EVENT_LBUTTONUP:
        isGrabbed = False
    if event == cv2.EVENT_MOUSEMOVE:
        if cnt == 0:
            [centerX,centerY] = [x,y]
        if cnt > 0:
            if isGrabbed == False:
                if distR < r-5: # inside
                    posState = 0
                    color = (255,0,0)
                elif (distR <= r+5) & (distR >= r-5): # on the line
                    posState = 1
                    color = (0,255,0)
                elif distR > r+5: # outside
                    posState = 2
                    color = (0,0,255)
            else:
                if isInside == True:
                    centerX = x+distX
                    centerY = y+distY
                elif isOntheLine == True:
                    r = distR
        img_tmp = testIm.copy()
        cv2.circle(img_tmp,(centerX,centerY), r, color, 5)
        cv2.imshow(windowName,img_tmp)
if __name__=="__main__":
    global cnt,windowName,testIm,centerX,centerY,r,color,distX,distY,isGrabbed,isInside,isOntheLine,posState
    centerX = 0                     #保存すべきX座標
    centerY = 0                     #保存すべきY座標
    cnt = 0                         #画像内でのクリック回数
    r = 50
    distX = 0
    distY = 0
    posState = 0 # 0,1,2
    isGrabbed = False
    isInside = False
    isOntheLine = False
    color = (0,0,255)
    testIm = cv2.imread(r'C:\****\sample_pic.jpg')
    testIm = testIm/255
    windowName = "Select window"
    cv2.namedWindow(windowName)
    cv2.setMouseCallback(windowName,draw_circle)
    cv2.imshow(windowName,testIm)
    key = cv2.waitKey()
※마우스 이벤트의 선언으로 글로벌 변수를 사용하고 있으므로 주의해 주세요.
Reference
이 문제에 관하여(【OpenCV】원 그리기, 마우스 이벤트로 이동과 확대 【Python】), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/NagayamaT/items/e044f36ca62b56992302텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)