Skip to content

Instantly share code, notes, and snippets.

@vardanagarwal
Last active December 27, 2021 13:37
Show Gist options
  • Select an option

  • Save vardanagarwal/094836d2876cdb045714424d6841ed23 to your computer and use it in GitHub Desktop.

Select an option

Save vardanagarwal/094836d2876cdb045714424d6841ed23 to your computer and use it in GitHub Desktop.
opencv_inpainting.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "opencv_inpainting.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyOFUEWyj0R7qeQtG44jujc8",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/vardanagarwal/094836d2876cdb045714424d6841ed23/opencv_inpainting.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "c88bidZtNI83",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
},
"outputId": "a1542c4d-76f9-4e5f-edd9-fb0571b3a0e8"
},
"source": [
"!pip install opencv-contrib-python==4.2.0.34"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Requirement already satisfied: opencv-contrib-python==4.2.0.34 in /usr/local/lib/python3.6/dist-packages (4.2.0.34)\n",
"Requirement already satisfied: numpy>=1.11.3 in /usr/local/lib/python3.6/dist-packages (from opencv-contrib-python==4.2.0.34) (1.18.5)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "6qpqWDuONZ7E",
"colab_type": "code",
"colab": {}
},
"source": [
"import cv2\n",
"import matplotlib.pyplot as plt"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "dZsytty9NtCB",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"outputId": "7394cd21-8696-41dd-8f87-054b915c66f8"
},
"source": [
"img = cv2.imread('image.jpg')\n",
"img = cv2.resize(img, None, fx=0.25, fy=0.25)\n",
"mask = cv2.imread('mask.jpg', 0)\n",
"print(img.shape)\n",
"mask = cv2.resize(mask, (img.shape[1], img.shape[0]))\n",
"_, mask = cv2.threshold(mask, 128, 255, cv2.THRESH_BINARY)\n",
"mask1 = cv2.bitwise_not(mask)\n",
"distort = cv2.bitwise_and(img, img, mask=mask1)\n",
"\n",
"output1 = cv2.inpaint(distort, mask, 3, cv2.INPAINT_NS)\n",
"output2 = cv2.inpaint(distort, mask, 3, cv2.INPAINT_TELEA)\n",
"\n",
"restored1 = img.copy()\n",
"restored2 = img.copy()\n",
"cv2.xphoto.inpaint(distort, mask1, restored1, cv2.xphoto.INPAINT_FSR_FAST)\n",
"cv2.xphoto.inpaint(distort, mask1, restored2, cv2.xphoto.INPAINT_FSR_BEST)\n",
"\n",
"dst3 = cv2.cvtColor(restored1, cv2.COLOR_BGR2RGB)\n",
"dst4 = cv2.cvtColor(restored2, cv2.COLOR_BGR2RGB)\n",
"dst1 = cv2.cvtColor(output1, cv2.COLOR_BGR2RGB)\n",
"dst2 = cv2.cvtColor(output2, cv2.COLOR_BGR2RGB)\n",
"dst = cv2.cvtColor(distort, cv2.COLOR_BGR2RGB)\n",
"img1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
"\n",
"plt.figure(figsize=(15, 20))\n",
"plt.subplot(2, 3, 1)\n",
"plt.imshow(img1)\n",
"plt.subplot(2, 3, 2)\n",
"plt.imshow(dst)\n",
"plt.subplot(2, 3, 3)\n",
"plt.imshow(dst1)\n",
"plt.subplot(2, 3, 4)\n",
"plt.imshow(dst2)\n",
"plt.subplot(2, 3, 5)\n",
"plt.imshow(dst3)\n",
"plt.subplot(2, 3, 6)\n",
"plt.imshow(dst4)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"(1008, 756, 3)\n",
"(1299, 1120)\n",
"7.243106842041016\n",
"11.15975308418274\n",
"51.774988889694214\n",
"1036.9667372703552\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7fadea264f98>"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
},
{
"output_type": "display_data",
"data": {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment