Skip to content

Instantly share code, notes, and snippets.

@ivan-krukov
Last active March 22, 2021 10:05
Show Gist options
  • Save ivan-krukov/5215ae912da29e59c4091863588eccac to your computer and use it in GitHub Desktop.
Save ivan-krukov/5215ae912da29e59c4091863588eccac to your computer and use it in GitHub Desktop.
Show opencv image in ipython notebook
import cv2
import urllib.request
# Will use matplotlib for showing the image
from matplotlib import pyplot as plt
# Plot inline
%matplotlib inline
# For local images, read as usual
# img = cv2.imread("opencv-logo2.png")
# For remote, use urllib, as per "http://stackoverflow.com/questions/21061814"
req = urllib.request.urlopen("http://cloudcv.org/static/img/opencv.jpg")
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1)
# The important part - Correct BGR to RGB channel
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Plot
plt.imshow(img)
@IgorFobia
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment