使用 python 从坐标元组中查找平均坐标

Find average coordinate from a coordinate tuple with python

你好,我有一个黑色的背景,有一个白点。我有白色像素的所有坐标。

points = np.where(image==255)

"print points" 给了我这个输出,我看到元组列表中有两个数组:

(array([119, 119, 119, 119, 120, 120, 120, 120, 120, 120, 120, 120, 120,
   120, 120, 120, 121, 121, 121, 121, 121, 121, 121, 121, 121, 121,
   121, 121, 121, 121, 121, 121, 122, 122, 122, 122, 122, 122, 122,
   122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
   123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
   123, 123, 123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124,
   124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124,
   124, 124, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 125,
   125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125,
   125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126,
   126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126,
   126, 126, 126, 126, 126, 126, 126, 126, 126, 127, 127, 127, 127,
   127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
   127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
   128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
   128, 128, 128, 128, 129, 129, 129, 129, 129, 129, 129, 129, 129,
   129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 129,
   129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130,
   130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
   130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
   130, 130, 130, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131,
   131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131,
   131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 131, 132, 132,
   132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132,
   132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132, 132,
   132, 132, 132, 132, 132, 132, 133, 133, 133, 133, 133, 133, 133,
   133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
   133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133,
   133, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134,
   134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 134,
   134, 134, 134, 134, 134, 134, 134, 134, 134, 134, 135, 135, 135,
   135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
   135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 135,
   135, 135, 135, 135, 135, 135, 136, 136, 136, 136, 136, 136, 136,
   136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
   136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
   136, 136, 136, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
   137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
   137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137, 137,
   138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138,
   138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138,
   138, 138, 138, 138, 138, 138, 138, 138, 138, 139, 139, 139, 139,
   139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
   139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
   139, 139, 139, 139, 139, 140, 140, 140, 140, 140, 140, 140, 140,
   140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140,
   140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140, 140,
   141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141,
   141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141, 141,
   141, 141, 141, 141, 141, 141, 141, 141, 142, 142, 142, 142, 142,
   142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
   142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142, 142,
   142, 142, 142, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143,
   143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 143,
   143, 143, 143, 143, 143, 143, 143, 143, 143, 143, 144, 144, 144,
   144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144,
   144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144,
   144, 144, 144, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
   145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145, 145,
   145, 145, 145, 145, 145, 145, 145, 145, 146, 146, 146, 146, 146,
   146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146,
   146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 147,
   147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147,
   147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147, 147,
   147, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148,
   148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148, 148,
   148, 148, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149,
   149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149,
   149, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150,
   150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 151, 151,
   151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151, 151,
   151, 151, 151, 151, 151, 152, 152, 152, 152, 152, 152, 152, 152,
   152, 152, 152, 152, 152, 152, 152, 152, 152, 153, 153, 153, 153,
   153, 153, 153, 153, 153, 153, 153, 153, 153, 154, 154, 154, 154, 154]), array([77, 78, 79, 80, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 71,
   72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 69, 70,
   71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
   88, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
   84, 85, 86, 87, 88, 89, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
   78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 66, 67, 68, 69,
   70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
   87, 88, 89, 90, 91, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
   77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 64,
   65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
   82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 64, 65, 66, 67, 68,
   69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
   86, 87, 88, 89, 90, 91, 92, 93, 63, 64, 65, 66, 67, 68, 69, 70, 71,
   72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
   89, 90, 91, 92, 93, 94, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
   74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
   91, 92, 93, 94, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
   75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
   92, 93, 94, 95, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
   75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
   92, 93, 94, 95, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
   75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
   92, 93, 94, 95, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
   74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
   91, 92, 93, 94, 95, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
   73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
   90, 91, 92, 93, 94, 95, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
   72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
   89, 90, 91, 92, 93, 94, 95, 96, 61, 62, 63, 64, 65, 66, 67, 68, 69,
   70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
   87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 61, 62, 63, 64, 65, 66, 67,
   68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
   85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 61, 62, 63, 64, 65, 66,
   67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
   84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 62, 63, 64, 65, 66,
   67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
   84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 62, 63, 64, 65, 66,
   67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
   84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 62, 63, 64, 65, 66,
   67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
   84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 62, 63, 64, 65, 66,
   67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
   84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 63, 64, 65, 66, 67, 68,
   69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
   86, 87, 88, 89, 90, 91, 92, 93, 94, 63, 64, 65, 66, 67, 68, 69, 70,
   71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
   88, 89, 90, 91, 92, 93, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
   75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
   92, 93, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 65, 66, 67, 68,
   69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
   86, 87, 88, 89, 90, 91, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
   77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 67, 68, 69,
   70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
   87, 88, 89, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
   83, 84, 85, 86, 87, 88, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
   81, 82, 83, 84, 85, 86, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
   83, 84, 76, 77, 78, 79, 80]))

我想求平均坐标。看起来像 (x,y)。

我该怎么做?

您已经可以使用 python 的内置函数来计算每个元组部分的总和。不要忘记检查 len(points[n]) > 0 以避免被零除异常。

myAvgPoint = (sum(points[0])/len(points[0]),sum(points[1])/len(points[1]))

np.where returns 一个与 2, N 数组同构的元组,您需要在 N 白点上计算平均值,即在第二个轴上,从 0 算起,是 axis=1 --- 最终你的计算只是一个线性

np.average(np.where(image==255), axis=1)

np.average(np.where(图像==255), 轴=1)

这才是真正的解决办法。

我的结果是: [136.6045082 78.33913934]