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

菜鸡不清楚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()

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

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

你可能感兴趣的:(OpenCV学习:找图片中的最亮区域)