TheOpenCV module is an open-source computer vision and machine learning software library. It is a huge open-source library for computer vision, machine learning, and image processing.OpenCVsupports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such asnumpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do inNumpy can be combined withOpenCV.
First, let's look at how to display images using OpenCV:
Now there is one function called cv2.imread() which will take the path of an image as an argument. Using this function you will read that particular image and simply display it using the cv2.imshow() function.
Python3# import required moduleimportcv2# read the Image by giving pathimage=cv2.imread('gfg.png')# display that imagecv2.imshow('GFG',image)
Output:
DIsplay image using OpenCVNow let's jump into displaying the images withMatplotlibmodule. It is an amazing visualization library in Python for 2D plots of arrays. TheMatplotlib module is a multi-platform data visualization library built onNumPy arrays and designed to work with the broaderSciPy stack.
We are doing minor changes to the above code to display our image withMatplotlibmodule.
Python3# import required moduleimportcv2importmatplotlib.pyplotasplt# read imageimage=cv2.imread('gfg.png')# call imshow() using plt objectplt.imshow(image)# display that imageplt.show()
Output:
image plot with MatplotlibOne can also display gray scaleOpenCV images withMatplotlibmodule for that you just need to convert colored image into a gray scale image.
Python3# import required modulesimportcv2importmatplotlib.pyplotasplt# read the imageimage=cv2.imread('gfg.png')# convert color image into grayscale imageimg1=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)# plot that grayscale image with Matplotlib# cmap stands for colormapplt.imshow(img1,cmap='gray')# display that imageplt.show()
Output:
Display grayscale image plot with MatplotlibThis is how we can displayOpenCV images in python withMatplotlibmodule.