python - 计算图像对象的中心点以确定方向

标签 python opencv direction opencv-contour

我想在Python中查看箭头指向图片的方向。在以下算法中,我计算箭头的重心:

image = cv2.imread(image.tiff")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

for c in cnts:
    M = cv2.moments(c)
    cX = int(M["m10"] / M["m00"])   # center of gravity (x coordinate)
    cY = int(M["m01"] / M["m00"])   # center of gravity (y coordinate)

enter image description here
(红点是重心)

还有另一种确定箭头中心点的方法,以便可以使用中心点和重心之间的差来计算箭头指向的方向吗?

最佳答案

这是我更详尽的答案...希望评论足够清楚!

import matplotlib.pyplot as plt
import imageio
import os
import numpy as np

# Load the image with imageio...
base_path = r'D:\temp'
im_name = 'arrow_image.png'
im_dir = os.path.join(base_path, im_name)
A = imageio.imread(im_dir)

# Get all the points that have a value in the A[:,:,0]-dimension of the image
pts2 = np.array([[j,i] for i in range(A.shape[0]) for j in range(A.shape[1]) if A[i,j,0] > 0])
# Using convexhull.. get an overlay of vertices for these found points
ch = ConvexHull(pts2)

# If you want to visualize them.. you can do that by
plt.plot(pts2[:, 0], pts2[:, 1], 'ko', markersize=10)
plt.show()

# Be aware that in this whole process your image might look like it has rotated.. so be aware of the dimension (and the way stuff is visualized)

# Get the indices of the hull points.
hull_indices = ch.vertices

# These are the actual points.
hull_pts = pts2[hull_indices, :]

# With this you are able to determine the 'end' points of your arrow. Fiddle around with it, know what you are trying to get and understand it.
z = [np.linalg.norm(x) for x in pts2]
z_max_norm = np.where(z==max(z))
z_min_norm = np.where(z==min(z))
y_max = np.where(pts2[:,1] == min(pts2[:,1]))[0]
x_min = np.where(pts2[:,0] == min(pts2[:,0]))[0]

# And here some more visualization tricks
plt.fill(hull_pts[:,0], hull_pts[:,1], fill=False, edgecolor='b')
# plt.scatter(pts2[z_min,0],pts2[z_min, 1])
# plt.scatter(pts2[z_max,0],pts2[z_max, 1])
plt.scatter(pts2[y_max,0],pts2[y_max, 1])
plt.scatter(pts2[x_min,0],pts2[x_min, 1])

plt.show()

这使用与@GPPK答案相同的策略。我的另一个解决方案是将数据转换为1d ...您可以想到这样的转换
pts3 = np.array([A[i,j,0]*i*np.log(j) for i in range(A.shape[0]) for j in range(A.shape[1]) if A[i,j,0] > 0])
plt.plot(pts3)
plt.show()

但是您必须对此进行一点测试,也许更改一些参数。这个想法是,您可以折扣某些尺寸以恢复箭头的形状。

关于python - 计算图像对象的中心点以确定方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49068444/

相关文章:

c# - 如何从鼠标坐标获取基本鼠标方向

javascript - 桥接 Python 后端和 JavaScript 前端

python opencv 不会使用 Video-writer 对象编写视频?

C++、OpenCV:异步运行 cuda::dft

c++ - C++ OpenCV 中的 cvPtr2D 等价物

ios实现任意方向任意角度的UISwipeGestureRecogniser

python - 如何保存memcache值直到配额被补充?

python - 如何编写多个整数数组的测试?

python - 将 zxJDBC 与 jython 一起使用不起作用

ios - 如何在 iOS 的 UITableViewCell 上禁用特定方向的滑动?