首先当然就是选择你需要分析的图片了,这边也是主要利用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值。
运行结果: