OpenCV学习:找图片中的最亮区域

本文探讨了使用OpenCV和NumPy库在灰度图像中寻找最大像素值及其坐标的多种方法。通过minMaxLoc()函数与where()函数对比,详细介绍了如何精确找到图像中的最亮点,并计算其重心位置,适用于图像处理与分析领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

菜鸡不清楚minMaxLoc()能不能返回多个最大值,所以在另一篇博客中用了where函数计算。

import numpy as np
import cv2
from time import time
t1 = time()  # 计时开始
src = cv2.imread('D:/opencv_study_by.03.23/test img/OIP.jpg')
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
r = maxLoc
target_dot = np.where(gray == np.max(gray))
row = len(target_dot[0])
x = y = 0
for i in range(row):
    x += target_dot[0][i]
    y += target_dot[1][i]
fx = int(x/row)
fy = int(y/row)
print('({}, {})'.format(fx, fy))
# print(gray.shape)
# print(r)
# print(cv2.minMaxLoc(gray))
# print(np.max(gray), np.where(gray == np.max(gray)))
# print(np.where(gray == np.max(gray))[1])
cv2.namedWindow("input", cv2.WINDOW_AUTOSIZE)
cv2.imshow("input", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

顺便提供一个刚学会的计算重心的算法:

如有不对,欢迎指正。同时,大佬们有啥想法和建议欢迎提出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值