title: For loop function for image processing and saving causing an error - Expected Ptr<cv::UMat> for argument '%s'
body: <p>I'm working on lane detection on images. I a have a function that takes a path to an image and returns an image with lanes detected. I need to write another function thae takes a path to a directory with multiple images and output path as input, uses the first function to process an image and then save it in an output directory.</p>
  <p>This is the code from the beginning, where I define helper functions if you want to reproduce it:</p>
  <pre><code>import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  import numpy as np
  import cv2
  %matplotlib inline
  
  def to_gray(image):
      gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
      return gray
  
  def blur_gray(gray):
      kernel_size = 5
      blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
      return blur_gray
  
  def Canny(blur_gray):
      low_threshold = 100
      high_threshold = 200
      edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
      return edges
  
  def masked_edges(image, edges):
      mask = np.zeros_like(edges)   
      ignore_mask_color = 255  
      imshape = image.shape
      vertices = np.array([[(130,imshape[0]),(420, 325), (540,325), (850,imshape[0])]], dtype=np.int32)
      cv2.fillPoly(mask, vertices, ignore_mask_color)
      masked_edges = cv2.bitwise_and(edges, mask)
      return masked_edges
  
  def lines (masked_edges, image):
      # Define the Hough transform parameters
      # Make a blank the same size as our image to draw on
      rho = 1 # distance resolution in pixels of the Hough grid
      theta = np.pi/180 # angular resolution in radians of the Hough grid
      threshold = 1     # minimum number of votes (intersections in Hough grid cell)
      min_line_length = 3 #minimum number of pixels making up a line
      max_line_gap = 4    # maximum gap in pixels between connectable line segments
      line_image = np.copy(image)*0 # creating a blank to draw lines on
  
      # Run Hough on edge detected image
      # Output &quot;lines&quot; is an array containing endpoints of detected line segments
      lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                                  min_line_length, max_line_gap)
      return lines
  
  def draw_lines(lines, image, edges):
      line_image = np.copy(image)*0 # creating a blank to draw lines o
      # Iterate over the output &quot;lines&quot; and draw lines on a blank image
      for line in lines:
          for x1,y1,x2,y2 in line:
              cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
  
      # Create a &quot;color&quot; binary image to combine with line image
      color_edges = np.dstack((edges, edges, edges)) 
  
      # Draw the lines on the edge image
      lines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) 
      pic_lanes = plt.imshow(lines_edges)
      return pic_lanes
  </code></pre>
  <p>So this is the function that incorporates multiple helper functions from above and returns a single image:</p>
  <pre><code>def image_pipeline(image_path):
    image = mpimg.imread(image_path)
    gray = to_gray(image)
    to_blur_gray = blur_gray(gray)
    edges = Canny(to_blur_gray)
    masked = masked_edges(image, edges)
    line = lines(masked, image)
    pic_lanes = draw_lines(line, image, edges)
    return pic_lanes
  </code></pre>
  <p><a href="https://i.stack.imgur.com/zPO4x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zPO4x.png" alt="A picture returned by the function above" /></a></p>
  <p>Then I want to use the above function within a loop function that would do the same for all the images from an input directory and save them in an output directory.</p>
  <pre><code>def video_loop(outPath, path):
    image_no = 1
    for image_path in os.listdir(path):
          
          # create the full input path and read the file
          input_path = os.path.join(path, image_path)
          pic_lanes = image_pipeline(input_path)
          fullpath = os.path.join(outPath, image_path)
          name = fullpath + '.jpg'
          cv2.imwrite(name, pic_lanes)
          os.chdir(fullpath)
          image_no += 1
  if __name__ == '__video_loop__':
      video_loop()
  </code></pre>
  <pre><code>out= '/content/CarND-LaneLines-P1/Solid White Frames Canny'
  path1 = '/content/CarND-LaneLines-P1/Frames Solid White Right'
  video_loop(out, path1)
  </code></pre>
  <p>Unfortunately I get an error together with one returned image:</p>
  <pre><code>TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-109-10dea7ed1446&gt; in &lt;module&gt;()
        1 out= '/content/CarND-LaneLines-P1/Solid White Frames Canny'
        2 path1 = '/content/CarND-LaneLines-P1/Frames Solid White Right'
  ----&gt; 3 video_loop(out, path1)
  
  &lt;ipython-input-108-a52f4438bc0d&gt; in video_loop(outPath, path)
        8         fullpath = os.path.join(outPath, image_path)
        9         name = fullpath + '.jpg'
  ---&gt; 10         cv2.imwrite(name, pic_lanes)
       11         os.chdir(fullpath)
       12         image_no += 1
  
  TypeError: Expected Ptr&lt;cv::UMat&gt; for argument '%s'
  </code></pre>
  <p>I have searched for the meaning of this error and someone suggested that the <code>cv2.imwrite()</code> doesn't get a valid argument (the picture doesn't exist) but I'm not sure how to fix this.</p>
  <p>EDIT:
  I also tried something simpler like this:</p>
  <pre><code>count=0
  for filename in os.listdir('/content/CarND-LaneLines-P1/Frames Solid White Right'):
    detected_lanes = image_pipeline(filename)
    detected_lanes = cv2.imread(detected_lanes)
    cv2.imwrite(filename, detected_lanes)
    os.chdir('/content/CarND-LaneLines-P1/Frames Solid White Right/Canny')
    count =+1
  </code></pre>
  <p>But i'm gettin a different error here:</p>
  <pre><code>SystemError                               Traceback (most recent call last)
  &lt;ipython-input-15-1d3fff5ab2bb&gt; in &lt;module&gt;()
        2 for filename in os.listdir('/content/CarND-LaneLines-P1/Frames Solid White Right'):
        3   detected_lanes = image_pipeline(filename)
  ----&gt; 4   detected_lanes = cv2.imread(detected_lanes)
        5   cv2.imwrite(filename, detected_lanes)
        6   os.chdir('/content/CarND-LaneLines-P1/Frames Solid White Right/Canny')
  
  SystemError: &lt;built-in function imread&gt; returned NULL without setting an error
  </code></pre>
  <p>I don't know how to approach this. <strong>If you want to run this code, just use the code provided and then use <code>image_pipeline</code> and pass it any image.</strong></p>
  <p>Any chance you could help me out here?</p>