display 2d array from opencv in matplotlib.pyplot.imshow()

  • Last Update :
  • Techknowledgy :

I believe that the problem is with your cv2numpy function. Try this one:

def cv2numpy(cvarr, the_type):
   a = np.asarray(cv.GetMat(cvarr), dtype = the_type)
return a

Are you using version 2.3.1? Using cv2 API, we don't need implement our own version of OpenCV/Numpy conversion anymore. For example, the following code works just right:

>>>
import cv2
   >>>
   from matplotlib
import pyplot as plt
   >>>
   lenna = cv2.imread('lenna.tiff', cv2.CV_LOAD_IMAGE_GRAYSCALE) >>>
   lenna
array([
      [162, 162, 162, ..., 170, 155, 128],
      [162, 162, 162, ..., 170, 155, 128],
      [162, 162, 162, ..., 170, 155, 128],
      ...,
      [43, 43, 50, ..., 104, 100, 98],
      [44, 44, 55, ..., 104, 105, 108],
      [44, 44, 55, ..., 104, 105, 108]
   ], dtype = uint8) >>>
   plt.imshow(lenna, cmap = 'gray') >>>
   plt.show()

Suggestion : 2

In order to create a numerical array to be passed to px.imshow, you can use a third-party library like PIL, scikit-image or opencv. We show below how to open an image from a file with skimage.io.imread, and alternatively how to load a demo image from skimage.data.,Facets can also be used to represent several images of equal shape, like in the example below where different values of the blurring parameter of a Gaussian filter are compared.,For go.Image, zmin and zmax need to be given for all channels, whereas it is also possible to pass a scalar value (used for all channels) to px.imshow.,This tutorial shows how to display and explore image data. If you would like instead a logo or static image, use go.layout.Image as explained here.

import plotly.express as px
import numpy as np
img_rgb = np.array([
   [
      [255, 0, 0],
      [0, 255, 0],
      [0, 0, 255]
   ],
   [
      [0, 255, 0],
      [0, 0, 255],
      [255, 0, 0]
   ]
], dtype = np.uint8)
fig = px.imshow(img_rgb)
fig.show()
import plotly.express as px
from skimage
import io
img = io.imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
fig = px.imshow(img)
fig.show()
import plotly.express as px
from skimage
import data
img = data.astronaut()
fig = px.imshow(img, binary_format = "jpeg", binary_compression_level = 0)
fig.show()
import plotly.express as px
import numpy as np
img = np.arange(15 ** 2).reshape((15, 15))
fig = px.imshow(img)
fig.show()
import plotly.express as px
import numpy as np
img = np.arange(100).reshape((10, 10))
fig = px.imshow(img, binary_string = True)
fig.show()
import plotly.express as px
import numpy as np
img = np.arange(100).reshape((10, 10))
fig = px.imshow(img, color_continuous_scale = 'gray')
fig.show()

Suggestion : 3

To read an image in Python using OpenCV, use cv2.imread() function. imread() returns a 2D or 3D matrix based on the number of color channels present in the image. For a binary or grey scale image, 2D array is sufficient. But for a colored image, you need 3D array.,We will also learn the order in which imread() function decodes the color channels from an image and how imread() treats different image extensions.,imread() decodes the image into a matrix with the color channels stored in the order of Blue, Green, Red and A (Transparency) respectively.,Concluding this tutorial of Python Examples, we learned to use cv2 imread() method to read an image into a Python Array.

The syntax of cv2.imread() function is given below.

cv2.imread(/path/to / image, flag)

Python Program

import cv2
#read image
img = cv2.imread('D:/image-1.png')
#print its shape
print('Image Dimensions :', img.shape)

Run the above python program, and you shall get the following output.

Image Dimensions: (400, 640, 3)