Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Calculate the area of an image using Matplotlib
Next article icon

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 OpenCV

Now 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 Matplotlib

One 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 Matplotlib

This is how we can displayOpenCV images in python withMatplotlibmodule.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp