def train_svm(svm_save_path, descriptors, labels):
# train_data = convert_to_ml(descriptors)
train_data = np.array(descriptors)
responses = np.array(labels, dtype=np.int32)
print "Start training..."
svm = cv2.ml.SVM_create()
# Default values to train SVM
svm.setCoef0(0.0)
svm.setDegree(3)
# svm.setTermCriteria(TermCriteria(cv2.TERMCRIT_ITER + cv2.TERMCRIT_EPS, 1000, 1e-3))
svm.setTermCriteria((cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 1000, 1e-3))
svm.setGamma(0)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setNu(0.5)
svm.setP(0.1) # for EPSILON_SVR, epsilon in loss function?
svm.setC(0.01) # From paper, soft classifier
svm.setType(cv2.ml.SVM_EPS_SVR) # C_SVC; # EPSILON_SVR; # may be also NU_SVR; # do regression task
svm.train(train_data, cv2.ml.ROW_SAMPLE, responses)
print "...[done]"
svm.save(svm_save_path)
# def test_classifier(svm_file_path, window_dims):
# # Set the trained svm to my_hog
# hog_detector = get_svm_detector(svm_file_path)
# hog = get_hog_object(window_dims)
# hog.setSVMDetector(hog_detector)
#
# locations = hog.detectMultiScale(img)
python类TERM_CRITERIA_EPS的实例源码
def createTrainingInstances(self, images):
instances = []
img_descriptors = []
master_descriptors = []
cv2.ocl.setUseOpenCL(False)
orb = cv2.ORB_create()
for img, label in images:
print img
img = read_color_image(img)
keypoints = orb.detect(img, None)
keypoints, descriptors = orb.compute(img, keypoints)
if descriptors is None:
descriptors = []
img_descriptors.append(descriptors)
for i in descriptors:
master_descriptors.append(i)
master_descriptors = np.float32(master_descriptors)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, labels, centers = cv2.kmeans(master_descriptors, self.center_num, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.ravel()
count = 0
img_num = 0
for img, label in images:
histogram = np.zeros(self.center_num)
feature_vector = img_descriptors[img_num]
for f in xrange(len(feature_vector)):
index = count + f
histogram.itemset(labels[index], 1 + histogram.item(labels[index]))
count += len(feature_vector)
pairing = Instance(histogram, label)
instances.append(pairing)
self.training_instances = instances
self.centers = centers
def createTrainingInstances(self, images):
instances = []
img_descriptors = []
master_descriptors = []
cv2.ocl.setUseOpenCL(False)
orb = cv2.ORB_create()
for img, label in images:
print img
img = read_color_image(img)
keypoints = orb.detect(img, None)
keypoints, descriptors = orb.compute(img, keypoints)
if descriptors is None:
descriptors = []
img_descriptors.append(descriptors)
for i in descriptors:
master_descriptors.append(i)
master_descriptors = np.float32(master_descriptors)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, labels, centers = cv2.kmeans(master_descriptors, self.center_num, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.ravel()
count = 0
img_num = 0
for img, label in images:
histogram = np.zeros(self.center_num)
feature_vector = img_descriptors[img_num]
for f in xrange(len(feature_vector)):
index = count + f
histogram.itemset(labels[index], 1 + histogram.item(labels[index]))
count += len(feature_vector)
pairing = Instance(histogram, label)
instances.append(pairing)
self.training_instances = instances
self.centers = centers
def local_bow_train(image):
instances = []
img_descriptors = []
master_descriptors = []
cv2.ocl.setUseOpenCL(False)
orb = cv2.ORB_create()
for img, label in images:
print img
img = read_color_image(img)
keypoints = orb.detect(img, None)
keypoints, descriptors = orb.compute(img, keypoints)
if descriptors is None:
descriptors = []
img_descriptors.append(descriptors)
for i in descriptors:
master_descriptors.append(i)
master_descriptors = np.float32(master_descriptors)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret, labels, centers = cv2.kmeans(master_descriptors, self.center_num, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
labels = labels.ravel()
count = 0
img_num = 0
for img, label in images:
histogram = np.zeros(self.center_num)
feature_vector = img_descriptors[img_num]
for f in xrange(len(feature_vector)):
index = count + f
histogram.itemset(labels[index], 1 + histogram.item(labels[index]))
count += len(feature_vector)
pairing = Instance(histogram, label)
instances.append(pairing)
self.training_instances = instances
self.centers = centers
def update(self,frame,events):
img = frame.img
img_shape = img.shape[:-1][::-1] # width,height
succeeding_frame = frame.index-self.prev_frame_idx == 1
same_frame = frame.index == self.prev_frame_idx
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#vars for calcOpticalFlowPyrLK
lk_params = dict( winSize = (90, 90),
maxLevel = 3,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 20, 0.03))
updated_past_gaze = []
#lets update past gaze using optical flow: this is like sticking the gaze points onto the pixels of the img.
if self.past_gaze_positions and succeeding_frame:
past_screen_gaze = np.array([denormalize(ng['norm_pos'] ,img_shape,flip_y=True) for ng in self.past_gaze_positions],dtype=np.float32)
new_pts, status, err = cv2.calcOpticalFlowPyrLK(self.prev_gray,gray_img,past_screen_gaze,None,minEigThreshold=0.005,**lk_params)
for gaze,new_gaze_pt,s,e in zip(self.past_gaze_positions,new_pts,status,err):
if s:
# print "norm,updated",gaze['norm_gaze'], normalize(new_gaze_pt,img_shape[:-1],flip_y=True)
gaze['norm_pos'] = normalize(new_gaze_pt,img_shape,flip_y=True)
updated_past_gaze.append(gaze)
# logger.debug("updated gaze")
else:
# logger.debug("dropping gaze")
# Since we will replace self.past_gaze_positions later,
# not appedning tu updated_past_gaze is like deliting this data point.
pass
else:
# we must be seeking, do not try to do optical flow, or pausing: see below.
pass
if same_frame:
# paused
# re-use last result
events['gaze_positions'][:] = self.past_gaze_positions[:]
else:
# trim gaze that is too old
if events['gaze_positions']:
now = events['gaze_positions'][0]['timestamp']
cutoff = now-self.timeframe
updated_past_gaze = [g for g in updated_past_gaze if g['timestamp']>cutoff]
#inject the scan path gaze points into recent_gaze_positions
events['gaze_positions'][:] = updated_past_gaze + events['gaze_positions']
events['gaze_positions'].sort(key=lambda x: x['timestamp']) #this may be redundant...
#update info for next frame.
self.prev_gray = gray_img
self.prev_frame_idx = frame.index
self.past_gaze_positions = events['gaze_positions']
def downsample_and_detect(self, img):
"""
Downsample the input image to approximately VGA resolution and detect the
calibration target corners in the full-size image.
Combines these apparently orthogonal duties as an optimization. Checkerboard
detection is too expensive on large images, so it's better to do detection on
the smaller display image and scale the corners back up to the correct size.
Returns (scrib, corners, downsampled_corners, board, (x_scale, y_scale)).
"""
# Scale the input image down to ~VGA size
height = img.shape[0]
width = img.shape[1]
scale = math.sqrt( (width*height) / (640.*480.) )
if scale > 1.0:
scrib = cv2.resize(img, (int(width / scale), int(height / scale)))
else:
scrib = img
# Due to rounding, actual horizontal/vertical scaling may differ slightly
x_scale = float(width) / scrib.shape[1]
y_scale = float(height) / scrib.shape[0]
if self.pattern == Patterns.Chessboard:
# Detect checkerboard
(ok, downsampled_corners, board) = self.get_corners(scrib, refine = True)
# Scale corners back to full size image
corners = None
if ok:
if scale > 1.0:
# Refine up-scaled corners in the original full-res image
# TODO Does this really make a difference in practice?
corners_unrefined = downsampled_corners.copy()
corners_unrefined[:, :, 0] *= x_scale
corners_unrefined[:, :, 1] *= y_scale
radius = int(math.ceil(scale))
if len(img.shape) == 3 and img.shape[2] == 3:
mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
mono = img
cv2.cornerSubPix(mono, corners_unrefined, (radius,radius), (-1,-1),
( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1 ))
corners = corners_unrefined
else:
corners = downsampled_corners
else:
# Circle grid detection is fast even on large images
(ok, corners, board) = self.get_corners(img)
# Scale corners to downsampled image for display
downsampled_corners = None
if ok:
if scale > 1.0:
downsampled_corners = corners.copy()
downsampled_corners[:,:,0] /= x_scale
downsampled_corners[:,:,1] /= y_scale
else:
downsampled_corners = corners
return (scrib, corners, downsampled_corners, board, (x_scale, y_scale))
def getP(self, dst):
"""
dst: ??????
return self.MTX,self.DIST,self.RVEC,self.TVEC:
?? ?????????????????
"""
if self.SceneImage is None:
return None
corners = np.float32([dst[1], dst[0], dst[2], dst[3]])
gray = cv2.cvtColor(self.SceneImage, cv2.COLOR_BGR2GRAY)
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (1,0,0), (1,1,0)
objp = np.zeros((2*2,3), np.float32)
objp[:,:2] = np.mgrid[0:2,0:2].T.reshape(-1,2)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
if self.PTimes < self.PCount or self.PCount == 0:
# Arrays to store object points and image points from all the images.
objpoints = self.OBJPoints # 3d point in real world space
imgpoints = self.IMGPoints # 2d points in image plane.
if len(imgpoints) == 0 or np.sum(np.abs(imgpoints[-1] - corners2)) != 0:
objpoints.append(objp)
imgpoints.append(corners2)
# Find mtx, dist, rvecs, tvecs
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
if not ret:
self.PTimes += 1
return None
self.OBJPoints = objpoints
self.IMGPoints = imgpoints
self.MTX = mtx
self.DIST = dist
self.RVEC = rvecs[0]
self.TVEC = tvecs[0]
else:
# Find the rotation and translation vectors.
_, rvec, tvec, _= cv2.solvePnPRansac(objp, corners2, self.MTX, self.DIST)
self.RVEC = rvec
self.TVEC = tvec
self.PTimes += 1
return self.MTX,self.DIST,self.RVEC,self.TVEC
def getP(self, dst):
"""
dst: ??????
return self.MTX,self.DIST,self.RVEC,self.TVEC:
?? ?????????????????
"""
if self.SceneImage is None:
return None
corners = np.float32([dst[1], dst[0], dst[2], dst[3]])
gray = cv2.cvtColor(self.SceneImage, cv2.COLOR_BGR2GRAY)
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (1,0,0), (1,1,0)
objp = np.zeros((2*2,3), np.float32)
objp[:,:2] = np.mgrid[0:2,0:2].T.reshape(-1,2)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
if self.PTimes < self.PCount or self.PCount == 0:
# Arrays to store object points and image points from all the images.
objpoints = self.OBJPoints # 3d point in real world space
imgpoints = self.IMGPoints # 2d points in image plane.
if len(imgpoints) == 0 or np.sum(np.abs(imgpoints[-1] - corners2)) != 0:
objpoints.append(objp)
imgpoints.append(corners2)
# Find mtx, dist, rvecs, tvecs
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
if not ret:
self.PTimes += 1
return None
self.OBJPoints = objpoints
self.IMGPoints = imgpoints
self.MTX = mtx
self.DIST = dist
self.RVEC = rvecs[0]
self.TVEC = tvecs[0]
else:
# Find the rotation and translation vectors.
_, rvec, tvec, _= cv2.solvePnPRansac(objp, corners2, self.MTX, self.DIST)
self.RVEC = rvec
self.TVEC = tvec
self.PTimes += 1
return self.MTX,self.DIST,self.RVEC,self.TVEC
def calibrate_intrinsics(camera, image_points,
object_points,
use_rational_model=True,
use_tangential=False,
use_thin_prism=False,
fix_radial=False,
fix_thin_prism=False,
max_iterations=30,
use_existing_guess=False,
test=False):
flags = 0
if test:
flags = flags | cv2.CALIB_USE_INTRINSIC_GUESS
# fix everything
flags = flags | cv2.CALIB_FIX_PRINCIPAL_POINT
flags = flags | cv2.CALIB_FIX_ASPECT_RATIO
flags = flags | cv2.CALIB_FIX_FOCAL_LENGTH
# apparently, we can't fix the tangential distance. What the hell? Zero it out.
flags = flags | cv2.CALIB_ZERO_TANGENT_DIST
flags = fix_radial_flags(flags)
flags = flags | cv2.CALIB_FIX_S1_S2_S3_S4
criteria = (cv2.TERM_CRITERIA_MAX_ITER, 1, 0)
else:
if fix_radial:
flags = fix_radial_flags(flags)
if fix_thin_prism:
flags = flags | cv2.CALIB_FIX_S1_S2_S3_S4
criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, max_iterations,
2.2204460492503131e-16)
if use_existing_guess:
flags = flags | cv2.CALIB_USE_INTRINSIC_GUESS
if not use_tangential:
flags = flags | cv2.CALIB_ZERO_TANGENT_DIST
if use_rational_model:
flags = flags | cv2.CALIB_RATIONAL_MODEL
if len(camera.intrinsics.distortion_coeffs) < 8:
camera.intrinsics.distortion_coeffs.resize((8,))
if use_thin_prism:
flags = flags | cv2.CALIB_THIN_PRISM_MODEL
if len(camera.intrinsics.distortion_coeffs) != 12:
camera.intrinsics.distortion_coeffs = np.resize(camera.intrinsics.distortion_coeffs, (12,))
return __calibrate_intrinsics(camera, image_points, object_points, flags, criteria)
def track_features(self, image_ref, image_cur):
"""Track Features
Parameters
----------
image_ref : np.array
Reference image
image_cur : np.array
Current image
"""
# Re-detect new feature points if too few
if len(self.tracks_tracking) < self.min_nb_features:
self.tracks_tracking = [] # reset alive feature tracks
self.detect(image_ref)
# LK parameters
win_size = (21, 21)
max_level = 2
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.03)
# Convert reference keypoints to numpy array
self.kp_ref = self.last_keypoints()
# Perform LK tracking
lk_params = {"winSize": win_size,
"maxLevel": max_level,
"criteria": criteria}
self.kp_cur, statuses, err = cv2.calcOpticalFlowPyrLK(image_ref,
image_cur,
self.kp_ref,
None,
**lk_params)
# Filter out bad matches (choose only good keypoints)
status = statuses.reshape(statuses.shape[0])
still_alive = []
for i in range(len(status)):
if status[i] == 1:
track_id = self.tracks_tracking[i]
still_alive.append(track_id)
kp = KeyPoint(self.kp_cur[i], 0)
self.tracks[track_id].update(self.frame_id, kp)
self.tracks_tracking = still_alive
def __init__(self, image_topic, feature_detector='FAST'):
super(OpticalFlowMatcher, self).__init__()
rospy.init_node('optical_flow_matcher')
self.cv_bridge = CvBridge()
self.rectified_image_topic = rospy.Subscriber(
image_topic,
Image,
self.new_image_callback
)
self.pub_keypoint_motion = rospy.Publisher(
'keypoint_motion',
KeypointMotion,
queue_size=10
)
self.feature_params = None
if feature_detector == 'FAST':
self.get_features = self.get_features_fast
# Initiate FAST detector with default values
self.fast = cv2.FastFeatureDetector_create()
self.fast.setThreshold(20)
elif feature_detector == 'GOOD':
self.get_features = self.get_features_good
# params for ShiTomasi 'GOOD' corner detection
self.feature_params = dict(
maxCorners=200,
qualityLevel=0.3,
minDistance=7,
blockSize=7
)
else:
raise Exception(
'{} feature detector not implemented'.format(feature_detector)
)
# Parameters for lucas kanade optical flow
self.lk_params = dict(
winSize=(15, 15),
maxLevel=2,
criteria=(
cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
10,
0.03
)
)
self.last_frame_gray = None
self.good_old = None
self.good_new = None
def translationalThermalReg(im1,im2):
import cv2,numpy
#get dimensions
s1=im1.shape
s2=im2.shape
#check sizes agree as a sanity check for inputs
if s1!=s2:
raise TypeError('Array Inputs are of different sizes!')
#Select translation model in CV
warp_model = cv2.MOTION_AFFINE
#Define 2x3 Warp Matrix
warp_matrix = numpy.eye(2, 3, dtype=numpy.float32)
#Number of iterations allowed to converge on solution
num_it=10000
#Terminal Threshold
termTh = 1e-9
#Define Stopping Criteria
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, num_it, termTh)
#Ensure images are of datatype float32 (for compatibility with transformation convergence)
im1=im1.astype(numpy.float32)
im2=im2.astype(numpy.float32)
#Find Ideal Transform given input parameters
(cc, warp_matrix) = cv2.findTransformECC(im1,im2,warp_matrix, warp_model, criteria)
#Apply Transform
aligned = cv2.warpAffine(im2, warp_matrix, (s1[1], s1[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP);
print('Calculated Affine Warp Matrix:')
print(warp_matrix)
return aligned, warp_matrix
#Test Harness for debugging and testing of functions
def camera_cal(self, image):
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
nx = 8
ny = 6
dst = np.copy(image)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((ny * nx, 3), np.float32)
objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
# Search for chessboard corners
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#ret_thresh, mask = cv2.threshold(grey, 30, 255, cv2.THRESH_BINARY)
ret, corners = cv2.findChessboardCorners(image, (nx, ny), None) #flags=(cv2.cv.CV_CALIB_CB_ADAPTIVE_THRESH + cv2.cv.CV_CALIB_CB_FILTER_QUADS))
# If found, add object points, image points
if ret == True:
objpoints.append(objp)
cv2.cornerSubPix(grey,corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners)
self.calibrated = True
print ("FOUND!")
#Draw and display the corners
cv2.drawChessboardCorners(image, (nx, ny), corners, ret)
# Do camera calibration given object points and image points
ret, self.mtx, self.dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, grey.shape[::-1], None, None)
# Save the camera calibration result for later use (we won't worry about rvecs / tvecs)
dist_pickle = {}
dist_pickle["mtx"] = self.mtx
dist_pickle["dist"] = self.dist
dist_pickle['objpoints'] = objpoints
dist_pickle['imgpoints'] = imgpoints
pickle.dump( dist_pickle, open( "/home/wil/ros/catkin_ws/src/av_sim/computer_vision/camera_calibration/data/camera_cal_pickle.p", "wb" ) )
#else:
#print("Searching...")
return image
def find_chessboard(self, sx=6, sy=9):
"""Finds the corners of an sx X sy chessboard in the image.
Parameters
----------
sx : int
Number of chessboard corners in x-direction.
sy : int
Number of chessboard corners in y-direction.
Returns
-------
:obj:`list` of :obj:`numpy.ndarray`
A list containing the 2D points of the corners of the detected
chessboard, or None if no chessboard found.
"""
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((sx * sy, 3), np.float32)
objp[:, :2] = np.mgrid[0:sx, 0:sy].T.reshape(-1, 2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
# create images
img = self.data.astype(np.uint8)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (sx, sy), None)
# If found, add object points, image points (after refining them)
if ret:
objpoints.append(objp)
cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners)
if corners is not None:
return corners.squeeze()
return None