Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

Opencv Python - Read and Display Image

The document discusses reading and displaying images using OpenCV Python. It explains how to read an image into a variable using cv2.imread(), specifying the image path and optional flag for color/grayscale. It then explains how to display the image in a window using cv2.imshow(), providing the window name and image. The code sample reads an image, displays it in a window called "sample image", and waits for a key press before destroying the window.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Opencv Python - Read and Display Image

The document discusses reading and displaying images using OpenCV Python. It explains how to read an image into a variable using cv2.imread(), specifying the image path and optional flag for color/grayscale. It then explains how to display the image in a window using cv2.imshow(), providing the window name and image. The code sample reads an image, displays it in a window called "sample image", and waits for a key press before destroying the window.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

OpenCV Python – Read and Display Image – Example

OpenCV Python – Read and Display Image

In Computer Vision applications, images are an integral part of the development process. Often there would be
a need to read images and display them if required. To read and display image using OpenCV Python, you
could use cv2.imread() for reading image to a variable and cv2.imshow() to display the image in a separate
window.

Syntax – cv2.imread()
cv2. imread(/complete/path/to/image,flag)

First argument is complete path to the image along with the extension.

Second argument is an optional flag which could be any one of the following :

1. cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
2. cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
3. cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel

Returns numpy array, containing the pixel values. For colored images, each pixel is represented as an array
containing Red, Green and Blue channels.

Note that the default flag is cv2.IMREAD_COLOR. Hence even if read a png image with transparency, the
transparency channel is neglected.

Syntax – cv2.imshow()
cv2. imshow (window_name, image)

First argument is name of the window that is displayed when image is shown in.

Second argument is the image to be shown in the window.

Example
Following is an example python program to read and display an image.

import cv2

img =
cv2.imread('/home/img/
python.png')

cv2.imshow('sample
image',img)

cv2.waitKey(0) # waits
until a key is pressed
import cv2

img = cv2.imread('/home/img/python.png')

cv2.imshow('sample image',img)

cv2.waitKey(0) # waits until a key is pressed


cv2.destroyAllWindows() # destroys the window showing image

When above program is run, an image is shown in another window named ‘sample image’, as specified in the
arguments of imshow().

Right click on image in the window shows options for various operations out of which some are zoom-in, zoom-
out etc.
OpenCV Python Tutorial

⊩ OpenCV Python Tutorial

⊩ OpenCV - Setup with Anaconda

⊩ OpenCV - Read and Display Image

⊩ OpenCV - Save Image

⊩ OpenCV - Get Image Shape/Dimensions

⊩ OpenCV - Rezise Image - Upscale, Downscale

⊩ OpenCV - Read Image with Transparency Channel

Image Processing

⊩ OpenCV - Edge Detection

⊩ OpenCV - Gaussian Blur

You might also like