Issue
I am trying a script that works with images in python. The first image was working and the second one did not work at all.
The script was
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
image = mpimg.imread('test.jpg')
# Grab the x and y size and make a copy of the image
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
# Define color selection criteria
###### MODIFY THESE VARIABLES TO MAKE YOUR COLOR SELECTION
red_threshold = 200
green_threshold = 200
blue_threshold = 200
######
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
# Do a boolean or with the "|" character to identify
# pixels below the thresholds
thresholds = (image[:,:,0] < rgb_threshold[0]) \
| (image[:,:,1] < rgb_threshold[1]) \
| (image[:,:,2] < rgb_threshold[2])
color_select[thresholds] = [0,0,0]
plt.imshow(color_select)
Then I discovered the reason why the second image did not work
print("Red channel - Min:", np.min(image[:,:,0]), "Max:", np.max(image[:,:,0]))
print("Green channel - Min:", np.min(image[:,:,1]), "Max:", np.max(image[:,:,1]))
print("Blue channel - Min:", np.min(image[:,:,2]), "Max:", np.max(image[:,:,2]))
For the first image
Red channel - Min: 0 Max: 255
Green channel - Min: 10 Max: 255
Blue channel - Min: 0 Max: 255
For the second image that did not work
Red channel - Min: 0.0 Max: 1.0
Green channel - Min: 0.0 Max: 1.0
Blue channel - Min: 0.0 Max: 1.0
Can someone explain to me the basics of why some images use 0-255 and other 0.0 to 1.0?
Solution
I'm sure there are many factors, and discussions of historic practices and influences are not well suited to StackOverflow, but here are my thoughts. I'm sure I have omitted plenty.
For many years camera sensors were 8-bit, producing images in range 0..255 for each sample. Then manufacturers started producing 10-bit sensors, with higher dynamic range 0..1023. Then 12-bit, then 14-bit so the range has increased.
Standard JPEG is only 8-bit capable. PNG is only capable of 8/16-bit at the higher end and still only integer. TIFF and EXR are capable of 32/64-bit at the higher end, but crucially also floating point.
Many early image processing algorithms were/are coded to expect 8-bit samples, e.g. pixel = 255 - pixel
for image inversion. Of course, that breaks when your data are in the range 0..65535.
Then machine learning came along and it's more sensible to use a range of 0..1 and floats for everything, so inversion is pixel = 1 - pixel
works if the data are 16-bit floats, 32-bit floats or 64-bit floats.
Answered By - Mark Setchell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.