-
-
Save back2yes/675889e0062cc53baae768fbe13764fb to your computer and use it in GitHub Desktop.
Comparison of openCV Interpolation methods by Anthony Tanbakuchi (Backup because his site is down)
This file contains hidden or 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", | |
| "metadata": {}, | |
| "source": [ | |
| "The following are comparisons of the different image interpolation algorithms available in OpenCV. The exampes are shown for both upsizing and downsizing images. Also shown is a comparison of the speed of the different algorithms.\n", | |
| "\n", | |
| "The algorithms are: (descriptions are from the OpenCV documentation)\n", | |
| " - INTER_NEAREST - a nearest-neighbor interpolation\n", | |
| " - INTER_LINEAR - a bilinear interpolation (used by default)\n", | |
| " - INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, \n", | |
| " as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.\n", | |
| " - INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood\n", | |
| " - INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Populating the interactive namespace from numpy and matplotlib\n", | |
| "opencv version 2.4.12\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%pylab inline\n", | |
| "import cv2\n", | |
| "import imageio\n", | |
| "import itertools\n", | |
| "import matplotlib.gridspec as gridspec\n", | |
| "import pandas as pd\n", | |
| "import time\n", | |
| "\n", | |
| "# standard images in imageio\n", | |
| "ref_images = [\"coffee.png\", \"page.png\", \"immunohistochemistry.png\", \"horse.png\"] \n", | |
| "# Limit starting size\n", | |
| "images_orig = [cv2.resize(imageio.imread(im), (400,400)) for im in ref_images] \n", | |
| "# interpolation methods to compare\n", | |
| "methods=[(\"area\", cv2.INTER_AREA), \n", | |
| " (\"nearest\", cv2.INTER_NEAREST), \n", | |
| " (\"linear\", cv2.INTER_LINEAR), \n", | |
| " (\"cubic\", cv2.INTER_CUBIC), \n", | |
| " (\"lanczos4\", cv2.INTER_LANCZOS4)]\n", | |
| "# opencv version\n", | |
| "print \"opencv version\", cv2.__version__" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# function to display images\n", | |
| "def display(images, titles=['']):\n", | |
| " if isinstance(images[0], list):\n", | |
| " c = len(images[0])\n", | |
| " r = len(images)\n", | |
| " images = list(itertools.chain(*images))\n", | |
| " else:\n", | |
| " c = len(images)\n", | |
| " r = 1\n", | |
| " plt.figure(figsize=(4*c, 4*r))\n", | |
| " gs1 = gridspec.GridSpec(r, c, wspace=0, hspace=0)\n", | |
| " #gs1.update(wspace=0.01, hspace=0.01) # set the spacing between axes. \n", | |
| " titles = itertools.cycle(titles)\n", | |
| " for i in range(r*c):\n", | |
| " im = images[i]\n", | |
| " title = titles.next()\n", | |
| " plt.subplot(gs1[i])\n", | |
| " # Don't let imshow doe any interpolation\n", | |
| " plt.imshow(im, cmap='gray', interpolation='none')\n", | |
| " plt.axis('off')\n", | |
| " if i < c:\n", | |
| " plt.title(title)\n", | |
| " plt.tight_layout()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "## Upsampling comparison\n", | |
| "Take a 50x50 image and rescale it to 400x400 pixels. For ease of comparison, the original and the rescaled are shown at the same size so the algorithm image quality can be compared. For updampling, area & nearest algorithms create jagged edges while the linear makes smoother edges. The cubic and Lanczos make the sharpest edges." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment