Since opencv reads in the image data type is uint8 type, direct addition and subtraction will lead to data overflow phenomenon
(1) Operate with Numpy
can be calculated by first converting the image data type to an int type, the
data=(image,dtype='int')
After processing (e.g., traversing, setting those greater than 255 to 255 and those less than 0 to 0)
Then reduce the image to uint8 type
data=(image,dtype='uint8')
Attention:
(1) If they are added directly, then
When the pixel value is > 255, the result is the result of modulo 256, e.g. (240+66) % 256 = 50
Instead of being automatically processed under 255
(2) If one subtracts directly, then
When the pixel value <0, the result is the result of adding 256, e.g. (100-140) + 256 = 216
Instead of being automatically treated as 0
Example:
Select an image R component for the experiment
Case 1: Direct numpy operation
First add to 240, then 66, over 255, you can see that instead of defaulting to 255, it becomes 50
Try the subtraction operation again: subtract another 100, which would have resulted in -50, but as you can see, it becomes 206 (-50+256)
(2) operate with opencv's own function
The images are summed:
()
Pixel value >255, direct automatic treatment according to 255
The images are subtracted:
()
Pixel values less than 0 are automatically treated as 0.
Example:
r plus 300 automatically becomes 255
Similarly, anything less than 0 automatically becomes 0
Both of these methods can be chosen as needed.
Additional knowledge:Storing images with uint8 type in Opencv numpy
When processing images with opencv, you can find that the matrix types obtained are all uint8
import cv2 as cv img=() print(img) array([[[...], [...], [...]]],dtype='uint8')
uint8 is designed to store various images (including RGB, grayscale images, etc.) in the range from 0-255.
Note here how the conversion to uint8 is done
1: numpy has np.uint8() function, but this function is only on the original data and 0xff with (and the lowest 2 bytes of data with), this will easily lead to if the original data is greater than 255, then in the direct use of np.uint8() after the data than the eighth bit of the larger than the data are truncated, for example.
>>>a=[2000,100,2] >>>np.uint8(a) array([208, 100, 2], dtype=uint8)
2: with the function with cv2.NORM_MINMAX, you can set the maximum and minimum value of the target array, and then let the original array of proportional zoom in or zoom out to the target array, such as the following example is the img of all the numbers of proportional zoom in or zoom out to the 0-255 range of the array.
(img, out, 0, 255, cv2.NORM_MINMAX)
Then change the data type
([out],dtype=‘uint8')
Summary:
To use the current array as an image type to perform various operations, it is necessary to convert to the uint8 type, the conversion is recommended to use the second way, because the first in the value is greater than 255 after the easy to lose.
The above this talk about python opencv to image color channel to add and subtract operations overflow is all I share with you, I hope to give you a reference, and I hope you support me more.