【问题标题】:How to compute minimum and maximum data value from a certain height and width of an image using python?如何使用python从图像的特定高度和宽度计算最小和最大数据值?
【发布时间】:2023-01-27 01:22:18
【问题描述】:
我一直在尝试为一组特定的高度和宽度获取某些数据集的最小和最大数据值。假设我确实有 (256,256) 的 numpy 图像。我想得到如下红框的最小和最大数据值:
enter image description here
到目前为止,我尝试了以下步骤:
使用 openCV 获取 numpy 数组后,我做了:
r,c = img.shape[0:2] #get the row and coloumns of the image
for i in range (row): #iterate over all the rows and we are considering all the rows
for j in range (col)[-50:] #trying to consider only the last 50th cols
.......
我被困在这一点上,比如如何从特定的红框像素中获取最小和最大数据值。
【问题讨论】:
标签:
python
arrays
numpy
opencv
minmax
【解决方案1】:
假设我们有一个由 ([x1, y1], [x2, y2]) 给出的边界框。也就是一个矩形的最小坐标和最大坐标(就像你在图中画的那个)。由于图像是数组,如果我们有 np.Array 格式的图像,我们可以索引行和列,然后计算裁剪的最大值和最小值。
import cv2
import numpy as np
image = cv2.cvtColor(cv2.imread("image/path"), cv2.COLOR_BGR2RGB)
bbox = ([x1, y1], [x2, y2])
crop = image[bbox[0][0]:bbox[1]:[0], bbox[0][1]:bbox[1]:[1], :]]
print(np.max(crop), np.min(crop))