def inverse_transform(self, img, offset=0):
if offset == 0:
return self.warp(img, self.M_inv)
else:
src = self.src.copy()
src[:, 0] = src[:, 0] + offset
dst = self.dst.copy()
dst[:, 0] = dst[:, 0] + offset
M_inv = cv2.getPerspectiveTransform(dst, src)
return self.warp(img, M_inv)
python类getPerspectiveTransform()的实例源码
def calibrate(self, img):
corners = self.detect_corners(img)
transform_matrix = cv2.getPerspectiveTransform(corners, self.corner_coords)
return cv2.warpPerspective(img, transform_matrix, self.area)
def windowToFieldCoordinates(basePoint, x1, y1, x2, y2, x3, y3, x4, y4, maxWidth=0, maxHeight=0):
(xp, yp) = basePoint
src = np.array([
[x1, y1],
[x2, y2],
[x3, y3],
[x4, y4]], dtype = "float32")
# those should be the same aspect as the real width/height of field
maxWidth = (x4-x1) if maxWidth == 0 else maxWidth
maxHeight = (y1-y2) if maxHeight == 0 else maxHeight
# make a destination rectangle with the width and height of above (starts at 0,0)
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
# find the transformation matrix for our transforms
transformationMatrix = cv2.getPerspectiveTransform(src, dst)
# put the original (source) x,y points in an array (not sure why do we have to put it 3 times though)
original = np.array([((xp, yp), (xp, yp), (xp, yp))], dtype=np.float32)
# use perspectiveTransform to transform our original(mouse coords) to new coords with the transformation matrix
transformed = cv2.perspectiveTransform(original, transformationMatrix)[0][0]
return transformed
def old_get_perspective_transform_normalized(p1,p2):
"""
A small wrapper around cv2.getPerspectiveTransform, with normalization of
point locations.
"""
return cv2.getPerspectiveTransform(p1,p2)
mu1 = p1.mean(axis=0)
std1 = p1.std(axis=0)
mu2 = p2.mean(axis=0)
std2 = p2.std(axis=0)
p1_ = (p1 - mu1) / std1
p2_ = (p2 - mu2) / std2
H_ = cv2.getPerspectiveTransform(p1_,p2_)
A1 = np.array([[1.0/std1[0], 0.0, -mu1[0]/std1[0]],
[0, 1.0/std1[1], -mu1[1]/std1[1]],
[0,0,1.0]])
A2inv = np.array([[std2[0], 0.0, mu2[0]],
[0, std2[1], mu2[1]],
[0,0,1.0]])
H = A2inv.dot(H_).dot(A1)
return H
def old_find_homography_normalized(p1,p2):
"""
A small wrapper around cv2.getPerspectiveTransform, with normalization of
point locations.
"""
return cv2.findHomography(p1,p2,method=cv2.LMEDS)[0]
mu1 = p1.mean(axis=0)
std1 = p1.std(axis=0)
mu2 = p2.mean(axis=0)
std2 = p2.std(axis=0)
p1_ = (p1 - mu1) / std1
p2_ = (p2 - mu2) / std2
H_ = cv2.findHomography(p1_,p2_,method=cv2.LMEDS)[0]
A1 = np.array([[1.0/std1[0], 0.0, -mu1[0]/std1[0]],
[0, 1.0/std1[1], -mu1[1]/std1[1]],
[0,0,1.0]])
A2inv = np.array([[std2[0], 0.0, mu2[0]],
[0, std2[1], mu2[1]],
[0,0,1.0]])
H = A2inv.dot(H_).dot(A1)
return H
def randomShiftScaleRotate(image, mask, mask_w,
shift_limit=(-0.0625, 0.0625),
scale_limit=(-0.1, 0.1),
rotate_limit=(-45, 45), aspect_limit=(0, 0),
borderMode=cv2.BORDER_CONSTANT, u=0.5):
if np.random.random() < u:
height, width, channel = image.shape
angle = np.random.uniform(rotate_limit[0], rotate_limit[1]) # degree
scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1])
aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1])
sx = scale * aspect / (aspect ** 0.5)
sy = scale / (aspect ** 0.5)
dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width)
dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height)
cc = np.math.cos(angle / 180 * np.math.pi) * sx
ss = np.math.sin(angle / 180 * np.math.pi) * sy
rotate_matrix = np.array([[cc, -ss], [ss, cc]])
box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
box1 = box0 - np.array([width / 2, height / 2])
box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])
box0 = box0.astype(np.float32)
box1 = box1.astype(np.float32)
mat = cv2.getPerspectiveTransform(box0, box1)
image = cv2.warpPerspective(image, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask_w = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
return image, mask, mask_w
psuedo_label_dataset_generator.py 文件源码
项目:unet-tensorflow
作者: timctho
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def randomShiftScaleRotate(image, mask, mask_w,
shift_limit=(-0.0625, 0.0625),
scale_limit=(-0.1, 0.1),
rotate_limit=(-45, 45), aspect_limit=(0, 0),
borderMode=cv2.BORDER_CONSTANT, u=0.5):
if np.random.random() < u:
height, width, channel = image.shape
angle = np.random.uniform(rotate_limit[0], rotate_limit[1]) # degree
scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1])
aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1])
sx = scale * aspect / (aspect ** 0.5)
sy = scale / (aspect ** 0.5)
dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width)
dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height)
cc = np.math.cos(angle / 180 * np.math.pi) * sx
ss = np.math.sin(angle / 180 * np.math.pi) * sy
rotate_matrix = np.array([[cc, -ss], [ss, cc]])
box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
box1 = box0 - np.array([width / 2, height / 2])
box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])
box0 = box0.astype(np.float32)
box1 = box1.astype(np.float32)
mat = cv2.getPerspectiveTransform(box0, box1)
image = cv2.warpPerspective(image, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask_w = cv2.warpPerspective(mask_w, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
return image, mask, mask_w
def trans_per(self, image):
image = self.binary_extraction(image)
self.binary_image = image
ysize = image.shape[0]
xsize = image.shape[1]
# define region of interest
left_bottom = (xsize/10, ysize)
apex_l = (xsize/2 - 2600/(self.look_ahead**2), ysize - self.look_ahead*275/30)
apex_r = (xsize/2 + 2600/(self.look_ahead**2), ysize - self.look_ahead*275/30)
right_bottom = (xsize - xsize/10, ysize)
# define vertices for perspective transformation
src = np.array([[left_bottom], [apex_l], [apex_r], [right_bottom]], dtype=np.float32)
dst = np.float32([[xsize/3,ysize],[xsize/4.5,0],[xsize-xsize/4.5,0],[xsize-xsize/3, ysize]])
self.M = cv2.getPerspectiveTransform(src, dst)
self.Minv = cv2.getPerspectiveTransform(dst, src)
if len(image.shape) > 2:
warped = cv2.warpPerspective(image, self.M, image.shape[-2:None:-1], flags=cv2.INTER_LINEAR)
else:
warped = cv2.warpPerspective(image, self.M, image.shape[-1:None:-1], flags=cv2.INTER_LINEAR)
return warped
# creat window mask for lane detecion
def perspectiveTransform(self):
folder=self.sort_files()
P=self.get_points()
self.height,self.width=cv2.imread("Frames/1.jpg").shape[:2]
# Process frames
for i in folder:
pic="Frames/"+str(i)+".jpg"
img = cv2.imread(pic)
pts1 = np.float32([[P[0][0],P[0][1]],[P[1][0],P[1][1]],[P[2][0],P[2][1]],[P[3][0],P[3][1]]])
pts2 = np.float32([[P[0][2],P[0][3]],[P[1][2],P[1][3]],[P[2][2],P[2][3]],[P[3][2],P[3][3]]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(self.width,self.height))
cv2.imwrite("Frames/%d.jpg" % i, dst)
# Get x,y co-ordinates
def crop_out(img,x1,y1,x2,y2,b,h):
'''
This function is used to crop the image into desired dimmensions.
img: image from which the rectangle is to be cropped
x1,y1: top left vertex parameter
x2,y2: bottom right parameter
b,h: dimmesions of the cropped image
'''
xa=x1
xb=x2
xc=x1
xd=x2
ya=y1
yb=y1
yc=y2
yd=y2
pts1 = np.float32([[xa,ya],[xc,yc],[xb,yb],[xd,yd]])
pts2 = np.float32([[0,0],[0,h],[b,0],[b,h]])
persM = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,persM,(b,h))
return dst
#detection begins here
def get_warped_image_from_corners(image, corners):
"""
Returns unwarped image of goal, using corners of goal and the original
source image.
Parameters:
:param: `image` - the original source image with the goal in it
:param: `corners` - a numpy array of the corner pixels of the goal
"""
orig_image = numpy.copy(image)
center = get_center(corners)
corners = sort_corners(corners, center)
height_right = int(math.sqrt((corners[1][0][0] - corners[2][0][0]) ** 2 +
(corners[1][0][1] - corners[2][0][1]) ** 2))
height_left = int(math.sqrt((corners[0][0][0] - corners[3][0][0]) ** 2 +
(corners[0][0][1] - corners[3][0][1]) ** 2))
height = int((height_left + height_right) / 2)
width = int(height * (300 / 210))
quad = numpy.zeros((width, height))
quad_pts = numpy.array([[[0, 0]], [[width, 0]],
[[width, height]], [[0, height]]], numpy.float32)
new_image_to_process = numpy.array(image, numpy.float32)
quad_pts = cv2.getPerspectiveTransform(corners, quad_pts)
warped_image = cv2.warpPerspective(new_image_to_process, quad_pts,
(width, height))
return warped_image
# def get_distance_to_goal(orig_image, warped_image):
# angle_between_sides = (len(warped_image[0]) / len(orig_image[0])) * FOV_OF_CAMERA
# print(math.degrees(angle_between_sides))
# return ((WIDTH_OF_GOAL_IN_METERS / 2) / math.sin(angle_between_sides / 2)) * math.sin((math.pi + angle_between_sides) / 2)
def randomShiftScaleRotate(image, mask,
shift_limit=(-0.0625, 0.0625),
scale_limit=(-0.1, 0.1),
rotate_limit=(-45, 45), aspect_limit=(0, 0),
borderMode=cv2.BORDER_CONSTANT, u=0.5, factor=1):
if np.random.random() < u:
height, width, channel = image.shape
angle = np.random.uniform(rotate_limit[0], rotate_limit[1]) # degree
scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1])
aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1])
sx = scale * aspect / (aspect ** 0.5)
sy = scale / (aspect ** 0.5)
dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width)
dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height)
cc = np.math.cos(angle / 180 * np.math.pi) * sx
ss = np.math.sin(angle / 180 * np.math.pi) * sy
rotate_matrix = np.array([[cc, -ss], [ss, cc]])
box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
box1 = box0 - np.array([width / 2, height / 2])
box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])
box0 = box0.astype(np.float32)
box1 = box1.astype(np.float32)
mat = cv2.getPerspectiveTransform(box0, box1)
image = cv2.warpPerspective(image, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
return image, mask
def save_chessboard_img(resize_pic,vertical_position,parallel_position):
pts3 = np.float32([vertical_position[0],vertical_position[1],parallel_position[0],parallel_position[1]])
pts4 = np.float32([[0,0],[640,0],[0,480],[640,480]])
M_perspective = cv2.getPerspectiveTransform(pts3,pts4)
img_perspective = cv2.warpPerspective(resize_pic, M_perspective, (0, 0))
cv2.imwrite('static/InterceptedIMG/clip.jpg',img_perspective)
return img_perspective
def get_topdown_quad(image, src):
# src and dst points
src = _order_points(src)
(max_width,max_height) = _max_width_height(src)
dst = _topdown_points(max_width, max_height)
# warp perspective
matrix = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(image, matrix, _max_width_height(src))
return warped
def four_point_transform(image, pts):
# obtain a consistent order of the points and unpack them
# individually
rect = order_points(pts)
(tl, tr, br, bl) = rect
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
# compute the perspective transform matrix and then apply it
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
# return the warped image
return warped
def randomShiftScaleRotate(image, mask,
shift_limit=(-0.0625, 0.0625),
scale_limit=(-0.1, 0.1),
rotate_limit=(-45, 45), aspect_limit=(0, 0),
borderMode=cv2.BORDER_CONSTANT, u=0.5):
if np.random.random() < u:
height, width, channel = image.shape
angle = np.random.uniform(rotate_limit[0], rotate_limit[1]) # degree
scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1])
aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1])
sx = scale * aspect / (aspect ** 0.5)
sy = scale / (aspect ** 0.5)
dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width)
dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height)
cc = np.math.cos(angle / 180 * np.math.pi) * sx
ss = np.math.sin(angle / 180 * np.math.pi) * sy
rotate_matrix = np.array([[cc, -ss], [ss, cc]])
box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ])
box1 = box0 - np.array([width / 2, height / 2])
box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy])
box0 = box0.astype(np.float32)
box1 = box1.astype(np.float32)
mat = cv2.getPerspectiveTransform(box0, box1)
image = cv2.warpPerspective(image, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
mask = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode,
borderValue=(
0, 0,
0,))
return image, mask
digital_display_ocr.py 文件源码
项目:digital-display-character-rec
作者: upupnaway
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def normalize_contrs(img,cntr_pts):
ratio = img.shape[0] / 300.0
norm_pts = np.zeros((4,2), dtype="float32")
s = cntr_pts.sum(axis=1)
norm_pts[0] = cntr_pts[np.argmin(s)]
norm_pts[2] = cntr_pts[np.argmax(s)]
d = np.diff(cntr_pts,axis=1)
norm_pts[1] = cntr_pts[np.argmin(d)]
norm_pts[3] = cntr_pts[np.argmax(d)]
norm_pts *= ratio
(top_left, top_right, bottom_right, bottom_left) = norm_pts
width1 = np.sqrt(((bottom_right[0] - bottom_left[0]) ** 2) + ((bottom_right[1] - bottom_left[1]) ** 2))
width2 = np.sqrt(((top_right[0] - top_left[0]) ** 2) + ((top_right[1] - top_left[1]) ** 2))
height1 = np.sqrt(((top_right[0] - bottom_right[0]) ** 2) + ((top_right[1] - bottom_right[1]) ** 2))
height2 = np.sqrt(((top_left[0] - bottom_left[0]) ** 2) + ((top_left[1] - bottom_left[1]) ** 2))
max_width = max(int(width1), int(width2))
max_height = max(int(height1), int(height2))
dst = np.array([[0,0], [max_width -1, 0],[max_width -1, max_height -1],[0, max_height-1]], dtype="float32")
persp_matrix = cv2.getPerspectiveTransform(norm_pts,dst)
return cv2.warpPerspective(img,persp_matrix,(max_width,max_height))
def four_point_transform(image, pts):
# obtain a consistent order of the points and unpack them
# individually
rect = order_points(pts)
(tl, tr, br, bl) = rect
# compute the width of the new image, which will be the
# maximum distance between bottom-right and bottom-left
# x-coordiates or the top-right and top-left x-coordinates
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
# compute the height of the new image, which will be the
# maximum distance between the top-right and bottom-right
# y-coordinates or the top-left and bottom-left y-coordinates
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
# now that we have the dimensions of the new image, construct
# the set of destination points to obtain a "birds eye view",
# (i.e. top-down view) of the image, again specifying points
# in the top-left, top-right, bottom-right, and bottom-left
# order
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
# compute the perspective transform matrix and then apply it
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
# return the warped image
return warped
def update_matricies(self):
# Build compatible arrays for cv2.getPerspectiveTransform
src = numpy.ones((4,2), dtype=numpy.float32)
dst = numpy.ones((4,2), dtype=numpy.float32)
src[:, :2] = self.align_handles[:4]
dst[:, :2] = corners
# And update the perspective transform
self.persp_matrix = cv2.getPerspectiveTransform(src, dst)
# Now, calculate the scale factor
da = self.dim_handles[1] - self.dim_handles[0]
db = self.dim_handles[3] - self.dim_handles[2]
ma = da.mag()
mb = db.mag()
sf = 100.0/max(ma, mb)
self.placeholder_dim_values[0] = sf * ma * MM
self.placeholder_dim_values[1] = sf * mb * MM
dims = self.__active_dims()
# Perspective transform handles - convert to
handles_pp = []
for handle in self.dim_handles:
p1 = self.persp_matrix.dot(handle.homol())
p1 /= p1[2]
handles_pp.append(p1[:2])
da = handles_pp[1] - handles_pp[0]
db = handles_pp[3] - handles_pp[2]
A = numpy.vstack([da**2, db**2])
B = numpy.array(dims) ** 2
res = numpy.abs(numpy.linalg.solve(A, B)) ** .5
self.scale_matrix = scale(res[0], res[1])
def return_random_perspective(img, row):
perc = 0.1
cols = img.shape[1]
rows = img.shape[0]
start0_max, end0_max, start1_max, end1_max = get_bounding_boxes_positions(img, row)
if start1_max <= 0:
p1 = random.randint(0, int(img.shape[1] * perc))
else:
p1 = random.randint(0, start1_max)
if start0_max <= 0:
p2 = random.randint(0, int(img.shape[0] * perc))
else:
p2 = random.randint(0, start0_max)
if end1_max >= img.shape[1]:
p3 = img.shape[1] - random.randint(0, int(img.shape[1] * perc))
else:
p3 = random.randint(end1_max, img.shape[1])
if start0_max <= 0:
p4 = random.randint(0, int(img.shape[0] * perc))
else:
p4 = random.randint(0, start0_max)
if start1_max <= 0:
p5 = random.randint(0, int(img.shape[1] * perc))
else:
p5 = random.randint(0, start1_max)
if end0_max >= img.shape[0]:
p6 = img.shape[0] - random.randint(0, int(img.shape[0] * perc))
else:
p6 = random.randint(end0_max, img.shape[0])
if end1_max >= img.shape[1]:
p7 = img.shape[1] - random.randint(0, int(img.shape[1] * perc))
else:
p7 = random.randint(end1_max, img.shape[1])
if end0_max >= img.shape[0]:
p8 = img.shape[0] - random.randint(0, int(img.shape[0] * perc))
else:
p8 = random.randint(end0_max, img.shape[0])
pts1 = np.float32([[p1, p2], [p3, p4], [p5, p6], [p7, p8]])
pts2 = np.float32([[0, 0], [cols, 0], [0, rows], [cols, rows]])
M = cv2.getPerspectiveTransform(pts1, pts2)
# img = cv2.rectangle(img, (int(start1_max), int(start0_max)), (int(end1_max), int(end0_max)), (0, 0, 255), thickness=5)
dst = cv2.warpPerspective(img, M, (cols, rows))
# show_resized_image(dst)
return dst