python - 边距

标签 python opencv distance

我想得到这张照片的距离(用红色绘制)。因此,这只是从外部检测到的边缘开始的一条直线。我是openCV中整个图像处理人员的新手,无法在线找到正确的解决方案,所以这就是为什么我在这里提出问题。我希望有人能帮助我。
enter image description here
enter image description here
enter image description here

最佳答案

这是做我在评论中建议的方法。首先,对各列求和,以得到与原始宽度相同的图像,但只有一行。然后,寻找第一列和最后一列的总和不为零,即第一列和最后一列的内容并非全为黑色。我修剪了您的图像-请不要张贴屏幕截图,否则算法会找到窗框!
enter image description here

import cv2
import numpy as np

# Load image as greyscale
im = cv2.imread('weld.png', cv2.IMREAD_GRAYSCALE)

# Sum down the columns
columnTotals = np.sum(im, axis=0)

# Now look for non-zero (non-black) entries
nz = np.nonzero(columnTotals)

# Now get left and right edges of white parts of image
left, right = nz[0][0], nz[0][-1]              # (53,271)
这是对列进行求和时代码的可视化表示:
enter image description herecolumnTotals看起来像这样:
array([    0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,   510,
       18870, 19890, 19890, 20400, 19890, 19890, 19125,  3060,  6120,
        6630,  6375,  6630,  6375,  6120,  5610,  5100,  5100,  5355,
        5355,  7140,  7395,  7650,  7395,  7395,  7905,  7650,  4335,
        4335,  4590,  5610,  6375,  6120,  6885,  6630,  6120,  5610,
        6375,  5865,  6120,  5100,  5100,  5355,  5865,  5865,  6120,
        5865,  5355,  5100,  5355,  5100,  4845,  4590,  4590,  4590,
        4590,  4590,  4590,  4590,  4845,  5355,  5865,  5865,  6375,
        5610,  5100,  5100,  4590,  4335,  3570,  3060,  2805,  2805,
        2550,  2295,  2805,  3570,  3570,  4080,  4080,  4335,  3825,
        3570,  3570,  3570,  3570,  3570,  3570,  3570,  3570,  3570,
        3825,  3825,  3825,  3825,  3570,  3570,  3570,  3570,  3570,
        3570,  3570,  3570,  3570,  3570,  3570,  3570,  3570,  3570,
        3570,  3570,  3570,  3570,  3570,  3570,  3315,  3315,  3315,
        3315,  3570,  3315,  3315,  3570,  3570,  3570,  3570,  3570,
        3570,  3570,  3570,  3570,  3825,  3570,  3570,  3315,  2805,
        2805,  2805,  2805,  2805,  3060,  3060,  3060,  3060,  3060,
        3060,  3060,  3060,  2805,  2805,  2295,  2295,  2550,  2805,
        2805,  3060,  3060,  3060,  2805,  2805,  2295,  1785,  1785,
        1785,  1785,  1785,  2040,  2040,  2295,  2550,  2295,  2295,
        2040,  2040,  1785,  1785,  1785,  2040,  1785,  2040,  1785,
        1785,  2295,  2295,  2295,  2295,  2040,  1785,  1785,  1785,
        1785,  1785,  1785,  2295,  3060,  3570,  4335,  4335,  4590,
        4845,  5100,  5355,  3570,  3570,  3060,  3060,  2040,  1785,
        2040,  2040,  2295,  3315, 11220, 11730, 11730, 11475, 10965,
        9945,  8670,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0,     0,     0,
           0,     0,     0,     0,     0,     0,     0], dtype=uint64)
nz看起来像这样:
(array([ 53,  54,  55,  56,  57,  58,  59,  60,  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,  97,  98,  99, 100, 101, 102, 103, 104,
        105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
        118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
        131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
        144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
        157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
        170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
        183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,
        196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
        209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,
        222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,
        235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
        248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260,
        261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271]),)

关于python - 边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63794118/

相关文章:

python - 使用 pybind11,如何将我的代码拆分为多个模块/文件?

python - 如何使用 Python 按字母数字顺序按值对字典进行排序?

python - 使用 Python 发送多个 ping

c# - c++调用opencv相关函数并打包成dll,c#调用不了

python - 如何改进Python中的距离函数

python - 使用 Apache 和多个 Django 站点的奇怪、不一致的行为

python - waitKey(0) 和 waitKey(1) 的输出差异

java - Android OpenCV 创建矩形

r - 更改 Graphics::stripchart 中的列间距

optimization - 找到最小距离