首页 > 解决方案 > 获取绘制圆的平均 RGB 值

问题描述

我编写了一个代码,使用鼠标单击在图像上绘制圆圈。绘制的圆的半径和中心存储在 .txt 文件中。

但是,我想在文本文件中添加另一个组件;- 绘制的圆圈内部的平均 RGB 值。我该怎么做呢?

我看过另一个 qs,作者建议使用面具。(圆圈的 RGB 平均值)是否可以在不添加蒙版的情况下这样做?

import cv2
import numpy as np
import math

drawing = False
colours=[(255,0,0),(0,255,0),(0,0,255),(204,0,204),(0,155,255),(0,223,255),(238,220,130),
        (255,79,79), (161,0,244),(32,178,170),(170,178,32)]

def draw_circle(event, x, y, flags, param):
    global x1, y1, drawing, radius, num, img, img2,z,circleProps
    if event == cv2.EVENT_LBUTTONDOWN:
    drawing = True
    x1, y1 = x, y
    radius = int(math.hypot(x - x1, y - y1))


    cv2.circle(img, (x1,y1), radius, (255, 0, 0), 1)




elif event == cv2.EVENT_MOUSEMOVE:
    if drawing == True:
        a, b = x, y
        if a != x & b != y:
            img = img2.copy()
            radius = int(math.hypot(a - x1, b - y1))
            cv2.circle(img, (x1,y1), radius, (255, 0, 0), 1)




elif event == cv2.EVENT_LBUTTONUP:
    drawing = False
    num += 1
    radius = int(math.hypot(x - x1, y - y1))

    z = (z + 1) % (len(colours) - 1)
    cv2.circle(img, (x1,y1), radius, colours[z], 1)     #just need outer circle colour to be different
    print("rad:",radius)
    circleProps += "%d "%num + "%d "%x1 + "%d "%y1 + "%d \n"%radius
    cv2.circle(img, (x1, y1), 1, (0, 255, 0), -1) #draws centre of circle
    #font = cv2.FONT_HERSHEY_SIMPLEX
    font = cv2.FONT_HERSHEY_PLAIN 
    cv2.putText(img, str(num), (x1 + radius, y1 + radius), font, 1, (200, 255, 155), 1, cv2.LINE_AA)
    img2 = img.copy()

elif event == cv2.EVENT_RBUTTONUP:
    pass

def handleKeyboard():
    if cv2.waitKey(20) == 38:
    print ('38')


if __name__ == "__main__":
    num = 0
    z = 0
    windowName = 'Drawing'
    circleProps ="Num | x1 | y1 | r \n"
    img = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
    img2 = img.copy()
    cv2.namedWindow(windowName)
    cv2.setMouseCallback(windowName, draw_circle)

while (True):
    cv2.imshow(windowName, img)
    # handleKeyboard()
    if cv2.waitKey(20) == 27:
        text_file = open("Output.txt", "w") # create txt file
        text_file.write(circleProps)
        text_file.close()
        break
    elif cv2.waitKey(20) == 38:
        print ('38')

cv2.destroyAllWindows()

编辑:

我刚刚将这些行添加到函数中,并且得到了 RGB 值。但是我不确定打印的值是否正确?有人可以澄清一下吗?

     elif event == cv2.EVENT_LBUTTONUP:
    drawing = False
    num += 1
    radius = int(math.hypot(x - x1, y - y1))

    z = (z + 1) % (len(colours) - 1)
    circle_drawn=cv2.circle(img, (x1,y1), radius, colours[z], 1) #lines added
    RGB=cv2.mean(circle_drawn) #lines added
    print("RGB: ", RGB) #lines added
    print("rad:",radius)
    circleProps += "%d "%num + "%d "%x1 + "%d "%y1 + "%d \n"%radius
    cv2.circle(img, (x1, y1), 1, (0, 255, 0), -1) #draws centre of circle
    #font = cv2.FONT_HERSHEY_SIMPLEX
    font = cv2.FONT_HERSHEY_PLAIN 
    cv2.putText(img, str(num), (x1 + radius, y1 + radius), font, 1, (200, 255, 155), 1, cv2.LINE_AA)
    img2 = img.copy()

标签: pythonopencv

解决方案


推荐阅读