Skip to main content

Show OpenCV Image With Matplotlib

Show Image With Matplotlib

Matplotlib is a drawing library that offers a wide variety of drawing methods for Python. Matplotlib; The basic python library we use in data visualization. It allows us to make 2 and 3 dimensional drawings. In order to use Matplotlib, we need to add it to our project as follows:

from matplotlib import pyplot as plt

The image is read as follows:

img = cv2.imread('bird.jpg') 

Finally, the captured image is displayed on the screen with the following code:

plt.imshow(img)
plt.xticks([])
, plt.yticks([])
plt.show()

Code and output are as follows:

import cv2
from matplotlib import pyplot as plt
img = cv2.imread(
'bird.jpg')
plt.imshow(img)
plt.xticks([])
, plt.yticks([])
plt.show()

Output:

 



Comments

Popular posts from this blog

OpenCV Crop an image in Python

 Crop operation in OpenCV is very simple. You should first read the image. Then the values x, y, h, w must be determined to specify the region to be cropped. You can do this using the code below. import cv2 image = cv2.imread( 'image.jpg' ) y= 10 x= 20 h= 200 w= 250 crop = image[y:y+h , x:x+w] cv2.imshow( 'Image' , crop) cv2.waitKey( 0 )

OpenCV Drawing a Line

How To Draw a Line In OpenCV Python? To draw a line, we need to select the starting and ending coordinates of the line. First, let's create a black image and draw a green line. We need to use the code below to create a black image: image = np.zeros(( 360 , 360 , 3 ) , np.uint8) Now we can create our line. cv2.line(image , ( 0 , 0 ) , ( 511 , 511 ) , ( 0 , 255 , 0 ) , 5 ) The code for creating a line is as follows: import numpy as np import cv2 image = np.zeros(( 360 , 360 , 3 ) , np.uint8) cv2.line(image , ( 0 , 0 ) , ( 511 , 511 ) , ( 0 , 255 , 0 ) , 5 ) cv2.imshow( "image" , image) cv2.waitKey( 0 ) cv2.destroyAllWindows() Output: Another Example import numpy as np import cv2 image = np.zeros(( 360 , 360 , 3 ) , np.uint8) cv2.line(image , ( 0 , 180 ) , ( 360 , 180 ) , ( 0 , 255 , 0 ) , 5 ) cv2.imshow( "image" , image) cv2.waitKey( 0 ) cv2.destroyAllWindows() Output:

Opencv Image Properties - rows, columns and channels, type of image data, number of pixels

Opencv Image Properties  OpenCV allows access to image properties. These properties are rows, columns and channels, type of image data, number of pixels. The img.shape command is used to access the shape of the image. Returns rows, columns and channels. import  cv2 img = cv2.imread( 'flower.jpg' ) print (img.shape) Result: (400, 400, 3) The data type of the image is obtained with img.dtype import cv2 img = cv2.imread( 'flower.jpg' ) print (img.dtype) Result: uint8