Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 找到极值点并在检测到极值点时绘制一条线_Python_Opencv_Image Processing - Fatal编程技术网

Python 找到极值点并在检测到极值点时绘制一条线

Python 找到极值点并在检测到极值点时绘制一条线,python,opencv,image-processing,Python,Opencv,Image Processing,当检测到极值点时,如何绘制垂直线,并进行一些测量,如(英寸、毫米、厘米等): 这是代码的结果: 以下起点必须在drawContour中绘制一个cv2.circle端点,而不是在cv2.矩形中绘制: import cv2 import numpy as np # Load image, grayscale, Gaussian blur, threshold image = cv2.imread('banana4.jpg') gray = cv2.cvtColor(image, cv2.COL

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

这是代码的结果:

以下起点必须在drawContour中绘制一个cv2.circle端点,而不是在
cv2.矩形中绘制:

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()


这里有很多问题

  • 为了去除阴影,应该减小二进制阈值的最小值

    thresh=cv2.threshold(模糊,180255,cv2.thresh\u BINARY\u INV)[1]

  • 您已经具有上/下/左/右边界框,因此可以使用绘制矩形

    cv2.矩形(图像,(左[0],上[1]),(右[0],下[1]),(0,255,0),2)

  • 如果您不知道有关相机或对象本身的任何信息,则无法估计大小


  • 这回答了你的问题吗?我的意思是,当极限点出现时,它会画一条垂直线。你们都在同一个班吗?有这么多类似的问题。看,我只是创建一个帐户,这样我就可以创建另一个问题,因为它是有限的。