1

I am brand new to coding and need lots of help. I am trying to create a code that can do image processing for me. I keep getting errors and I have no idea how to fix it.

I am using an online code as my baseline that is this:

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

And I am getting this as an output:

runfile('/home/whoiuser/Desktop/untitled7.py', wdir='/home/whoiuser/Desktop')
usage: untitled7.py [-h] -i IMAGE
untitled7.py: error: the following arguments are required: -i/--image
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

/home/whoiuser/anaconda2/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3304: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

When I type in %tb I get this output:

%tb
Traceback (most recent call last):

  File "<ipython-input-1-dab57867b1e1>", line 1, in <module>
    runfile('/home/whoiuser/Desktop/untitled7.py', wdir='/home/whoiuser/Desktop')

  File "/home/whoiuser/anaconda2/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 826, in runfile
    execfile(filename, namespace)

  File "/home/whoiuser/anaconda2/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/whoiuser/Desktop/untitled7.py", line 42, in <module>
    args = vars(ap.parse_args())

  File "/home/whoiuser/anaconda2/lib/python3.6/argparse.py", line 1734, in parse_args
    args, argv = self.parse_known_args(args, namespace)

  File "/home/whoiuser/anaconda2/lib/python3.6/argparse.py", line 1766, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)

  File "/home/whoiuser/anaconda2/lib/python3.6/argparse.py", line 2001, in _parse_known_args
    ', '.join(required_actions))

  File "/home/whoiuser/anaconda2/lib/python3.6/argparse.py", line 2393, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

  File "/home/whoiuser/anaconda2/lib/python3.6/argparse.py", line 2380, in exit
    _sys.exit(status)

SystemExit: 2
bad_coder
  • 11,289
  • 20
  • 44
  • 72
mlearner
  • 11
  • 1
  • 1
  • 2
  • 2
    This needs serious formatting adjustments in order to be readable. Can you please fix it for other people that can answer your question? Thank you in advance – Arnav Poddar Jun 18 '19 at 14:30

1 Answers1

0

The problem that you should solve is: the following arguments are required: -i/--image

The argument parser (argparse) is printing it and call 'sys.exit(2)'

So you need to supply the image to the program.

In your code there is a declaration of required argument:

ap.add_argument("-i", "--image", required = True, help = "Path to the image.")
balderman
  • 22,927
  • 7
  • 34
  • 52