Home > Enterprise >  I want to know what does np.float32 means
I want to know what does np.float32 means

Time:02-25

What does np.float32 do in this code , This code is to change the perspective of a given image can anyonne plls explain as to what does np.float32([[320,15], [700,215], [85,610], [530,780]]) do?

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('images/scan.jpg')

cv2.imshow('Original', image)
cv2.waitKey(0)

# Cordinates of the 4 corners of the original image
points_A = np.float32([[320,15], [700,215], [85,610], [530,780]])

# Cordinates of the 4 corners of the desired output
# We use a ratio of an A4 Paper 1 : 1.41
points_B = np.float32([[0,0], [420,0], [0,594], [420,594]])
 
# Use the two sets of four points to compute 
# the Perspective Transformation matrix, M    
M = cv2.getPerspectiveTransform(points_A, points_B)
 
warped = cv2.warpPerspective(image, M, (420,594))
 
cv2.imshow('warpPerspective', warped)
cv2.waitKey(0)
cv2.destroyAllWindows()

CodePudding user response:

It's generating a 2D-list of float32 (a 'float' type with '32' bits).


The formatting is a bit hard to understand at first but basically its creating one list with '[]', and inside that list its creating new lists with two variables. So each item in the first list is a second list, with two items in it:

points_B = [  [item1, item2], [item3, item4]  ]

to access the second item we could:

x = points_B[0][1]

The float32s in the list are referring to points which are then being passed into getPerspectiveTransform which is being used to compute the transformation matrix which to my understanding just defines the area of the image that you want to warp.

CodePudding user response:

np.float32(a) is equivalent to np.array(a, dtype=np.float32). it's a data type of some size.

a = [1, 2, 3]                      # a(list,len=3): [1, 2, 3]
b = np.float32(a)                  # b(numpy.ndarray,s=(3,),dtype=float32): [1.00, 2.00, 3.00]
c = np.array(a, dtype=np.float32)  # c(numpy.ndarray,s=(3,),dtype=float32): [1.00, 2.00, 3.00]

a = [100, 200, 300]             # a = [100, 200, 300]
b = np.uint8(a)                 # b(numpy.ndarray,s=(3,),dtype=uint8): [100, 200, 44]
c = np.array(a, dtype=np.uint8) # c = np.array(a, dtype=np.uint8)
d = np.int32(a)                 # d(numpy.ndarray,s=(3,),dtype=int32): [100, 200, 300]

Notice on the second example that an unsigned int8 is not big enough for the number 300 - hence overflow (max val for uint8 is 255).

  • Related