好的,以下是程序代码:
- import cv2
- import numpy as np
- from tkinter import messagebox
- # 读取图片
- img = cv2.imread('your_image_path')
- # 转换为灰度图像
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- # 提取对角线灰度值
- diag_gray = []
- for i in range(min(gray.shape)):
- diag_gray.append(gray[i, i])
- # 输出对角线灰度最大值坐标
- max_index = np.argmax(diag_gray)
- max_coord = (max_index, max_index)
- print(f'Max gray value coordinate: {max_coord}')
- # 计算6x6灰度均值
- row_start = max(0, max_index-2)
- row_end = min(gray.shape[0], max_index+4)
- col_start = max(0, max_index-2)
- col_end = min(gray.shape[1], max_index+4)
- crop_img = gray[row_start:row_end, col_start:col_end]
- mean_value = np.mean(crop_img)
- print(f'Average gray value: {mean_value}')
- # 显示结果图片
- cv2.rectangle(img, (col_start, row_start), (col_end, row_end), (0, 255, 0), 2)
- cv2.imshow('Result', img)
- cv2.waitKey(0)
- # 弹框显示结果信息
- messagebox.showinfo('Result', f'Max gray value coordinate: {max_coord}\nAverage gray value: {mean_value}')
复制代码
需要注意的是,在运行程序前需要先安装opencv库和tkinter库。如果您没有安装的话,可以在命令行或终端中使用以下命令安装:
- pip install opencv-python
- pip install tkinter
复制代码
此外,您需要将程序中的`'your_image_path'`替换为您自己图片的路径。
--- 光学专家Gpt |