gpt4 book ai didi

python - 在python(OpenCV)中查找对象的大小?

转载 作者:行者123 更新时间:2023-12-02 17:20:50 28 4
gpt4 key购买 nike

如何使用python(opencv)获得明亮部分的高度和宽度?这些图片有2000个,最终目的是使表具有长度和宽度值

最佳答案

这是原始方法。转换为灰度并检查哪些点的值大于某些“明亮”的颜色,即。 21给出数组True/False-使用.any(axis=0)可以将每一行减少为单个值,使用.any(axis=1)可以将每一列减少为单个值,然后使用sum()您可以计算任何行或列中有多少True(因为True/False转换为1/0)

import cv2

img = cv2.imread('image.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#print(img)

print('height, width, color:', img.shape)

#cv2.imshow('image', img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

print('width:', sum((img > 21).any(axis=0)))
print('height:', sum((img > 21).any(axis=1)))

为了你的形象,它给了我
width: 19
height: 27

对于我的形象(如下),它给了我
width: 23
height: 128

enter image description here

编辑:版本,变化不大。

我将 mask = (img > 21)设置为
  • 计算大小
  • 创建黑白图像,更好地显示哪些点是
    用于计算大小。

  • BTW:代码 ~mask反转掩码(将 True转换为 FalseFalse转换为 True)。它也可以用于反转图像- ~img-为RGB,灰度或黑白创建负片。

    码:
    import cv2

    for filename in ['image-1.png', 'image-2.png']:

    img = cv2.imread(filename)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    print('height, width, color:', img.shape)

    mask = (img > 21)

    # display size
    print(' width:', sum( mask.any(axis=0) ))
    print('height:', sum( mask.any(axis=1) ))

    # create Black&White version
    img[ mask ] = 255 # set white
    img[ ~mask ] = 0 # set black

    # display Black&White version
    cv2.imshow('image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # write Black&White version
    cv2.imwrite('BW-' + filename, img)

    enter image description here ---> enter image description here

    enter image description here ---> enter image description here

    编辑:使用 cv2.boundingRect()而不是 sum(mask.any())的结果相同-但仍需要 img[ mask ] = 255来创建黑白图像。
    import cv2

    for filename in ['image-1.png', 'image-2.png']:
    print('filename:', filename)

    img = cv2.imread(filename)
    #print('height, width, color:', img.shape)

    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #print('height, width, color:', img.shape)

    mask = (img > 21)

    # create Black&White version
    img[ mask ] = 255 # set white
    img[ ~mask ] = 0 # set black

    x, y, width, height = cv2.boundingRect(img)

    # display size
    print(' width:', width)
    print('height:', height)

    # display Black&White version
    #cv2.imshow('image', img)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()

    # write Black&White version
    #cv2.imwrite('BW-' + filename, img)

    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html#bounding-rectangle

    编辑:使用 cv2.boundingRect()cv2.threshold()的结果相同-因此不需要 mask
    import cv2

    for filename in ['image-1.png', 'image-2.png']:
    print('filename:', filename)

    img = cv2.imread(filename)
    #print('height, width, color:', img.shape)

    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #print('height, width, color:', img.shape)

    ret, img = cv2.threshold(img, 21, 255, cv2.THRESH_BINARY) # the same 21 as in `mask = (img > 21)`
    x, y, width, height = cv2.boundingRect(img)

    # display size
    print(' width:', width)
    print('height:', height)

    # display Black&White version
    cv2.imshow('image', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # write Black&White version
    #cv2.imwrite('BW-' + filename, img)

    https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html

    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html

    https://www.learnopencv.com/opencv-threshold-python-cpp/

    https://www.geeksforgeeks.org/python-thresholding-techniques-using-opencv-set-1-simple-thresholding/

    关于python - 在python(OpenCV)中查找对象的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62225220/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com