0
0
Computer Visionml~15 mins

Displaying images (cv2.imshow, matplotlib) in Computer Vision - Deep Dive

Choose your learning style9 modes available
Overview - Displaying images (cv2.imshow, matplotlib)
What is it?
Displaying images means showing pictures on your computer screen so you can see them. In computer vision, we often work with images as data, and tools like cv2.imshow and matplotlib help us open windows or plots to view these images. cv2.imshow is part of OpenCV, a library focused on image processing, while matplotlib is a plotting library that can also show images inside graphs. Both let you see what your program is working with, which is important for understanding and debugging.
Why it matters
Without being able to see images, it would be very hard to know if your computer vision program is working correctly. Imagine trying to fix a photo filter without seeing the photo! Displaying images helps you check your work, understand how your code changes pictures, and share results with others. It makes the invisible data visible, turning numbers into pictures you can understand.
Where it fits
Before learning to display images, you should know how to read and store images as data arrays. After this, you can learn how to process images, like changing colors or detecting edges, and then display the results. Later, you might explore interactive image tools or build apps that show images dynamically.
Mental Model
Core Idea
Displaying images is like opening a window to look at the picture data your program is working with, using tools that create that window or plot for you.
Think of it like...
It's like holding a photo in your hand to see it clearly, instead of just hearing someone describe it. The tools are your hands and the window where you hold the photo.
Image Data (array) ──▶ Display Tool ──▶ Screen Window

[Image Array] → [cv2.imshow or matplotlib.imshow] → [Window showing image]
Build-Up - 7 Steps
1
FoundationUnderstanding image data arrays
🤔
Concept: Images are stored as arrays of numbers representing pixels and colors.
An image is like a grid of tiny dots called pixels. Each pixel has color values, often in red, green, and blue (RGB). In code, this grid is stored as a 2D or 3D array of numbers. For example, a 100x100 pixel image with RGB colors is a 100x100x3 array.
Result
You can access and change any pixel's color by indexing into this array.
Knowing that images are just arrays helps you understand how display tools read and show these numbers as pictures.
2
FoundationReading images with OpenCV and matplotlib
🤔
Concept: Before displaying, you must load images into arrays using libraries like OpenCV or matplotlib.
OpenCV uses cv2.imread('path') to read an image file into an array. Matplotlib can use plt.imread('path') for the same. Note OpenCV reads images in BGR color order, while matplotlib uses RGB.
Result
You get an array representing the image pixels, ready to display or process.
Understanding how images load prepares you to handle color differences when displaying.
3
IntermediateDisplaying images with cv2.imshow
🤔Before reading on: Do you think cv2.imshow automatically waits for you to close the window, or does it close immediately? Commit to your answer.
Concept: cv2.imshow opens a new window to show the image array, but you must tell it to wait for a key press to keep the window open.
Use cv2.imshow('window name', image_array) to open the window. Then use cv2.waitKey(0) to pause the program until you press a key. Finally, cv2.destroyAllWindows() closes the window.
Result
An image window appears and stays open until you press a key, then closes cleanly.
Knowing that cv2.imshow needs waitKey and destroyAllWindows prevents windows from flashing and disappearing instantly.
4
IntermediateDisplaying images with matplotlib.pyplot.imshow
🤔Before reading on: Does matplotlib.pyplot.imshow show the image immediately, or do you need an extra command to display it? Commit to your answer.
Concept: matplotlib.pyplot.imshow shows images inside a plot, but you must call plt.show() to display the plot window.
Use plt.imshow(image_array) to prepare the image plot. Then call plt.axis('off') to hide axes if you want a clean image. Finally, plt.show() opens the window with the image.
Result
A plot window opens showing the image with optional axes hidden.
Understanding the need for plt.show() helps avoid confusion when images don't appear immediately.
5
IntermediateHandling color differences between OpenCV and matplotlib
🤔Before reading on: If you display an OpenCV image with matplotlib without changes, will the colors look correct or swapped? Commit to your answer.
Concept: OpenCV uses BGR color order, but matplotlib expects RGB, so colors can look wrong unless converted.
To fix colors, convert BGR to RGB using cv2.cvtColor(image, cv2.COLOR_BGR2RGB) before plt.imshow. Without this, reds and blues swap places.
Result
Images display with correct colors in matplotlib after conversion.
Knowing color order differences prevents confusing color mistakes when switching display tools.
6
AdvancedDisplaying multiple images side-by-side with matplotlib
🤔Before reading on: Can you display multiple images in one window using cv2.imshow? Commit to your answer.
Concept: matplotlib allows creating multiple subplots in one figure to show images side-by-side, which cv2.imshow does not support directly.
Use plt.subplot(rows, cols, index) to create grid positions, then plt.imshow for each image. Call plt.show() once to display all images in one window.
Result
A single window shows multiple images arranged neatly.
Understanding subplotting helps compare images easily without opening many windows.
7
ExpertManaging display windows and event loops in production
🤔Before reading on: Do you think cv2.imshow and matplotlib can be used interchangeably in real-time video display? Commit to your answer.
Concept: cv2.imshow is better for real-time image/video display with fast event handling, while matplotlib is slower and suited for static images or analysis plots.
In real-time apps, cv2.imshow with cv2.waitKey(delay) handles frame updates and user input efficiently. Matplotlib's event loop is slower and blocks code execution, making it unsuitable for live video.
Result
Choosing the right display method improves performance and user experience in real applications.
Knowing the strengths and limits of each display tool guides you to build responsive, efficient computer vision systems.
Under the Hood
cv2.imshow creates a native OS window and sends the image pixel data to it. It relies on an event loop controlled by cv2.waitKey to keep the window responsive and open. Matplotlib builds a figure canvas and renders the image as part of a plot using its backend (like Tkinter or Qt). plt.show() starts the GUI event loop to display and manage the window. Both convert the numeric arrays into visual pixels but use different underlying GUI frameworks and event handling.
Why designed this way?
OpenCV focuses on speed and real-time processing, so cv2.imshow is minimal and fast, requiring explicit waitKey calls to control window life. Matplotlib is designed for flexible plotting and analysis, so it integrates image display into its plotting system with more overhead but richer features. This separation allows each tool to excel in its domain.
Image Array
   │
   ├─▶ cv2.imshow ──▶ OS Window ──▶ User sees image
   │                   │
   │                   └─ cv2.waitKey controls window events
   │
   └─▶ matplotlib.imshow ──▶ Plot Canvas ──▶ plt.show() starts GUI loop ──▶ User sees image in plot
Myth Busters - 4 Common Misconceptions
Quick: Does cv2.imshow automatically keep the window open until you close it? Commit yes or no.
Common Belief:cv2.imshow shows the image and keeps the window open by itself until you close it manually.
Tap to reveal reality
Reality:cv2.imshow only opens the window briefly; you must call cv2.waitKey to pause and keep the window open.
Why it matters:Without waitKey, the window closes immediately, making it seem like the image never appeared.
Quick: If you display an OpenCV image with matplotlib without changes, will the colors look correct? Commit yes or no.
Common Belief:You can display OpenCV images directly with matplotlib and the colors will look right.
Tap to reveal reality
Reality:OpenCV uses BGR color order, so matplotlib shows swapped colors unless you convert to RGB first.
Why it matters:Incorrect colors can mislead analysis and debugging, causing wrong conclusions.
Quick: Can you display multiple images in one window using cv2.imshow? Commit yes or no.
Common Belief:cv2.imshow supports showing multiple images in one window like matplotlib subplots.
Tap to reveal reality
Reality:cv2.imshow can only show one image per window; to show multiple images, you must create separate windows or combine images manually.
Why it matters:Expecting multi-image display in one window with cv2.imshow leads to inefficient code and cluttered windows.
Quick: Is matplotlib suitable for real-time video display? Commit yes or no.
Common Belief:Matplotlib is fine for displaying live video frames in real-time applications.
Tap to reveal reality
Reality:Matplotlib is too slow and blocking for real-time video; cv2.imshow is preferred for live video display.
Why it matters:Using matplotlib for video causes lag and poor user experience in real-time systems.
Expert Zone
1
cv2.imshow windows are managed by the OS and can behave differently across platforms, requiring platform-specific tweaks for consistent behavior.
2
Matplotlib's image display integrates with its plotting system, allowing overlays like titles, colorbars, and annotations, which cv2.imshow cannot do.
3
Using cv2.waitKey with small delays (like 1 ms) enables smooth video playback and user input handling simultaneously.
When NOT to use
Avoid using cv2.imshow for complex image analysis plots or when you need to combine images with charts and annotations; use matplotlib instead. Conversely, avoid matplotlib for real-time video or interactive image processing where speed and responsiveness are critical; use OpenCV's display functions.
Production Patterns
In production, cv2.imshow is used for debugging and quick visualization during development, especially for video streams. Matplotlib is used for generating reports, static image analysis, and combining images with other plots. For GUI applications, developers often integrate OpenCV display with frameworks like Qt or use specialized visualization tools.
Connections
Image Processing Pipelines
Displaying images is a key step to verify each stage in an image processing pipeline.
Seeing intermediate results helps catch errors early and understand how each processing step transforms the image.
Human Visual Perception
Displaying images connects raw pixel data to how humans perceive color and detail.
Understanding display tools helps bridge the gap between numeric data and human interpretation, crucial for designing user-friendly vision systems.
Graphical User Interfaces (GUIs)
Image display functions rely on GUI event loops and window management.
Knowing how display tools interact with GUI frameworks aids in building interactive vision applications with responsive image windows.
Common Pitfalls
#1Image window closes immediately after opening.
Wrong approach:cv2.imshow('image', img) cv2.destroyAllWindows()
Correct approach:cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
Root cause:Not calling cv2.waitKey means the program does not pause, so the window closes instantly.
#2Colors look strange or swapped when displaying OpenCV images with matplotlib.
Wrong approach:plt.imshow(img) plt.show() # img loaded with cv2.imread
Correct approach:img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img_rgb) plt.show()
Root cause:OpenCV uses BGR color order, but matplotlib expects RGB, causing color mismatch.
#3Trying to show multiple images in one window using cv2.imshow.
Wrong approach:cv2.imshow('images', img1) cv2.imshow('images', img2)
Correct approach:cv2.imshow('image1', img1) cv2.imshow('image2', img2)
Root cause:cv2.imshow creates separate windows per call; it cannot combine images in one window automatically.
Key Takeaways
Displaying images turns raw pixel data into visible pictures, essential for understanding and debugging computer vision tasks.
cv2.imshow is fast and suited for real-time image display but requires explicit window and event management with waitKey and destroyAllWindows.
matplotlib.imshow integrates image display into plotting, allowing rich annotations and multiple images in one window but is slower and less suited for live video.
OpenCV uses BGR color order, so converting to RGB is necessary when displaying images with matplotlib to avoid color errors.
Choosing the right display method depends on your task: use cv2.imshow for real-time and interactive work, matplotlib for analysis and presentation.