Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mstfldmr/45d6e47bb661800b982c39d30215bc88 to your computer and use it in GitHub Desktop.
Save mstfldmr/45d6e47bb661800b982c39d30215bc88 to your computer and use it in GitHub Desktop.
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()
@r0oland
Copy link

r0oland commented Nov 16, 2022

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')

image

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