1

I have this program that is supposed to select one color channel from an image, and square each element elementwise. However, it is not returning any results greater than the values in the first array? As if it is bounded? I have tried squaring it in numerous ways and it works fine for non-imported image datasets. Below is the code run on a Mac and the results:

import numpy as np
import cv2
im = cv2.imread("blue")
im22=im
im22[im22<100] = 0
blue=np.array(im22[:,:,2])
blue2=np.square(blue)
print("type is ",type(blue))
print("blue max", np.max(blue))
print("blue min",np.min(blue))
print("blue Squared max", np.max(blue2))
print("blue Squared min",np.min(blue2))

Results are:

blue max 255

blue min 0

blue Squared max 249

blue Squared min 0

Christoph Rackwitz
16.4k5 gold badges42 silver badges56 bronze badges
asked Jun 9, 2025 at 10:49
1
  • 3
    Your array is probably uint8, which has a maximum representable value of 255. You need to convert the array to another dtype with a larger maximum value to be able to represent the squared values - uint16 would work for this. Commented Jun 9, 2025 at 11:11

1 Answer 1

2

According to Nin17 comment, I added the missing line :

import numpy as np
import cv2
im = cv2.imread("blue")
im22=im
im22[im22<100] = 0
blue=np.array(im22[:,:,2])
blue = blue.astype(np.uint16)
blue2=np.square(blue)
print("type is ",type(blue))
print("blue max", np.max(blue))
print("blue min",np.min(blue))
print("blue Squared max", np.max(blue2))
print("blue Squared min",np.min(blue2))

It worked on my setup

▶ more pyproject.toml
[project]
name = "python"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
 "numpy>=2.3.0",
 "opencv-python>=4.11.0.86",
]
answered Jun 9, 2025 at 12:11
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.