Last active
August 4, 2023 15:49
-
-
Save lalanikarim/df44bac00918ce8efdfc438c2d73592d to your computer and use it in GitHub Desktop.
Calling cv2_imshow in jupyter notebook
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "a763c365-f965-40cd-8877-ffaea9f8885b", | |
"metadata": {}, | |
"source": [ | |
"## Overview\n", | |
"\n", | |
"`cv2_imshow` is an opencv image rendering function available on **Google Colab** from `google.colab.patches` module. \n", | |
"You may be unable to use it locally within **Jupyter Notebook** though. \n", | |
"\n", | |
"You may use the definition below for the `cv2_imshow` function to get the code to work locally." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "abfd3459-19fa-4b09-954e-2e4d76c23063", | |
"metadata": {}, | |
"source": [ | |
"## Import Libraries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "74fb2b55-02d9-4c32-a359-c698c565dfe8", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np \n", | |
"import cv2\n", | |
"# comment the original import from google.colab.patch\n", | |
"# from google.colab.patches import cv2_imshow \n", | |
"\n", | |
"# import pytplot from matplotlib if not already imported\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"# for inline plot rendering\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "00405dab-26d3-4b1e-8d17-8d147ca042b3", | |
"metadata": {}, | |
"source": [ | |
"## Override cv2_imshow" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "ba71b471-50da-4b94-9603-b1071278f226", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"def cv2_imshow(img):\n", | |
" # set appropriate figure size in case it is too small or too large. Don't worry about it being square.\n", | |
" plt.figure(figsize=(10,10))\n", | |
" try:\n", | |
" # try simple BGR 2 RGB conversion so pyplot can show the image\n", | |
" plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n", | |
" except:\n", | |
" # this is for a special case in case the above doesn't work after applying convolutions\n", | |
" plt.imshow(cv2.cvtColor(np.uint8(np.absolute(img)), cv2.COLOR_BGR2RGB))\n", | |
" plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "8a46c049-3bc6-4fae-bb43-ec8657fd3c00", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment