本文整理汇总了Python中cv2.cornerSubPix方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.cornerSubPix方法的具体用法?Python cv2.cornerSubPix怎么用?Python cv2.cornerSubPix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.cornerSubPix方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calculateCorners
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def calculateCorners(self, gray, points=None):
'''
gray is OpenCV gray image,
points is Marker.points
>>> marker.calculateCorners(gray)
>>> print(marker.corners)
'''
if points is None: points = self.points
if points is None: raise TypeError('calculateCorners need a points value')
'''
rotations = 0 -> 0,1,2,3
rotations = 1 -> 3,0,1,2
rotations = 2 -> 2,3,0,1
rotations = 3 -> 1,2,3,0
=> A: 1,0,3,2; B: 0,3,2,1; C: 2,1,0,3; D: 3,2,1,0
'''
i = self.rotations
A = (1,0,3,2)[i]; B = (0,3,2,1)[i]; C = (2,1,0,3)[i]; D = (3,2,1,0)[i]
corners = np.float32([points[A], points[B], points[C], points[D]])
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)
self.corners = cv2.cornerSubPix(gray, corners, (5,5), (-1,-1), criteria)
示例2: add_corners
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def add_corners(self, i_frame, subpixel_criteria, frame_folder_path=None,
save_image=False, save_chekerboard_overlay=False):
grey_frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
cv2.cornerSubPix(grey_frame, self.current_image_points, (11, 11), (-1, -1), subpixel_criteria)
if save_image:
png_path = (os.path.join(frame_folder_path,
"{0:s}{1:04d}{2:s}".format(self.name, i_frame, ".png")))
cv2.imwrite(png_path, self.frame)
if save_chekerboard_overlay:
png_path = (os.path.join(frame_folder_path,
"checkerboard_{0:s}{1:04d}{2:s}".format(self.name, i_frame, ".png")))
overlay = self.frame.copy()
cv2.drawChessboardCorners(overlay, self.current_board_dims, self.current_image_points, True)
cv2.imwrite(png_path, overlay)
self.usable_frames[i_frame] = len(self.image_points)
self.image_points.append(self.current_image_points)
示例3: get_vectors
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def get_vectors(image, points, mtx, dist):
# order points
points = _order_points(points)
# set up criteria, image, points and axis
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
imgp = np.array(points, dtype='float32')
objp = np.array([[0.,0.,0.],[1.,0.,0.],
[1.,1.,0.],[0.,1.,0.]], dtype='float32')
# calculate rotation and translation vectors
cv2.cornerSubPix(gray,imgp,(11,11),(-1,-1),criteria)
rvecs, tvecs, _ = cv2.solvePnPRansac(objp, imgp, mtx, dist)
return rvecs, tvecs
示例4: processImage
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def processImage(fn):
print('processing %s... ' % fn)
img = cv.imread(fn, 0)
if img is None:
print("Failed to load", fn)
return None
assert w == img.shape[1] and h == img.shape[0], ("size: %d x %d ... " % (img.shape[1], img.shape[0]))
found, corners = cv.findChessboardCorners(img, pattern_size)
if found:
term = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_COUNT, 30, 0.1)
cv.cornerSubPix(img, corners, (5, 5), (-1, -1), term)
if debug_dir:
vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
cv.drawChessboardCorners(vis, pattern_size, corners, found)
_path, name, _ext = splitfn(fn)
outfile = os.path.join(debug_dir, name + '_chess.png')
cv.imwrite(outfile, vis)
if not found:
print('chessboard not found')
return None
print(' %s... OK' % fn)
return (corners.reshape(-1, 2), pattern_points)
示例5: _get_corners
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def _get_corners(self, image):
"""Find subpixel chessboard corners in image."""
temp = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(temp,
(self.rows, self.columns))
if not ret:
raise ChessboardNotFoundError("No chessboard could be found.")
cv2.cornerSubPix(temp, corners, (11, 11), (-1, -1),
(cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS,
30, 0.01))
return corners
示例6: cube
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def cube(img):
#img_in = cv2.imread("Picture 27.jpg")
#img = cv2.resize(img_in,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
#cv2.imshow('img',img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#cv2.imshow('gray',gray)
ret, corners = cv2.findChessboardCorners(gray, (8,7),None)
# print ret,corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((7*8,3), np.float32)
objp[:,:2] = np.mgrid[0:8,0:7].T.reshape(-1,2)
#axis = np.float32([[3,0,0], [0,3,0], [0,0,-3]]).reshape(-1,3)
axis = np.float32([[0,0,0], [0,3,0], [3,3,0], [3,0,0],
[0,0,-3],[0,3,-3],[3,3,-3],[3,0,-3] ])
if ret == True:
cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
# Find the rotation and translation vectors.
rvecs, tvecs, inliers = cv2.solvePnPRansac(objp, corners, mtx, dist)
# project 3D points to image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
#print imgpts
img = draw2(img,corners,imgpts)
return img
示例7: process_images
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def process_images(self, filepath):
"""Read images, detect corners, refine corners, and save data."""
# Arrays to store object points and image points from all the images.
self.objpoints = [] # 3d point in real world space
self.imgpoints_l = [] # 2d points in image plane.
self.imgpoints_r = [] # 2d points in image plane.
self.calib_successes = [] # polygon ids of left/right image sets with checkerboard corners.
images_left = glob.glob(filepath + "/left/*")
images_right = glob.glob(filepath + "/right/*")
images_left.sort()
images_right.sort()
print("\nAttempting to read images for left camera from dir: " +
filepath + "/left/")
print("Attempting to read images for right camera from dir: " +
filepath + "/right/")
assert len(images_left) != 0, "ERROR: Images not read correctly, check directory"
assert len(images_right) != 0, "ERROR: Images not read correctly, check directory"
for image_left, image_right in zip(images_left, images_right):
img_l = cv2.imread(image_left, 0)
img_r = cv2.imread(image_right, 0)
assert img_l is not None, "ERROR: Images not read correctly"
assert img_r is not None, "ERROR: Images not read correctly"
print("Finding chessboard corners for %s and %s..." % (os.path.basename(image_left), os.path.basename(image_right)))
start_time = time.time()
# Find the chess board corners
flags = 0
flags |= cv2.CALIB_CB_ADAPTIVE_THRESH
flags |= cv2.CALIB_CB_NORMALIZE_IMAGE
ret_l, corners_l = cv2.findChessboardCorners(img_l, (9, 6), flags)
ret_r, corners_r = cv2.findChessboardCorners(img_r, (9, 6), flags)
# termination criteria
self.criteria = (cv2.TERM_CRITERIA_MAX_ITER +
cv2.TERM_CRITERIA_EPS, 30, 0.001)
# if corners are found in both images, refine and add data
if ret_l and ret_r:
self.objpoints.append(self.objp)
rt = cv2.cornerSubPix(img_l, corners_l, (5, 5),
(-1, -1), self.criteria)
self.imgpoints_l.append(corners_l)
rt = cv2.cornerSubPix(img_r, corners_r, (5, 5),
(-1, -1), self.criteria)
self.imgpoints_r.append(corners_r)
self.calib_successes.append(polygon_from_image_name(image_left))
print("\t[OK]. Took %i seconds." % (round(time.time() - start_time, 2)))
else:
print("\t[ERROR] - Corners not detected. Took %i seconds." % (round(time.time() - start_time, 2)))
self.img_shape = img_r.shape[::-1]
print(str(len(self.objpoints)) + " of " + str(len(images_left)) +
" images being used for calibration")
self.ensure_valid_images()
示例8: get_K_and_D
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def get_K_and_D(checkerboard, imgsPath):
CHECKERBOARD = checkerboard
subpix_criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)
calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC+cv2.fisheye.CALIB_CHECK_COND+cv2.fisheye.CALIB_FIX_SKEW
objp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)
objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
_img_shape = None
objpoints = []
imgpoints = []
images = glob.glob(imgsPath + '/*.png')
for fname in images:
img = cv2.imread(fname)
if _img_shape == None:
_img_shape = img.shape[:2]
else:
assert _img_shape == img.shape[:2], "All images must share the same size."
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD,cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
if ret == True:
objpoints.append(objp)
cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),subpix_criteria)
imgpoints.append(corners)
N_OK = len(objpoints)
K = np.zeros((3, 3))
D = np.zeros((4, 1))
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]
rms, _, _, _, _ = cv2.fisheye.calibrate(
objpoints,
imgpoints,
gray.shape[::-1],
K,
D,
rvecs,
tvecs,
calibration_flags,
(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
)
DIM = _img_shape[::-1]
print("Found " + str(N_OK) + " valid images for calibration")
print("DIM=" + str(_img_shape[::-1]))
print("K=np.array(" + str(K.tolist()) + ")")
print("D=np.array(" + str(D.tolist()) + ")")
return DIM, K, D
示例9: load_frame_images
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def load_frame_images(self):
"""
Load images (or image pairs) from self.full_frame_folder_path
"""
print("Loading frames from '{0:s}'".format(self.full_frame_folder_path))
all_files = [f for f in os.listdir(self.full_frame_folder_path)
if osp.isfile(osp.join(self.full_frame_folder_path, f)) and f.endswith(".png")]
all_files.sort()
usable_frame_ct = sys.maxsize
frame_number_sets = []
for video in self.videos:
# assume matching numbers in corresponding left & right files
files = [f for f in all_files if f.startswith(video.name)]
files.sort() # added to be explicit
cam_frame_ct = 0
frame_numbers = []
for ix_pair in range(len(files)):
frame = cv2.imread(osp.join(self.full_frame_folder_path, files[ix_pair]))
frame_number = int(re.search(r'\d\d\d\d', files[ix_pair]).group(0))
frame_numbers.append(frame_number)
found, corners = cv2.findChessboardCorners(frame, self.board_dims)
if not found:
raise ValueError("Could not find corners in image '{0:s}'".format(files[ix_pair]))
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.cornerSubPix(grey, corners, (11, 11), (-1, -1), self.criteria_subpix)
video.image_points.append(corners)
video.usable_frames[frame_number] = ix_pair
cam_frame_ct += 1
usable_frame_ct = min(usable_frame_ct, cam_frame_ct)
frame_number_sets.append(frame_numbers)
if len(self.videos) > 1:
# check that all cameras have the same frame number sets
if len(frame_number_sets[0]) != len(frame_number_sets[1]):
raise ValueError(
"There are some non-paired frames in folder '{0:s}'".format(self.full_frame_folder_path))
for i_fn in range(len(frame_number_sets[0])):
fn0 = frame_number_sets[0][i_fn]
fn1 = frame_number_sets[1][i_fn]
if fn0 != fn1:
raise ValueError("There are some non-paired frames in folder '{0:s}'." +
" Check frame {1:d} for camera {2:s} and frame {3:d} for camera {4:s}."
.format(self.full_frame_folder_path,
fn0, self.videos[0].name,
fn1, self.videos[1].name))
for i_frame in range(usable_frame_ct):
self.object_points.append(self.board_object_corner_set)
return usable_frame_ct
示例10: getP
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
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
示例11: calibrate
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def calibrate(dirpath, prefix, image_format, square_size, width=9, height=6):
""" Apply camera calibration operation for images in the given directory path. """
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(8,6,0)
objp = np.zeros((height*width, 3), np.float32)
objp[:, :2] = np.mgrid[0:width, 0:height].T.reshape(-1, 2)
objp = objp * square_size # Create real world coords. Use your metric.
# 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.
# Directory path correction. Remove the last character if it is '/'
if dirpath[-1:] == '/':
dirpath = dirpath[:-1]
# Get the images
images = glob.glob(dirpath+'/' + prefix + '*.' + image_format)
# Iterate through the pairs and find chessboard corners. Add them to arrays
# If openCV can't find the corners in an image, we discard the image.
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (width, height), None)
# If found, add object points, image points (after refining them)
if ret:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)
# Draw and display the corners
# Show the image to see if pattern is found ! imshow function.
img = cv2.drawChessboardCorners(img, (width, height), corners2, ret)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
return [ret, mtx, dist, rvecs, tvecs]
示例12: find_chessboard
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
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
示例13: export_loader
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import cornerSubPix [as 别名]
def export_loader(image, name, experiment, **config):
has_keypoints = config.get('has_keypoints', True)
has_descriptors = config.get('has_descriptors', True)
num_features = config.get('num_features', 0)
remove_borders = config.get('remove_borders', 0)
keypoint_predictor = config.get('keypoint_predictor', None)
do_nms = config.get('do_nms', False)
nms_thresh = config.get('nms_thresh', 4)
keypoint_refinement = config.get('keypoint_refinement', False)
binarize = config.get('binarize', False)
entries = ['keypoints', 'scores', 'descriptors', 'local_descriptors']
name = name.decode('utf-8') if isinstance(name, bytes) else name
path = Path(EXPER_PATH, 'exports', experiment, name+'.npz')
with np.load(path) as p:
pred = {k: v.copy() for k, v in p.items()}
image_shape = image.shape[:2]
if keypoint_predictor:
keypoint_config = config.get('keypoint_config', config)
keypoint_config['keypoint_predictor'] = None
pred_detector = keypoint_predictor(
image, name, **{'experiment': experiment, **keypoint_config})
pred['keypoints'] = pred_detector['keypoints']
pred['scores'] = pred_detector['scores']
elif has_keypoints:
assert 'keypoints' in pred
if remove_borders:
mask = keypoints_filter_borders(
pred['keypoints'], image_shape, remove_borders)
pred = {**pred,
**{k: v[mask] for k, v in pred.items() if k in entries}}
if do_nms:
keep = nms_fast(
pred['keypoints'], pred['scores'], image_shape, nms_thresh)
pred = {**pred,
**{k: v[keep] for k, v in pred.items() if k in entries}}
if keypoint_refinement:
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
30, 0.001)
pred['keypoints'] = cv2.cornerSubPix(
image, np.float32(pred['keypoints']),
(3, 3), (-1, -1), criteria)
if num_features:
keep = np.argsort(pred['scores'])[::-1][:num_features]
pred = {**pred,
**{k: v[keep] for k, v in pred.items() if k in entries}}
if has_descriptors:
if 'descriptors' in pred:
pass
elif 'local_descriptors' in pred:
pred['descriptors'] = pred['local_descriptors']
else:
assert 'local_descriptor_map' in pred
pred['descriptors'] = sample_descriptors(
pred['local_descriptor_map'], pred['keypoints'], image_shape,
input_shape=pred['input_shape'][:2] if 'input_shape' in pred
else None)
if binarize:
pred['descriptors'] = pred['descriptors'] > 0
return pred