简体   繁体   English

在 python (OpenCV) 中查找 object 的大小?

[英]Finding size of the object in python (OpenCV)?

How to get height and width of bright part using python (opencv)?如何使用 python (opencv) 获得明亮部分的高度和宽度? Having 2000 of these picture and end goal is to make table with length and width values拥有 2000 张这些图片,最终目标是制作具有长度和宽度值的表格

It is primitive method.这是原始的方法。 Convert to grayscale and check which points have value bigger then some "bright" color ie.转换为灰度并检查哪些点的值大于一些“明亮”颜色,即。 21 and it gives array True/False - using .any(axis=0) you can reduce every row to single value, using .any(axis=1) you can reduce every column to single value and then using sum() you can count how many True was in any row or column (because True/False is converted to 1/0 ) 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)))

For your image it gives me对于你的形象,它给了我

width: 19
height: 27

For my image (below) it gives me对于我的图像(下),它给了我

width: 23
height: 128

在此处输入图像描述


EDIT: Version with small change.编辑:小改动的版本。

I set mask = (img > 21) to我将mask = (img > 21)设置为

  • calculate size计算大小

  • create Black&White image which better shows which points are used to calculate size.创建黑白图像,更好地显示用于计算大小的点。

BTW: code ~mask inverts mask (convert True to False and False to True ).顺便说一句:代码~mask反转掩码(将True转换为False并将False转换为True )。 It can be used also to invert image - ~img - to create negative for RGB, Grayscale or B&W.它还可用于反转图像 - ~img - 为 RGB、灰度或黑白创建负片。

Code:代码:

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)

在此处输入图像描述 ---> ---> 在此处输入图像描述

在此处输入图像描述 ---> ---> 在此处输入图像描述


EDIT: The same result using cv2.boundingRect() instead of sum(mask.any()) - but it still needs img[ mask ] = 255 to create Black&White image.编辑:使用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 https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html#bounding-rectangle


EDIT: The same result using cv2.boundingRect() and cv2.threshold() - so it doesn't need mask编辑:使用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://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://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.learnopencv.com/opencv-threshold-python-cpp/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM