Created
May 2, 2017 09:28
-
-
Save mstfldmr/45d6e47bb661800b982c39d30215bc88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from matplotlib import pyplot as plt | |
import cv2 | |
img = cv2.imread('/Users/mustafa/test.jpg') | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
plt.imshow(gray) | |
plt.title('my picture') | |
plt.show() |
We can also use
display()
of IPython.display module andImage.fromarray()
of PIL modulefrom PIL import Image import cv2 from IPython.display import display img = cv2.imread('image.png') # with the OpenCV function imread(), the order of colors is BGR (blue, green, red). # In Pillow, the order of colors is assumed to be RGB (red, green, blue). # As we are using Image.fromarray() of PIL module, we need to convert BGR to RGB. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Converting BGR to RGB display(Image.fromarray(img))
Worked really nice! Thank you so much!
Here is a simple function based on the code above to display the image with an optional title.
def show_rgb_image(image, title=None, conversion=cv2.COLOR_BGR2RGB):
# Converts from one colour space to the other. this is needed as RGB
# is not the default colour space for OpenCV
image = cv2.cvtColor(image, conversion)
# Show the image
plt.imshow(image)
# remove the axis / ticks for a clean looking image
plt.xticks([])
plt.yticks([])
# if a title is provided, show it
if title is not None:
plt.title(title)
plt.show()
Usage is simple:
image = cv2.imread(r'c:\Users\johan\Pictures\road.jpg')
show_rgb_image(image, 'Original Image')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try COLOR_BGR2RGB (RGB, not RBG).