Skip to content

Instantly share code, notes, and snippets.

@georgeblck
Created November 20, 2019 13:08
Show Gist options
  • Select an option

  • Save georgeblck/e3e0274d725c858ba98b1c36c14e2835 to your computer and use it in GitHub Desktop.

Select an option

Save georgeblck/e3e0274d725c858ba98b1c36c14e2835 to your computer and use it in GitHub Desktop.
Comparison of openCV Interpolation methods by Anthony Tanbakuchi (Backup because his site is down)
Display the source blob
Display the rendered blob
Raw
{
"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": {
@michelpf
Copy link

Great job!

@hostlie
Copy link

hostlie commented May 14, 2022

Very Good !

@VidhuSarwal
Copy link

Thanks.

@petermg
Copy link

petermg commented May 16, 2025

Sweet!

@chr1sren
Copy link

chr1sren commented Sep 22, 2025

Well done!

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