在opencvpython中将float32类型转换为uint8类型后,如何恢复图像颜色?

2024-04-20 15:36:23 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试在图像中检测aruco标记,函数要求图像为uint8类型,因此我使用以下公式转换图像:

IMG = ((img.astype(np.uint8)*255)) #img is the original float32 type image

以下是float32类型的图像: Image

把它转换成uint8类型,然后把它传递给一个函数,这个函数需要一个uint8类型的映像来处理,然后我得到了:Image uint8

我将图像传递给的函数是:

def detect_Aruco(img):  #returns the detected aruco list dictionary with id: corners
    aruco_list = {}
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_5X5_50)
    parameters = aruco.DetectorParameters_create()
    #lists of ids and the corners belonging to each id
    corners, ids, _ = aruco.detectMarkers(gray, aruco_dict, parameters = parameters)
    # print len(corners), corners, ids
    gray = aruco.drawDetectedMarkers(gray, corners,ids)
    # cv2.imshow('frame',gray)
    #print (type(corners[0]))
    if len(corners):    #returns no of arucos
        #print (len(corners))
        #print (len(ids))
        for k in range(len(corners)):
            temp_1 = corners[k]
            temp_1 = temp_1[0]
            temp_2 = ids[k]
            temp_2 = temp_2[0]
            aruco_list[temp_2] = temp_1
        return aruco_list

我想得到图像2,但它有原来的颜色。有什么帮助吗?你知道吗

编辑:这是我的原始图像数组(float32):Image as float array

编辑2:我想我明白了。 为了增加对比度,我把它乘以3。数值超过1。所以正常化是错误的。你知道吗

同样正如注释所建议的,正确的行是:ENHAN_IMG = ((enhan_img*255).astype(np.uint8))


Tags: the函数图像ids类型imglentemp