【问题标题】:Finding the extreme points and draw a line when extreme points is detected找到极值点并在检测到极值点时画一条线
【发布时间】:2020-03-02 14:41:04
【问题描述】:

当检测到极值点时如何绘制垂直线并进行一些测量,例如(英寸、毫米、厘米等):

这是代码的结果:

下面的起点必须在drawContour而不是cv2.rectangle中画一个cv2.circle极值点:

import cv2
import numpy as np

# Load image, grayscale, Gaussian blur, threshold
image = cv2.imread('banana4.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 220, 255, cv2.THRESH_BINARY_INV)[1]

# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
c = max(cnts, key=cv2.contourArea)

# Obtain outer coordinates
left = tuple(c[c[:, :, 0].argmin()][0])
right = tuple(c[c[:, :, 0].argmax()][0])
top = tuple(c[c[:, :, 1].argmin()][0])
bottom = tuple(c[c[:, :, 1].argmax()][0])

# Draw dots onto image
cv2.drawContours(image, [c], -1, (36, 255, 12), 2)
cv2.circle(image, left, 8, (0, 50, 255), -1)
cv2.circle(image, right, 8, (0, 255, 255), -1)
cv2.circle(image, top, 8, (255, 50, 0), -1)
cv2.circle(image, bottom, 8, (255, 255, 0), -1)

print('left: {}'.format(left))
print('right: {}'.format(right))
print('top: {}'.format(top))
print('bottom: {}'.format(bottom))
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

【问题讨论】:

  • 我的意思是当极值点出现时,它会画一条垂直线。
  • 你们都在同一个班吗?类似的问题还有很多。见here,
  • 我只是创建一个帐户,因此我可以创建另一个问题,因为它是有限的。

标签: python opencv image-processing


【解决方案1】:

您在这里有多个问题和问题。

  1. 您应该减小二进制阈值的最小值以消除阴影。

    thresh = cv2.threshold(blur, 180, 255, cv2.THRESH_BINARY_INV)[1]

  2. 您已经有了边界框 top/bottom/left/right,因此您可以使用 cv2.rectangle 绘制一个矩形。

    cv2.Rectangle(image, (left[0], top[1]), (right[0], bottom[1]), (0, 255, 0), 2)

  3. 如果您不知道有关相机或物体本身的任何信息,则无法估计尺寸。

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多