OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

OpenCV | Saving an Image

Saving or writing an image to a file is a basic operation in OpenCV. You might want to save the output after performing some operation on an image, or simply save the frame captured from a webcam. Here's a simple tutorial on how to save an image using OpenCV.

Prerequisites:

  • Install necessary libraries:
pip install opencv-python

Step-by-Step Tutorial:

  • Import necessary libraries:
import cv2
  • Read the Image: Load an image from file. For this tutorial, we'll start by reading an image and then save it with a different name.
image = cv2.imread('path_to_input_image.jpg')
  • Save/Write the Image: To save an image to file, use the imwrite function. The function requires two arguments: the filename (including the desired file extension) and the image object.
cv2.imwrite('path_to_output_image.jpg', image)

Additional Tips:

  • File Format: OpenCV supports various file formats including .jpg, .png, .bmp, etc. The format is inferred from the file extension you provide. For instance, if you want to save as a PNG, you'd use 'output_image.png' as the filename.

  • JPEG Quality: For JPEG images, you can specify the quality of the output image by adding an additional argument:

cv2.imwrite('path_to_output_image.jpg', image, [cv2.IMWRITE_JPEG_QUALITY, 90])

Here, 90 is the quality level, which can range from 0 to 100 (default is 95). Lower numbers will result in higher compression and lower file sizes, but with a potential reduction in image quality.

  • PNG Compression: For PNG images, you can specify the compression level:
cv2.imwrite('path_to_output_image.png', image, [cv2.IMWRITE_PNG_COMPRESSION, 4])

Here, 4 is the compression level which can range from 0 (no compression) to 9. Higher values will produce smaller files but will take more time to write.

Conclusion:

Saving an image in OpenCV is straightforward using the imwrite function. By understanding the additional parameters, you can optimize the balance between output image quality and file size according to your needs.

  1. Save image with OpenCV in Python:

    • Description: Saving an image to a file using OpenCV's imwrite function.
    • Code:
      import cv2
      
      # Read an image
      image = cv2.imread('input_image.jpg')
      
      # Save the image
      cv2.imwrite('output_image.jpg', image)
      
  2. Save image in different formats with OpenCV:

    • Description: Illustrating how to save an image in various formats (e.g., JPEG, PNG) using OpenCV.
    • Code:
      # Save as JPEG
      cv2.imwrite('output_image.jpg', image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
      
      # Save as PNG
      cv2.imwrite('output_image.png', image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
      
  3. OpenCV save image to specific directory:

    • Description: Demonstrating how to save an image to a specific directory.
    • Code:
      import os
      
      output_dir = 'output_images'
      
      # Create the directory if it doesn't exist
      os.makedirs(output_dir, exist_ok=True)
      
      # Save the image to the specified directory
      cv2.imwrite(os.path.join(output_dir, 'output_image.jpg'), image)
      
  4. OpenCV save image with custom filename:

    • Description: Allowing users to specify a custom filename when saving an image.
    • Code:
      custom_filename = 'custom_output.jpg'
      cv2.imwrite(custom_filename, image)
      
  5. Save image with OpenCV and matplotlib:

    • Description: Demonstrating how to save an image using both OpenCV and matplotlib.
    • Code:
      import matplotlib.pyplot as plt
      
      # Display the image using matplotlib
      plt.imshow(image)
      plt.savefig('matplotlib_output.png')