Finding the Value of the Min and Max Pixel

How to find min and max pixel value of an index?

Try using .reduceRegion() and specifying the region of interest:

var NDVI = ee.Image('assetID') // specify the image of interest
var roi = ee.Geometry.Polygon([[-98,44], [-96,44], [-96,46], [-98,46], [-98,44]]) // specify your region of interest

var minMaxValues = NDVI.reduceRegion({reducer: ee.Reducer.minMax(),
geometry: roi
})

print('minMaxValues', minMaxValues);

How to find max pixel value in a bounding box on cv::Mat

Given a cv::Mat single channel image m and a cv::Rect box b, you can get the min and max values using cv::minMaxIdx() like this:

double minValue, maxValue;

cv::minMaxIdx(m(b), &minValue, &maxValue);

Numpy 3D array max and min value

First of, to compute things like a mean you probably want to use floating point numbers instead of integers to start width. So in the following I assume you use those instead.

By using python-loops you're giving away all the advantages of numpy, because they are inherently slow, at least compared to the underlying compiled code that is executed when calling numpy functions. If you want your code to be reasonably fast you should make use of vectorization. Consider following code that does what you ask for but without any loops in python:

# compute minima, maxima and sum
mins = np.min(images, axis=0)
maxs = np.max(images, axis=0)
sums = np.sum(images, axis=0)
# compute the mean without the extremes
mean_without_extremes = (sums - mins - maxs) / (xlen - 2)
# replace maxima with the mean
images[images == mins] = mean_without_extremes.reshape(-1)
images[images == maxs] = mean_without_extremes.reshape(-1)

As you're probably not familiar with that, I recommend reading the introdcutions in the documentation about indexing and broadcasting in order to use numpy efficiently:

  • https://numpy.org/doc/stable/user/basics.indexing.html#basics-indexing
  • https://numpy.org/doc/stable/user/theory.broadcasting.html#array-broadcasting-in-numpy

EDIT: As pointed out in the comments, the solution above only works for xlen > 2 and if the extrema are only attained once per pixel location. this could be fixed by replacing these lines with

images = np.where(images == mins, images, mean_without_extremes)
images[np.isnan(images)] = 0 # set "empty mean" to zero
# using "np.where" as suggested by OP
# we can actually reduce that to one "np.where" call which might be slightly faster
images = np.where(np.logical_or(images == mins, images == maxs), images, mean_without_extremes)

How do i find the min\max value Numpy array

If I understand the question correctly, the goal is to find indices of rows and columns bounding the area of the image that contains all non-white pixels. This can be done, for example, as follows.

Load a sample image:

import numpy as np
import matplotlib.pyplot as plt

img = plt.imread("sample.jpg").copy()
plt.imshow(img)

sample image

Find the bounding box of non-white pixels and display the result:

# indices of non-white pixels 
rows, cols = np.nonzero(np.any(img != 255, axis=-1))
# indices of rows and columns of the bounding box
rbox = rows.min(), rows.max()
cbox = cols.min(), cols.max()

# show the selection
img[rbox[0]:rbox[1]+1, cbox[0]:cbox[1]+1, 1] = 0
plt.imshow(img)

image with the bounding box

Determine maximum and minimum pixel value to draw bounding box

  1. Redraw the object you want to box in unique for that semantic map color. For instance I've used green:

semantic map


  1. Run following script:

.

from PIL import Image, ImageDraw

IMAGE = 't5XM4.png'
IMAGE_MAP = 'Gz8b7.png'
IMAGE_OUTPUT = 'Result.png'
GREEN = (0, 255, 0)
OFFSET = 10

image_map = Image.open(IMAGE_MAP)
image = Image.open(IMAGE)
pixels = image_map.load()
size_sm = image_map.size
size = image.size
ratio = (size_sm[0]/size[0], size_sm[1]/size[1])
x_list = []
y_list = []

for x in range(size_sm[0]):
for y in range(size_sm[1]):
if pixels[x, y] == GREEN:
x_list.append(x)
y_list.append(y)

draw = ImageDraw.Draw(image)
draw.rectangle(((min(x_list)/ratio[0]-OFFSET, min(y_list)/ratio[1]-OFFSET),
(max(x_list)/ratio[0]+OFFSET,max(y_list)/ratio[1]+OFFSET)),
width=5, outline=GREEN)
image.save(IMAGE_OUTPUT, 'PNG')

  1. You've got following image at the end:

result image with bounding box

Maximum pixel value in array

IIUC, you want to get the index of the max sum?

image[np.argmax(image.sum(1))]

output: array([220, 200, 180])



Related Topics



Leave a reply



Submit