本文介绍了在OpenCV中是否有可能将局部曲率绘制为表示对象“点”的热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 给定一个可以检测并绘制轮廓的斑点的阈值图像,是否可以在绘制轮廓时将局部曲率表示为热图? Given a thresholded image of blobs that you can detect and draw contours around, is it possible when drawing the contour to represent the local curvature as a heat-map? 即。是(1)可以确定开放的CV轮廓上的局部曲率(2)将该曲率映射到热图颜色空间(3)将轮廓绘制为热图。i.e. is it (1) possible to determine local curvature on a open cv contour (2) map this curvature to a heat-map color space (3) draw the contour as a heatmap.我的目标是测量一个物体的尖锐度,这样我就可以从尖端到相反的非尖端绘制一个矢量。对于我的物体,我碰巧知道尖尖的一面是顶部。 My goal is to measure the "pointiness" of an object so that I can draw a vector from the pointy side to the opposite non-pointy side. For my objects, I happen to know that the pointy side is the top. 如果其他技巧在表示点方面比曲率更有效,可以随意提出。If other techniques would be more effective at representing "pointiness" than curvature feel free to suggest. 推荐答案 编辑:修正了之前版本中的一个错误。EDIT: Fixed a bug in the previous version.我使用了第i个和第(i + n)个点上的渐变向量之间的角度作为得分确定一个点的重要性。代码和结果如下。I used angle between the gradient vectors at the ith and (i + n)th point on the contour as the score to determine the pointiness of a point. Code and results below.import numpy as npimport cv2import pylab as pldef compute_pointness(I, n=5): # Compute gradients # GX = cv2.Sobel(I, cv2.CV_32F, 1, 0, ksize=5, scale=1) # GY = cv2.Sobel(I, cv2.CV_32F, 0, 1, ksize=5, scale=1) GX = cv2.Scharr(I, cv2.CV_32F, 1, 0, scale=1) GY = cv2.Scharr(I, cv2.CV_32F, 0, 1, scale=1) GX = GX + 0.0001 # Avoid div by zero # Threshold and invert image for finding contours _, I = cv2.threshold(I, 100, 255, cv2.THRESH_BINARY_INV) # Pass in copy of image because findContours apparently modifies input. C, H = cv2.findContours(I.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) heatmap = np.zeros_like(I, dtype=np.float) pointed_points = [] for contour in C: contour = contour.squeeze() measure = [] N = len(contour) for i in xrange(N): x1, y1 = contour[i] x2, y2 = contour[(i + n) % N] # Angle between gradient vectors (gx1, gy1) and (gx2, gy2) gx1 = GX[y1, x1] gy1 = GY[y1, x1] gx2 = GX[y2, x2] gy2 = GY[y2, x2] cos_angle = gx1 * gx2 + gy1 * gy2 cos_angle /= (np.linalg.norm((gx1, gy1)) * np.linalg.norm((gx2, gy2))) angle = np.arccos(cos_angle) if cos_angle < 0: angle = np.pi - angle x1, y1 = contour[((2*i + n) // 2) % N] # Get the middle point between i and (i + n) heatmap[y1, x1] = angle # Use angle between gradient vectors as score measure.append((angle, x1, y1, gx1, gy1)) _, x1, y1, gx1, gy1 = max(measure) # Most pointed point for each contour # Possible to filter for those blobs with measure > val in heatmap instead. pointed_points.append((x1, y1, gx1, gy1)) heatmap = cv2.GaussianBlur(heatmap, (3, 3), heatmap.max()) return heatmap, pointed_pointsdef plot_points(image, pointed_points, radius=5, color=(255, 0, 0)): for (x1, y1, _, _) in pointed_points: cv2.circle(image, (x1, y1), radius, color, -1)def main(): I = cv2.imread("glLqt.jpg", 0) heatmap, pointed_points = compute_pointness(I, n=5) pl.figure() pl.imshow(heatmap, cmap=pl.cm.jet) pl.colorbar() I_color = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB) plot_points(I_color, pointed_points) pl.figure() pl.imshow(I_color)if __name__ == '__main__': main() 请注意,热图中更清晰的点更明亮。Notice that sharper points are brighter in the heatmap. 这篇关于在OpenCV中是否有可能将局部曲率绘制为表示对象“点”的热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 20:57