comparison trafficintelligence/cvutils.py @ 1168:d71a4d174b1a

corrected potential bug with dtype in image to world projection
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 06 Jul 2021 00:28:24 -0400
parents 7eb972942f22
children 2f89dc3d99e5
comparison
equal deleted inserted replaced
1167:f67b4f892195 1168:d71a4d174b1a
5 from os import listdir 5 from os import listdir
6 from subprocess import run 6 from subprocess import run
7 from math import floor, log10, ceil 7 from math import floor, log10, ceil
8 from time import time 8 from time import time
9 9
10 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil, nonzero 10 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil, nonzero, dtype
11 from numpy.linalg import inv 11 from numpy.linalg import inv
12 from matplotlib.pyplot import imread, imsave, imshow, figure, subplot 12 from matplotlib.pyplot import imread, imsave, imshow, figure, subplot
13 13
14 try: 14 try:
15 import cv2 15 import cv2
69 points = loadtxt(filename, delimiter=',') 69 points = loadtxt(filename, delimiter=',')
70 savetxt(utils.removeExtension(filename)+'-point-correspondences.txt',append(points[:,:2].T, points[:,3:].T, axis=0)) 70 savetxt(utils.removeExtension(filename)+'-point-correspondences.txt',append(points[:,:2].T, points[:,3:].T, axis=0))
71 71
72 def loadPointCorrespondences(filename): 72 def loadPointCorrespondences(filename):
73 '''Loads and returns the corresponding points in world (first 2 lines) and image spaces (last 2 lines)''' 73 '''Loads and returns the corresponding points in world (first 2 lines) and image spaces (last 2 lines)'''
74 points = loadtxt(filename, dtype=float32) 74 points = loadtxt(filename, dtype=dtype('float32'))
75 return (points[:2,:].T, points[2:,:].T) # (world points, image points) 75 return (points[:2,:].T, points[2:,:].T) # (world points, image points)
76 76
77 def cvMatToArray(cvmat): 77 def cvMatToArray(cvmat):
78 '''Converts an OpenCV CvMat to numpy array.''' 78 '''Converts an OpenCV CvMat to numpy array.'''
79 print('Deprecated, use new interface') 79 print('Deprecated, use new interface')
557 '''Projects points (2xN array) from image (video) space to world space 557 '''Projects points (2xN array) from image (video) space to world space
558 1. through undistorting if provided by intrinsic camera matrix and distortion coefficients 558 1. through undistorting if provided by intrinsic camera matrix and distortion coefficients
559 2. through homograph projection (from ideal point (no camera) to world)''' 559 2. through homograph projection (from ideal point (no camera) to world)'''
560 if points.shape[0] != 2: 560 if points.shape[0] != 2:
561 raise Exception('points of dimension {}'.format(points.shape)) 561 raise Exception('points of dimension {}'.format(points.shape))
562 562 if points.dtype != dtype('float64'):
563 points = array(points, dtype = dtype('float64'))
564
563 if intrinsicCameraMatrix is not None and distortionCoefficients is not None: 565 if intrinsicCameraMatrix is not None and distortionCoefficients is not None:
564 undistortedPoints = cv2.undistortPoints(points.T.reshape(1,points.shape[1], 2), intrinsicCameraMatrix, distortionCoefficients).reshape(-1,2) 566 undistortedPoints = cv2.undistortPoints(points.T.reshape(1,points.shape[1], 2), intrinsicCameraMatrix, distortionCoefficients).reshape(-1,2)
565 return homographyProject(undistortedPoints.T, homography) 567 return homographyProject(undistortedPoints.T, homography)
566 else: 568 else:
567 return homographyProject(points, homography) 569 return homographyProject(points, homography)
641 img = imread(imageDirectory+filename) 643 img = imread(imageDirectory+filename)
642 features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, blockNorm, visualize, transformSqrt) 644 features = HOG(img, rescaleSize, orientations, pixelsPerCell, cellsPerBlock, blockNorm, visualize, transformSqrt)
643 inputData.append(features) 645 inputData.append(features)
644 646
645 nImages = len(inputData) 647 nImages = len(inputData)
646 return array(inputData, dtype = float32), array([classLabel]*nImages) 648 return array(inputData, dtype = dtype('float32')), array([classLabel]*nImages)
647 649
648 650
649 ######################### 651 #########################
650 # running tests 652 # running tests
651 ######################### 653 #########################