python opencv 找出图片中最亮和最暗的的行、列、区域
最亮的列
import cv2
import numpy as np
#得到图片路径image_path的宽wide、高heigh和深度deep
def wide_heigh(image_path):
img = cv2.imread(image_path)
(heigh,wide,deep) = img.shape
print('图片的高是:',heigh)
print('图片的宽是:',wide)
print('图片的深是:', deep)
return heigh,wide,deep
#最亮的列
def brightestColumn(img_path):
# 图片的高、宽、深
heigh, wide, deep = wide_heigh(img_path)
# 读取图片
img = cv2.imread(img_path)
#列亮度列表
bright_list = []
# 遍历宽
for i in range(wide):
# 提取每一列
Column_i = img[:,i:i+1]
# 列的亮度求和
bright_sum = Column_i.sum()
# 列亮度加到列亮度列表中
bright_list.append(bright_sum)
#找出列表bright_list中最大索引(就是图片的最亮的列数)
brightest_Column = bright_list.index(max(bright_list))
# 找出列表bright_list中最小索引(就是图片的最暗的列数)
# dark_Column = bright_list.index(min(bright_list))
# 最亮列起点的坐标
ptStart = (brightest_Column, 0)
# 最亮列终点的坐标
ptEnd = (brightest_Column, heigh)
# 最亮列的颜色
point_color = (0, 255, 0) # BGR
# 最亮列的线条宽度
thickness = 1
# lineType:最亮线的类型
# - 8( or omitted) : 8 - connectedline
# - 4:4 - connectedline
# - CV_AA - antialiased line
lineType = 4
# 画出最亮列
cv2.line(img, ptStart, ptEnd, point_color, thickness, lineType)
cv2.imshow('img', img)
cv2.waitKey(0)
return brightest_Column
if __name__ == '__main__':
img_path = '1.jpg'
brightestColumn(img_path)
效果如下:
最亮的行
import cv2
import numpy as np
#得到图片路径image_path的宽wide、高heigh和深度deep
def wide_heigh(image_path):
img = cv2.imread(image_path)
(heigh,wide,deep) = img.shape
print('图片的高是:',heigh)
print('图片的宽是:',wide)
print('图片的深是:', deep)
return heigh,wide,deep
#最亮的行
def brightestColumn(img_path):
# 图片的高、宽、深
heigh, wide, deep = wide_heigh(img_path)
# 读取图片
img = cv2.imread(img_path)
#行亮度行表
bright_list = []
# 遍历宽
for i in range(heigh):
# 提取每一行
Column_i = img[i:i+1]
# 行的亮度求和
bright_sum = Column_i.sum()
# 行亮度加到行亮度行表中
bright_list.append(bright_sum)
#找出行表bright_list中最大索引(就是图片的最亮的行数)
brightest_Column = bright_list.index(max(bright_list))
# 找出行表bright_list中最小索引(就是图片的最暗的行数)
# dark_Column = bright_list.index(min(bright_list))
# 最亮行起点的坐标
ptStart = (0,brightest_Column)
# 最亮行终点的坐标
ptEnd = (wide,brightest_Column)
# 最亮行的颜色
point_color = (0, 255, 0) # BGR
# 最亮行的线条宽度
thickness = 1
# lineType:最亮线的类型
# - 8( or omitted) : 8 - connectedline
# - 4:4 - connectedline
# - CV_AA - antialiased line
lineType = 4
# 画出最亮行
cv2.line(img, ptStart, ptEnd, point_color, thickness, lineType)
cv2.imshow('img', img)
cv2.waitKey(0)
return brightest_Column
if __name__ == '__main__':
img_path = '1.jpg'
brightestColumn(img_path)
效果如下:
最亮点和最亮区域
# coding=utf-8
# 导入python包
import numpy as np
import cv2
# 读取图片并将其转化为灰度图片
image = cv2.imread('1.jpg')
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 利用cv2.minMaxLoc寻找到图像中最亮和最暗的点
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
# 在图像中绘制结果
cv2.circle(image, maxLoc, 5, (255, 0, 0), 2)
# 应用高斯模糊进行预处理(由找点变成找区域)
gray = cv2.GaussianBlur(gray, (59,59), 0)
# 利用cv2.minMaxLoc寻找到图像中最亮和最暗的区域
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image1 = orig.copy()
cv2.circle(image1, maxLoc, 59, (255, 0, 0), 2)
# 显示结果
result = np.hstack([orig, image, image1])
cv2.imwrite("2.jpg", result)
cv2.imshow("Robust", result)
cv2.waitKey(0)
效果如下: