我是编码的新手,需要很多帮助。我正在尝试创建一个可以为我做图像处理的代码。我不断收到错误,而且不知道如何解决。
我将在线代码用作基线:
import numpy as np
import argparse
import cv2
def fill_holes(imInput, threshold):
# Threshold.
th, thImg = cv2.threshold(imInput, threshold, 255, cv2.THRESH_BINARY_INV)
# Copy the thresholded image.
imFloodfill = thImg.copy()
# Get the mask.
h, w = thImg.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
# Floodfill from point (0, 0).
cv2.floodFill(imFloodfill, mask, (0,0), 255)
# Invert the floodfilled image.
imFloodfillInv = cv2.bitwise_not(imFloodfill)
# Combine the two images.
imOut = thImg | imFloodfillInv
return imOut
if __name__ == "__main__":
# Extract arguments from the command line.
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image.")
args = vars(ap.parse_args())
# Load the image.
image = cv2.imread(args["image"])
cv2.imshow("Original image", image)
# Convert the image into grayscale image.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
cv2.imshow("Blurred", blurred)
# Fille the "holes" on the image.
filled = fill_holes(blurred, 220)
cv2.imshow("Filled", filled)
# Find circles by the Hough transfermation.
circles = cv2.HoughCircles(filled, cv2.HOUGH_GRADIENT, 1, 20, param1 = 25, param2 = 10, minRadius = 0, maxRadius = 20)
# Draw circles on the original image.
if circles is not None:
for i in range(circles.shape[1]):
c = circles[0, i]
cv2.circle( image, (c[0], c[1]), c[2], (0, 255, 0), 2)
print("i = %d, r = %f" % (i, c[2]))
cv2.imshow("Marked", image)
else:
print("circle is None")
# Block the execution.
cv2.waitKey(0)
cv2.destroyAllWindows()
我得到这个作为输出:
runfile('/ home / whoiuser / Desktop / untitled7.py',wdir ='/ home / whoiuser / Desktop') 用法:untitled7.py [-h] -i图片 untitled7.py:错误:需要以下参数:-i /-image 发生异常,请使用%tb查看完整的追溯。
SystemExit:2
/home/whoiuser/anaconda2/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3304:用户警告:要退出:请使用“退出”,“退出”或Ctrl-D。 警告(“退出:使用'退出','退出'或Ctrl-D。”,堆栈级别= 1)
当我输入%tb
时,得到以下输出:
%tb
回溯(最近通话最近一次):
文件“”,第1行,在 运行文件('/home/whoiuser/Desktop/untitled7.py',wdir ='/ home / whoiuser / Desktop')
Runfile中的文件“ /home/whoiuser/anaconda2/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py”,行826 execfile(文件名,命名空间)
exec文件中的文件“ /home/whoiuser/anaconda2/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py”,第110行 exec(compile(f.read(),文件名,'exec'),命名空间)
文件“ /home/whoiuser/Desktop/untitled7.py”,第42行,在 args = vars(ap.parse_args())
文件“ /home/whoiuser/anaconda2/lib/python3.6/argparse.py”,行1734,在parse_args中 args,argv = self.parse_known_args(args,名称空间)
文件“ /home/whoiuser/anaconda2/lib/python3.6/argparse.py”,行1766,位于parse_known_args中 名称空间,args = self._parse_known_args(参数,命名空间)
文件“ /home/whoiuser/anaconda2/lib/python3.6/argparse.py”,第2001行,位于_parse_known_args中 ','.join(required_actions))
文件“ /home/whoiuser/anaconda2/lib/python3.6/argparse.py”,第2393行,错误 self.exit(2,_('%(prog)s:error:%(message)s \ n')%args)
文件“ /home/whoiuser/anaconda2/lib/python3.6/argparse.py”,行2380,在出口处 _sys.exit(状态)
SystemExit:2
答案 0 :(得分:0)
您应该解决的问题是:以下参数是必需的:-i /-image
参数解析器(argparse)正在打印它并调用'sys.exit(2)'
因此您需要将图像提供给程序。
您的代码中有一个必需参数的声明:
ap.add_argument("-i", "--image", required = True, help = "Path to the image.")