본문 바로가기
Libraries & Packages/OpenCV

[OpenCV] 이미지 데이터 타입 확인 및 변경

by yongee97 2023. 7. 22.

* 주요 데이터 타입 종류

 

uint8 : cv2.imread() 함수를 통해 읽은 경우

float32 : pytorch tensor를 numpy matrix로 바꾼 경우

 

* 데이터 타입 확인(Python)

print(image.dtype)

 

* 데이터 타입 변경 방법

- float32를 uint8로

J = I*255
J = J.astype(np.uint8)

 

- uint8을 float32로

image = image.astype(np.float32)
image /= 255.

 

 

 

* Reference

https://stackoverflow.com/questions/53235638/how-should-i-convert-a-float32-image-to-an-uint8-image

 

How should I convert a float32 image to an uint8 image?

I want to convert a float32 image into uint8 image in Python using the openCV library. I used the following code, but I do not know whether it is correct or not. Here I is the float32 image. J = ...

stackoverflow.com

https://stackoverflow.com/questions/57325720/opencv-convert-uint8-image-to-float32-normalized-image

 

OpenCV - convert uint8 image to float32 normalized image

I'm trying to convert parts of a Keras DarkNet code to try to make the code run faster. Here is the code I'm trying to optimize: model_image_size = (416, 416) import cv2 from PIL import Image fr...

stackoverflow.com