Skip to content

Instantly share code, notes, and snippets.

@nassarofficial
Created March 30, 2016 21:32
Show Gist options
  • Save nassarofficial/31cb0b1068076d47f8ac6c56032edcf0 to your computer and use it in GitHub Desktop.
Save nassarofficial/31cb0b1068076d47f8ac6c56032edcf0 to your computer and use it in GitHub Desktop.
Assignment 3
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Papoulis-Gerchberg algorithm"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"from PIL import Image\n",
"from scipy import fftpack\n",
"from matplotlib import pyplot as plt\n",
"import cv2\n",
"import scipy.signal as signal\n",
"from scipy.ndimage.filters import gaussian_filter\n",
"from scipy import misc\n",
"from scipy import ndimage"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Function That Stores Text Values and Replaces The Text Pixels with White Pixels\n",
"#### Due to the noise surrounding the pixel, i remove all pixels surrounding the \"Red Pixel\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def replacer(pixarri,pixarrj,arr,i,j):\n",
" pixarri.append(i)\n",
" pixarrj.append(j)\n",
" pixarri.append(i)\n",
" pixarrj.append(j+1)\n",
" pixarri.append(i+1)\n",
" pixarrj.append(j+1)\n",
" pixarri.append(i+1)\n",
" pixarrj.append(j)\n",
" pixarri.append(i+1)\n",
" pixarrj.append(j-1)\n",
" pixarri.append(i)\n",
" pixarrj.append(j-1)\n",
" pixarri.append(i-1)\n",
" pixarrj.append(j-1)\n",
" pixarri.append(i-1)\n",
" pixarrj.append(j)\n",
" pixarri.append(i-1)\n",
" pixarrj.append(j+1)\n",
" arr[i][j][0] = 255\n",
" arr[i][j][1] = 255\n",
" arr[i][j][2] = 255\n",
"\n",
" arr[i][j+1][0] = 255\n",
" arr[i+1][j+1][0] = 255\n",
" arr[i+1][j][0] = 255\n",
" arr[i+1][j-1][0] = 255\n",
" arr[i][j-1][0] = 255\n",
" arr[i-1][j-1][0] = 255\n",
" arr[i-1][j][0] = 255\n",
" arr[i-1][j+1][0] = 255\n",
"\n",
" arr[i][j+1][1] = 255\n",
" arr[i+1][j+1][1] = 255\n",
" arr[i+1][j][1] = 255\n",
" arr[i+1][j-1][1] = 255\n",
" arr[i][j-1][1] = 255\n",
" arr[i-1][j-1][1] = 255\n",
" arr[i-1][j][1] = 255\n",
" arr[i-1][j+1][1] = 255\n",
"\n",
" arr[i][j+1][2] = 255\n",
" arr[i+1][j+1][2] = 255\n",
" arr[i+1][j][2] = 255\n",
" arr[i+1][j-1][2] = 255\n",
" arr[i][j-1][2] = 255\n",
" arr[i-1][j-1][2] = 255\n",
" arr[i-1][j][2] = 255\n",
" arr[i-1][j+1][2] = 255\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Loading the image and storing it into an array"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-------- Done ----------\n"
]
}
],
"source": [
"def main():\n",
"\n",
" file = \"Image Inpainting 1_w RED text.jpg\"\n",
" img = Image.open(file)\n",
" arr = np.array(img)\n",
"\n",
" pixarri = []\n",
" pixarrj = []\n",
" for i in range(arr.shape[0]):\n",
" for j in range(arr.shape[1]):\n",
" if arr[i][j][0] > 120 and arr[i][j][1] < 100 and arr[i][j][2] < 100:\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] > 120 and arr[i][j][1] > 120 and arr[i][j][2] < 110:\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] > 200 and arr[i][j][1] > 160 and arr[i][j][1] < 195 and arr[i][j][1] > 160 and arr[i][j][1] < 195:\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] > 90 and arr[i][j][0] < 140 and arr[i][j][1] > 160 and arr[i][j][1] < 195 and arr[i][j][1] > 160 and arr[i][j][1] < 195:\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(55,135) and arr[i][j][1] in range(15,25) and arr[i][j][2] in range(15,25):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(49,65) and arr[i][j][1] in range(15,35) and arr[i][j][2] in range(15,35):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(220,256) and arr[i][j][1] in range(80,200) and arr[i][j][2] in range(80,200):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(95,230) and arr[i][j][1] in range(10,70) and arr[i][j][2] in range(15,88):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(150,220) and arr[i][j][1] in range(-1,60) and arr[i][j][2] in range(-1,60):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(180,256) and arr[i][j][1] in range(-1,30) and arr[i][j][2] in range(-5,30):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(180,248) and arr[i][j][1] in range(45,113) and arr[i][j][2] in range(31,113):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(145,155) and arr[i][j][1] in range(120,135) and arr[i][j][2] in range(110,135):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" elif arr[i][j][0] in range(140,155) and arr[i][j][1] in range(106,115) and arr[i][j][2] in range(128,135):\n",
" replacer(pixarri,pixarrj,arr,i,j)\n",
" \n",
" ## Due to format issues, we save the image we replaced it pixels, and load it again for OpenCV to \n",
" ## turn it into grayscale and into a structure we could apply the algorithm on\n",
" \n",
" img = Image.fromarray(arr)\n",
" ## Saved Replaced Pixels File\n",
" img.save(\"red-removed\"+file)\n",
" \n",
" ## Reading the saved file into open CV\n",
" img1 = cv2.imread(\"red-removed\"+file,0)\n",
"\n",
" rows, cols = img1.shape\n",
" crow,ccol = rows/2 , cols/2\n",
"\n",
" mask = np.zeros((rows,cols,2),np.uint8)\n",
" mask[crow-15:crow+15, ccol-15:ccol+15] = 1\n",
"\n",
" img_float32bkp = np.float32(img1)\n",
"\n",
" ### Iterating of the Papoulis-Gerchberg algorithm\n",
" for d in range(10):\n",
" ## Discrete Fourier Transform\n",
" dft = cv2.dft(img_float32bkp,flags = cv2.DFT_COMPLEX_OUTPUT)\n",
" \n",
" ## The Shift\n",
" dft_shift = np.fft.fftshift(dft)\n",
" \n",
" magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))\n",
"\n",
" fshift = dft_shift*mask\n",
" f_ishift = np.fft.ifftshift(fshift)\n",
" img_back = cv2.idft(f_ishift)\n",
" img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])\n",
" cv2.normalize(img_back,img_back,0,255,cv2.NORM_MINMAX)\n",
"\n",
" for i in range(len(pixarri)):\n",
" img_float32bkp[pixarri[i]][pixarrj[i]] = img_back[pixarri[i]][pixarrj[i]]\n",
"\n",
" print \"-------- Done ----------\"\n",
" plt.subplot(122),plt.imshow(img_float32bkp, cmap = 'gray')\n",
" plt.title('Output'), plt.xticks([]), plt.yticks([])\n",
" plt.savefig('Output.png', bbox_inches='tight')\n",
" plt.show()\n",
"\n",
"main()\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Due to the quality and the noise of the image, there are more than red pixels... These conditions remove Red, Maroon, Pink, Purple and Brown Pixels with their different hues...."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://dl.dropboxusercontent.com/u/46105572/pixels.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://dl.dropboxusercontent.com/u/46105572/red-removedImage%20Inpainting%201_w%20RED%20text.jpg\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://dl.dropboxusercontent.com/u/46105572/Output.png\">"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment