利用Python统计图片所框选区域的RGB值亮度值以及信噪比

首先当然就是选择你需要分析的图片了,这边也是主要利用Tkinter模块:

def select_circle_area():
    # Open a file dialog to select the picture file
    Tk().withdraw()  # Hide the Tkinter root window
    file_path = askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.bmp")])

    # Read the image file
    img = cv2.imread(file_path)

    # Create a window to display the image
    cv2.namedWindow("Select Area")
    cv2.moveWindow("Select Area", 10, 10)

再定义鼠标响应函数,主要就用CV2模块

def mouse_callback(event, x, y, flags, param):
        nonlocal drawing, circle_center, radius
        #  左键击下
        if event == cv2.EVENT_LBUTTONDOWN:
            drawing = True
            circle_center = (x, y)
        #  左键弹起
        elif event == cv2.EVENT_LBUTTONUP:
            drawing = False
            radius = int(np.sqrt((x - circle_center[0]) ** 2 + (y - circle_center[1]) ** 2))

    # Set the callback function for mouse events
    cv2.setMouseCallback("Select Area", mouse_callback)

    while True:
        # Display the image with the selected circle area
        img_copy = img.copy()

        if drawing:
            cv2.circle(img_copy, circle_center, radius, (0, 255, 0), 1)

 接着就是对所选区域的RGB,亮度以及SNR的计算以及显示了,这边我是直接显示在图片上,当然也可以print出来:

selected_area = img_copy[circle_center[1]-radius:circle_center[1]+radius, circle_center[0]-radius:circle_center[0]+radius]

        # Calculate the RGB value, brightness value, and signal-to-noise ratio (SNR) of the selected area
        rgb_value = np.mean(selected_area, axis=(0, 1))
        brightness_value = np.mean(selected_area)
        snr = np.mean(selected_area) / np.std(selected_area)

        # Display the output values on the image
        # newline='\n'
        # text = f"RGB: {rgb_value} {newline} Brightness: {brightness_value:.2f} {newline} SNR: {snr:.2f}"
        text = f"RGB: {rgb_value}  Brightness: {brightness_value:.2f} SNR: {snr:.2f}"
        cv2.putText(img_copy, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)

        result_img = img.copy()
        cv2.circle(result_img, circle_center, radius, (0, 255, 0), 2)
       
        cv2.imshow("Select Area", img_copy)

完整代码如下:

import cv2
import numpy as np
from tkinter import Tk
from tkinter.filedialog import askopenfilename

def select_circle_area():
    # Open a file dialog to select the picture file
    Tk().withdraw()  # Hide the Tkinter root window
    file_path = askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.bmp")])

    # Read the image file
    img = cv2.imread(file_path)

    # Create a window to display the image
    cv2.namedWindow("Select Area")
    cv2.moveWindow("Select Area", 10, 10)

    # Initialize variables
    drawing = False
    circle_center = (0, 0)
    radius = 0

    def mouse_callback(event, x, y, flags, param):
        nonlocal drawing, circle_center, radius
        #  左键击下
        if event == cv2.EVENT_LBUTTONDOWN:
            drawing = True
            circle_center = (x, y)
        #  左键弹起
        elif event == cv2.EVENT_LBUTTONUP:
            drawing = False
            radius = int(np.sqrt((x - circle_center[0]) ** 2 + (y - circle_center[1]) ** 2))

    # Set the callback function for mouse events
    cv2.setMouseCallback("Select Area", mouse_callback)

    while True:
        # Display the image with the selected circle area
        img_copy = img.copy()

        if drawing:
            cv2.circle(img_copy, circle_center, radius, (0, 255, 0), 1)
        #### cv2.imshow("Select Area", img_copy)

        #  # Wait for key press
        # key = cv2.waitKey(1) & 0xFF

        # # Exit loop if 'q' is pressed
        # if key == ord("q"):
        #     break

        # # Clear the image copy to remove previous drawing
        # img_copy = img.copy()
        # Calculate the selected circle area
        selected_area = img_copy[circle_center[1]-radius:circle_center[1]+radius, circle_center[0]-radius:circle_center[0]+radius]

        # Calculate the RGB value, brightness value, and signal-to-noise ratio (SNR) of the selected area
        rgb_value = np.mean(selected_area, axis=(0, 1))
        brightness_value = np.mean(selected_area)
        snr = np.mean(selected_area) / np.std(selected_area)

        # Display the output values on the image
        # newline='\n'
        # text = f"RGB: {rgb_value} {newline} Brightness: {brightness_value:.2f} {newline} SNR: {snr:.2f}"
        text = f"RGB: {rgb_value}  Brightness: {brightness_value:.2f} SNR: {snr:.2f}"
        cv2.putText(img_copy, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1)

        result_img = img.copy()
        cv2.circle(result_img, circle_center, radius, (0, 255, 0), 2)
       
        cv2.imshow("Select Area", img_copy)

        # Wait for key press
        key = cv2.waitKey(1) & 0xFF

        # Exit loop if 'q' is pressed
        if key == ord("q"):
            break
        #### img_copy = img.copy()

    # Cleanup and close the window
    cv2.destroyAllWindows()

# Call the function to start selecting the circle area
select_circle_area()

虽然最终显示很鸡肋,但该有的还是有啦,对应的RGB值其实应该时BGR值。

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值