Python: the float type and uint8 type of the picture

tags: Road to Cultivation: Python

1 Float type and uint8 type

In the process of python image processing, the value of the RGB image encountered is between 0-255. In order to process the image better, the image value is usually converted to between 0-1
This process is the process of converting the uint8 type of the image to the float type.

img.astype('uint8')/255
  • Value range of float type: -1 to 1 or 0 to 1
  • Uint8 is an unsigned eight-bit integer, which means an integer in the range [0, 255]

The range of common types:

 

Python recommends the following two types of images:

  • PIL (pip install pillow), this is relatively primitive, and it is always uint8 during processing
from PIL import Image
import numpy as np

 im = Image.open('test.jpg') # From reading is uint8
 npim = np.array(im) # Convert to numpy array processing
  • cv2 (pip install opencv-python), the python implementation of opencv
import cv2

im = cv2.imread('test.jpg') # Read the default numpy array in uint8 format

In general, uint8 can be used directly, if there is a need (such as neural network, etc.), it can be converted into floating-point numbers. If you need to convert back to the PIL image object, it must be in uint8 format. If you always use cv2, you can also directly save the floating-point number form (note that it is 0~255, not 0~1).

reference:

 

 

 

Intelligent Recommendation

Note that the image type of python-opencv must be dtype="uint8"

When using numpy to create a matrix (numpy.ndarray) and want to use it to generate a picture, you must remember that the type of each element in the matrix must be dtype=np.uint8 or you can write dtyp...

Conversion between float type and float64 type in Python

1. ‘float’ to ‘float64’ x x xIt was originally a "float" type After the above x x xIt becomes the "float64" type 2. From "float64" to "float&...

Python INT8 INT16 INT32 INT64 FLOAT UINT8

Python INT8 INT16 INT32 INT64 FLOAT UINT8 INT8, accounting for 1 byte. INT16, accounting for 2 bytes. int32, 4 bytes. int64, 8 bytes. Float type value range: -1 to 1 or 0 to 1 uint8 Type Value range: ...

Golang value type is converted to [] uint8 type

In go language, byte is actually an alias of uint8, and byte and uint8 can be directly converted between each other. Currently, only int in the range of 0 to 255 can be converted to byte. When using b...

python __builtins__ float type (25)

25, 'float', for converting the integer to floating point and string.   ...

More Recommendation

Convert Float type into date in Python

When reading a date from an Excel, it is found to be a float type, but it is a date when it is displayed. This will be handled in the code, and the conversion is conversion: This is the string after t...

python basic data type Float

Recently, I want to use Python better, so I decided to sort out the basic knowledge. The code comes from the source code and analyzed the official documentation. Reference document:https://docs.python...

Python converts UINT8 type array elements to float32 type, then do division implementation

Just as a record, please skip. use.astype The Train_Images array is an integer of the UINT8 type (if the obtained value will be taken from the integer), the blogger wants to turn it into float, and th...

Matlab: uint8, double, data type processing

ImportWhen the image is used, the matlab read image data type is saved as uint8 (8-bit unsigned integer); In operationThe value in matlab generally uses double type (64 bit); Display imageWhen (imshow...

Matlab image data type uint8, double relationship

Digital image processing Matlab image data type uint8, double relationship (1) The double-type image matrix is ​​converted to the uint8(I) type. (2) Display of [0, 1] and [0, 255] of the image Matlab ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top