本文整理匯總了Python中cv2.pointPolygonTest方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.pointPolygonTest方法的具體用法?Python cv2.pointPolygonTest怎麽用?Python cv2.pointPolygonTest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.pointPolygonTest方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: mark_hand_center
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def mark_hand_center(frame_in,cont):
max_d=0
pt=(0,0)
x,y,w,h = cv2.boundingRect(cont)
for ind_y in xrange(int(y+0.3*h),int(y+0.8*h)): #around 0.25 to 0.6 region of height (Faster calculation with ok results)
for ind_x in xrange(int(x+0.3*w),int(x+0.6*w)): #around 0.3 to 0.6 region of width (Faster calculation with ok results)
dist= cv2.pointPolygonTest(cont,(ind_x,ind_y),True)
if(dist>max_d):
max_d=dist
pt=(ind_x,ind_y)
if(max_d>radius_thresh*frame_in.shape[1]):
thresh_score=True
cv2.circle(frame_in,pt,int(max_d),(255,0,0),2)
else:
thresh_score=False
return frame_in,pt,max_d,thresh_score
# 6. Find and display gesture
示例2: blendImages
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def blendImages(src, dst, mask, featherAmount=0.2):
#indeksy nie czarnych pikseli maski
maskIndices = np.where(mask != 0)
#te same indeksy tylko, ze teraz w jednej macierzy, gdzie kazdy wiersz to jeden piksel (x, y)
maskPts = np.hstack((maskIndices[1][:, np.newaxis], maskIndices[0][:, np.newaxis]))
faceSize = np.max(maskPts, axis=0) - np.min(maskPts, axis=0)
featherAmount = featherAmount * np.max(faceSize)
hull = cv2.convexHull(maskPts)
dists = np.zeros(maskPts.shape[0])
for i in range(maskPts.shape[0]):
dists[i] = cv2.pointPolygonTest(hull, (maskPts[i, 0], maskPts[i, 1]), True)
weights = np.clip(dists / featherAmount, 0, 1)
composedImg = np.copy(dst)
composedImg[maskIndices[0], maskIndices[1]] = weights[:, np.newaxis] * src[maskIndices[0], maskIndices[1]] + (1 - weights[:, np.newaxis]) * dst[maskIndices[0], maskIndices[1]]
return composedImg
#uwaga, tutaj src to obraz, z ktorego brany bedzie kolor
示例3: inside_point
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def inside_point(point, rect):
# point is a list (x, y)
# rect is a contour with shape [4, 2]
rect = rect.reshape([4, 1, 2]).astype(np.int64)
dist = cv2.pointPolygonTest(rect,(point[0], point[1]),True)
if dist>0:
# print(dist)
return True
else:
return False
示例4: inside_point
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def inside_point(self, point, rect):
# point is a list (x, y)
# rect is a contour with shape [4, 2]
rect = rect.reshape([4, 1, 2]).astype(np.int64)
dist = cv2.pointPolygonTest(rect,(point[0], point[1]),True)
if dist>=0:
# print(dist)
return True
else:
return False
示例5: get_pts_of_interest
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def get_pts_of_interest(keypoints, area_of_interest):
for i in range(int(len(keypoints)/3)):
is_of_interest = cv2.pointPolygonTest(area_of_interest, (keypoints[3*i], keypoints[3*i+1]), False) >= 0
keypoints[3*i] = keypoints[3*i] if is_of_interest else 0
keypoints[3*i+1] = keypoints[3*i+1] if is_of_interest else 0
keypoints[3*i+2] = keypoints[3*i+2] if is_of_interest else 0
return keypoints
示例6: find_innerpoint
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def find_innerpoint(self, cont):
"""
generate an inner point of input polygon using mean of x coordinate by:
1. calculate mean of x coordinate(xmean)
2. calculate maximum and minimum of y coordinate(ymax, ymin)
3. iterate for each y in range (ymin, ymax), find first segment in the polygon
4. calculate means of segment
:param cont: input polygon
:return:
"""
xmean = cont[:, 0, 0].mean()
ymin, ymax = cont[:, 0, 1].min(), cont[:, 0, 1].max()
found = False
found_y = []
#
for i in np.arange(ymin - 1, ymax + 1, 0.5):
# if in_poly > 0, (xmean, i) is in `cont`
in_poly = cv2.pointPolygonTest(cont, (xmean, i), False)
if in_poly > 0:
found = True
found_y.append(i)
# first segment found
if in_poly < 0 and found:
break
if len(found_y) > 0:
return (xmean, np.array(found_y).mean())
# if cannot find using above method, try each point's neighbor
else:
for p in range(len(cont)):
point = cont[p, 0]
for i in range(-1, 2, 1):
for j in range(-1, 2, 1):
test_pt = point + [i, j]
if cv2.pointPolygonTest(cont, (test_pt[0], test_pt[1]), False) > 0:
return test_pt
示例7: in_contour
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def in_contour(self, cont, point):
"""
utility function for judging whether `point` is in the `contour`
:param cont: cv2.findCountour result
:param point: 2d coordinate (x, y)
:return:
"""
x, y = point
return cv2.pointPolygonTest(cont, (x, y), False) > 0
示例8: getEncirclingContour
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def getEncirclingContour(self, contours):
for contour in contours:
if cv2.pointPolygonTest(contour, self.point, False) > 0:
return contour
示例9: whichSquares
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def whichSquares(self, points):
"""
Returns the squares which a list of points lie within. This function is needed to filter out changes in the
images that are compared which have nothing to do with the game, e.g. an arm.
"""
matches = []
for square in self.squares:
for point in points:
dist = cv2.pointPolygonTest(square.contours,point,False)
if dist >= 0:
matches.append(square)
return matches
示例10: get_poly_by_pt_in_hull
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def get_poly_by_pt_in_hull(self, cli_pos):
img_pos = self.cli_to_img_pt(cli_pos)
for poly in reversed(self.ie_polys.get_polys()):
pts = poly.get_pts()
if len(pts) >= 3:
if cv2.pointPolygonTest( pts, tuple(img_pos), False) >= 0:
return poly
return None
示例11: is_inside_table
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def is_inside_table(polygon, reference):
"""
Determine if a given polygon is fully located inside
This works by checking if all polygon points are within (or on the edge of)
the reference
returns True if and only if polygon is fully within or identical to the reference.
"""
brect = cv2.boxPoints(cv2.minAreaRect(polygon))
# All points need to be inside table corners
for point in brect:
if cv2.pointPolygonTest(reference, tuple(point), False) < 0:
return False # Not in contour
return True
示例12: is_in_contour
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def is_in_contour(point, cnt):
"""tell whether a point is in contour or not.
In-contour here includes both the 'in contour' and 'on contour' cases.
point:(x, y)
cnt: a cv2 contour
"""
# doc of pointPolygonTest: http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=pointpolygontest#cv.PointPolygonTest
# the last argument means only tell if in or not, without calculating the shortest distance
in_cnt = cv2.pointPolygonTest(cnt, point, False)
return in_cnt >= 0;
示例13: _pointInsidePolygon
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def _pointInsidePolygon(self,point,polygon):
"""
returns true if tuple point (x,y) is inside polygon of the form ((a,b),(c,d),...,(a,b)) the polygon should be closed
"""
# try:
# import cv2
# except:
# logger.warning("Unable to import cv2")
# return False
if( len(polygon) < 3 ):
logger.warning("feature._pointInsidePolygon - this is not a valid polygon")
return False
if( not isinstance(polygon,list)):
logger.warning("feature._pointInsidePolygon - this is not a valid polygon")
return False
#if( not isinstance(point,tuple) ):
#if( len(point) == 2 ):
# point = tuple(point)
#else:
# logger.warning("feature._pointInsidePolygon - this is not a valid point")
# return False
#if( cv2.__version__ == '$Rev:4557'):
counter = 0
retVal = True
p1 = None
#print "point: " + str(point)
poly = copy.deepcopy(polygon)
poly.append(polygon[0])
#for p2 in poly:
N = len(poly)
p1 = poly[0]
for i in range(1,N+1):
p2 = poly[i%N]
if( point[1] > np.min((p1[1],p2[1])) ):
if( point[1] <= np.max((p1[1],p2[1])) ):
if( point[0] <= np.max((p1[0],p2[0])) ):
if( p1[1] != p2[1] ):
test = float((point[1]-p1[1])*(p2[0]-p1[0]))/float(((p2[1]-p1[1])+p1[0]))
if( p1[0] == p2[0] or point[0] <= test ):
counter = counter + 1
p1 = p2
if( counter % 2 == 0 ):
retVal = False
return retVal
return retVal
#else:
# result = cv2.pointPolygonTest(np.array(polygon,dtype='float32'),point,0)
# return result > 0
示例14: random_warp_src_dest
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def random_warp_src_dest( in_image,srcPoints,destPoints ):
source = srcPoints
destination = (destPoints.copy().astype('float')+numpy.random.normal( size=(destPoints.shape), scale= 2 ))
destination = destination.astype('int')
faceCore =cv2.convexHull( numpy.concatenate([source[17:],destination[17:]], axis=0).astype(int) )
source = [(y,x) for x,y in source]+edgeAnchors
destination = [(y,x) for x,y in destination]+edgeAnchors
indeciesToRemove = set()
for fpl in source,destination:
for i,(y,x) in enumerate(fpl):
if i>17:
break
elif cv2.pointPolygonTest(faceCore,(y,x),False) >= 0:
indeciesToRemove.add(i)
for i in sorted(indeciesToRemove,reverse=True):
source.pop(i)
destination.pop(i)
grid_z = griddata( destination,source, (grid_x, grid_y), method='linear')
map_x = numpy.append([], [ar[:,1] for ar in grid_z]).reshape(256,256)
map_y = numpy.append([], [ar[:,0] for ar in grid_z]).reshape(256,256)
map_x_32 = map_x.astype('float32')
map_y_32 = map_y.astype('float32')
warped = cv2.remap(in_image[:,:,:3], map_x_32, map_y_32, cv2.INTER_LINEAR,cv2.BORDER_TRANSPARENT )
target_mask = in_image[:,:,3].reshape((256,256,1))
target_image = in_image[:,:,:3]
warped = cv2.resize( warped[ 128-120:128+120,128-120:128+120,:] ,(64,64),cv2.INTER_AREA)
target_image = cv2.resize( target_image[ 128-120:128+120,128-120:128+120,: ] ,(64*2,64*2),cv2.INTER_AREA)
target_mask = cv2.resize( target_mask[ 128-120:128+120,128-120:128+120,: ] ,(64*2,64*2),cv2.INTER_AREA).reshape((64*2,64*2,1))
return warped,target_image,target_mask
# get pair of random warped images from aligened face image
示例15: compute_transformed_contour
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import pointPolygonTest [as 別名]
def compute_transformed_contour(width, height, fontsize, M, contour, minarea=0.5):
"""Compute the permitted drawing contour
on a padded canvas for an image of a given size.
We assume the canvas is padded with one full image width
and height on left and right, top and bottom respectively.
Args:
width: Width of image
height: Height of image
fontsize: Size of characters
M: The transformation matrix
contour: The contour to which we are limited inside
the rectangle of size width / height
minarea: The minimum area required for a character
slot to qualify as being visible, expressed as
a fraction of the untransformed fontsize x fontsize
slot.
"""
spacing = math.ceil(fontsize / 2)
xslots = int(np.floor(width / spacing))
yslots = int(np.floor(height / spacing))
ys, xs = np.mgrid[:yslots, :xslots]
basis = np.concatenate([xs[..., np.newaxis], ys[..., np.newaxis]], axis=-1).reshape((-1, 2))
basis *= spacing
slots_pretransform = np.concatenate(
[(basis + offset)[:, np.newaxis, :]
for offset in [[0, 0], [spacing, 0], [spacing, spacing], [0, spacing]]],
axis=1)
slots = cv2.perspectiveTransform(src=slots_pretransform.reshape((1, -1, 2)).astype('float32'),
m=M)[0]
inside = np.array([
cv2.pointPolygonTest(contour=contour, pt=(x, y), measureDist=False) >= 0 for x, y in slots
]).reshape(-1, 4).all(axis=1)
slots = slots.reshape(-1, 4, 2)
areas = np.abs((slots[:, 0, 0] * slots[:, 1, 1] - slots[:, 0, 1] * slots[:, 1, 0]) +
(slots[:, 1, 0] * slots[:, 2, 1] - slots[:, 1, 1] * slots[:, 2, 0]) +
(slots[:, 2, 0] * slots[:, 3, 1] - slots[:, 2, 1] * slots[:, 3, 0]) +
(slots[:, 3, 0] * slots[:, 0, 1] - slots[:, 3, 1] * slots[:, 0, 0])) / 2
slots_filtered = slots_pretransform[(areas > minarea * spacing * spacing) & inside]
temporary_image = cv2.drawContours(image=np.zeros((height, width), dtype='uint8'),
contours=slots_filtered,
contourIdx=-1,
color=255)
temporary_image = cv2.dilate(src=temporary_image, kernel=np.ones((spacing, spacing)))
newContours, _ = cv2.findContours(temporary_image,
mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_SIMPLE)
x, y = slots_filtered[0][0]
contour = newContours[next(
index for index, contour in enumerate(newContours)
if cv2.pointPolygonTest(contour=contour, pt=(x, y), measureDist=False) >= 0)][:, 0, :]
return contour