Home > Software design >  How to compute minimum and maximum data value from a certain height and width of an image using pyth
How to compute minimum and maximum data value from a certain height and width of an image using pyth

Time:01-27

I have been trying to get the minimum and maximum data value of some datasets for a particular set of height and width. Suppose I do have a numpy image of (256,256). I want to get the minimum and maximum data value of the following red box:

1

I tried the following steps so far:

After getting the numpy array with openCV, I did:

r,c = img.shape[0:2] #get the row and columns 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
         .......

I am stuck at this point, like how to get the minimum and maximum data values from the particular red box pixels.

CodePudding user response:

Suppose we have a bounding box given by ([x1, y1], [x2, y2]). That is, the minimum and maximum coordinates of a rectangle (like the one you paint in the figure). As the images are arrays, if we have the image in np.Array format, we can index the rows and columns and then calculate the maximum and minimum value of the crop.


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))

CodePudding user response:

You can do the following:

bb = img[:,-50:, :] # gets the red bouding box
max_value = bb.max()
min_value = bb.min()
  • Related