Skip to content

A foundational Python project demonstrating the core workflow of reading, writing, and displaying images using OpenCV. This notebook highlights the crucial BGR to RGB color space conversion required for accurate visualization with Matplotlib in a Jupyter environment.

License

Notifications You must be signed in to change notification settings

imehranasgari/OpenCV_Simple_Read_Write

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foundational Image Handling with OpenCV: Read, Write, and Display

This project serves as a practical, hands-on demonstration of the core image handling capabilities within the OpenCV library. It is designed to showcase the essential workflow of loading an image, handling color space conversions for accurate visualization, and saving the image back to disk—a fundamental skill set for any computer vision task.

Problem Statement and Goal of Project

In computer vision, the first step in almost any pipeline is to correctly load and represent image data. Without this, any subsequent analysis or model training will be flawed. The goal of this project was to implement a clean, commented notebook that clearly demonstrates:

  1. Reading an image from a local file into a NumPy array.
  2. Visualizing the image correctly within a Jupyter environment.
  3. Writing the image data back to a new file on disk.

This exercise reinforces my understanding of how OpenCV handles image data at a fundamental level.

Solution Approach

My approach was to create a straightforward, step-by-step process within a Jupyter Notebook.

  1. Image Loading: I used the cv2.imread() function to load the source image. This function efficiently reads the image and converts it into a NumPy array, which is the standard data structure for any further processing.
  2. Color Space Correction & Visualization: I identified that OpenCV loads images in a BGR (Blue-Green-Red) channel order by default. However, visualization libraries like Matplotlib expect an RGB order. To ensure the image was displayed correctly in the notebook, I used cv2.cvtColor() to convert the image from BGR to RGB before plotting it with matplotlib.pyplot. This step is crucial and demonstrates an attention to detail in data handling.
  3. Image Writing: Finally, I used the cv2.imwrite() function to save the original image data (in BGR format) to a new file, demonstrating the completion of the read-process-save cycle.

Technologies & Libraries

  • Python 3
  • OpenCV: The primary library for all image reading, writing, and color conversion tasks.
  • NumPy: Used for the underlying multi-dimensional array data structure that holds the image pixels.
  • Matplotlib: Utilized for displaying the image accurately within the Jupyter Notebook.
  • Jupyter Notebook: The interactive environment used for developing and documenting the code.

Description about Dataset

This project uses a single sample image (input.jpg) to demonstrate the read/write functionality. The specific content of the image is secondary to the project's main objective, which is to showcase the technical process of image handling.

Installation & Execution Guide

  1. Clone the repository:
    git clone <your-repository-url>
    cd <your-repository-name>
  2. Install the required libraries:
    pip install -r requirements.txt
    or
    pip install opencv-python numpy matplotlib
  3. Prepare the input: Place your input image in an images subfolder and name it input.jpg.
  4. Run the notebook: Launch Jupyter Notebook or Jupyter Lab and open the simple_read_write.ipynb file. Execute the cells in order to see the process.

Key Results / Performance

The project successfully achieves its objectives. The notebook correctly:

  • Loads the input.jpg image file.
  • Displays the image with accurate colors inside the notebook by handling the BGR to RGB conversion.
  • Saves the image data to a new file named output.jpg in the images directory.

Sample Output

The following screenshot shows the core logic for reading and writing the image file using OpenCV.

The notebook also produces visual outputs, plotting the image using Matplotlib to verify that it has been loaded correctly.

Additional Learnings / Reflections

While this project covers a basic operation, it was a valuable exercise in reinforcing a critical concept in computer vision: data representation. The discrepancy between OpenCV's BGR default and Matplotlib's RGB standard is a common source of bugs. Proactively identifying and correcting this demonstrates a thorough understanding of the tools and a commitment to data integrity before more complex processing begins. This foundational knowledge is essential for building reliable AI and deep learning pipelines.


💡 Some interactive outputs (e.g., plots, widgets) may not display correctly on GitHub. If so, please view this notebook via nbviewer.org for full rendering.


🙏 Acknowledgments

This project represents my initial steps into the practical application of computer vision. The foundational knowledge and guidance for this work were derived from the outstanding OpenCV course taught by Alireza Akhavanpour on the Maktabkhooneh platform. His ability to deconstruct complex topics into clear, actionable steps was instrumental in the successful implementation of this project.


👤 Author

Mehran Asgari


📄 License

This project is licensed under the Apache 2.0 License – see the LICENSE file for details.

About

A foundational Python project demonstrating the core workflow of reading, writing, and displaying images using OpenCV. This notebook highlights the crucial BGR to RGB color space conversion required for accurate visualization with Matplotlib in a Jupyter environment.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published