numpy 如何使用opencv [closed]使用4个坐标对图像进行遮罩并找到图像的中心

dpiehjr4  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(108)

已关闭。此问题需要超过focused。当前不接受答案。
**想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

2天前关闭。
Improve this question
我想找到一个鼠标点击图像的4个坐标。然后我想在设定的坐标处遮罩这个图像,并找到这个遮罩图像的中心。
用鼠标左键点击我得到的坐标,但我似乎不能找到一个很好的解决办法,其余的。

import cv2
import numpy as np

#Mouse_click_events anzeigen 
events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)

#Variable zum speichern der Coordinaten
refPt = []
counter = 0

#click event function
def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x,",",y)
        refPt.append([x,y])
        font = cv2.FONT_HERSHEY_SIMPLEX
        strXY = str(x)+", "+str(y)
        cv2.putText(img, strXY, (x,y), font, 0.5, (255,255,0), 2)
        cv2.imshow("image", img)
        counter +=1

    if event == cv2.EVENT_RBUTTONDOWN:
        blue = img[y, x, 0]
        green = img[y, x, 1]
        red = img[y, x, 2]
        font = cv2.FONT_HERSHEY_SIMPLEX
        strBGR = str(blue)+", "+str(green)+","+str(red)
        cv2.putText(img, strBGR, (x,y), font, 0.5, (0,255,255), 2)
        cv2.imshow("image", img)
    
        


img = cv2.imread("C:/Users/FK8ROME/Desktop/Defrost/Scripts/images/example.jpg")
cv2.imshow("image", img)

cv2.setMouseCallback("image", click_event)



cv2.waitKey(0)
cv2.destroyAllWindows()
hk8txs48

hk8txs481#

您可以使用我在下面编写的mask_area函数。

def mask_area(frame, refPt):
    # Create a zero array and apply the mask
    mask = np.zeros_like(frame)
    cv2.fillPoly(mask, np.int32([refPt]), (255, 255, 255))
    # Get the masked image
    result = cv2.bitwise_and(frame, mask)
    # Find the minimum and maximum values of the coordinates
    min_x = min(refPt, key=lambda x: x[0])[0]
    max_x = max(refPt, key=lambda x: x[0])[0]
    min_y = min(refPt, key=lambda x: x[1])[1]
    max_y = max(refPt, key=lambda x: x[1])[1]
    # Calculate the average values
    mean_x = (min_x + max_x) // 2
    mean_y = (min_y + max_y) // 2
    center = (mean_x, mean_y)
    # Put a dot at the center of the masked image
    cv2.circle(result, center, 5, (0, 0, 255), -1)
    # Show the masked image on the screen
    cv2.imshow("mask", result)

下面是完整的代码:

import cv2
import numpy as np

#Mouse_click_events anzeigen 
events = [i for i in dir(cv2) if 'EVENT' in i]
print(events)

#Variable zum speichern der Coordinaten
refPt = []
counter = 0

def mask_area(frame, refPt):
    # Create a zero array and apply the mask
    mask = np.zeros_like(frame)
    cv2.fillPoly(mask, np.int32([refPt]), (255, 255, 255))
    # Get the masked image
    result = cv2.bitwise_and(frame, mask)
    # Find the minimum and maximum values of the coordinates
    min_x = min(refPt, key=lambda x: x[0])[0]
    max_x = max(refPt, key=lambda x: x[0])[0]
    min_y = min(refPt, key=lambda x: x[1])[1]
    max_y = max(refPt, key=lambda x: x[1])[1]
    # Calculate the average values
    mean_x = (min_x + max_x) // 2
    mean_y = (min_y + max_y) // 2
    center = (mean_x, mean_y)
    # Put a dot at the center of the masked image
    cv2.circle(result, center, 5, (0, 0, 255), -1)
    # Show the masked image on the screen
    cv2.imshow("mask", result)

#click event function
def click_event(event, x, y, flags, param):
    global counter
    if event == cv2.EVENT_LBUTTONDOWN:
        print(x,",",y)
        refPt.append([x,y])
        font = cv2.FONT_HERSHEY_SIMPLEX
        strXY = str(x)+", "+str(y)
        cv2.putText(img, strXY, (x,y), font, 0.5, (255,255,0), 2)
        cv2.imshow("image", img)
        counter +=1
        if(counter>3): # or if(len(refPt)>3):
            mask_area(img,refPt)

    if event == cv2.EVENT_RBUTTONDOWN:
        blue = img[y, x, 0]
        green = img[y, x, 1]
        red = img[y, x, 2]
        font = cv2.FONT_HERSHEY_SIMPLEX
        strBGR = str(blue)+", "+str(green)+","+str(red)
        cv2.putText(img, strBGR, (x,y), font, 0.5, (0,255,255), 2)
        cv2.imshow("image", img)

img = cv2.imread("frame.jpg")
cv2.imshow("image", img)

cv2.setMouseCallback("image", click_event)

cv2.waitKey(0)
cv2.destroyAllWindows()

相关问题