In [1]:
import numpy as np
import cv2
In [2]:
img = cv2.imread('roybatty_100dpi.png')
In [3]:
img.shape
Out[3]:
In [4]:
img_r = img[:,:,0]
img_g = img[:,:,1]
img_b = img[:,:,2]
In [5]:
img_r.shape
Out[5]:
In [6]:
interpolated_img_r = np.zeros((img_r.shape[0]*2-1,img_r.shape[1]*2-1))
interpolated_img_g = np.zeros((img_r.shape[0]*2-1,img_r.shape[1]*2-1))
interpolated_img_b = np.zeros((img_r.shape[0]*2-1,img_r.shape[1]*2-1))
In [8]:
def place_pixel(interpolated_mat,mat):
for i in range(interpolated_mat.shape[0]):
for j in range(interpolated_mat.shape[1]):
if i%2==0 and j%2==0:
interpolated_mat[i,j]=mat[int(i/2),int(j/2)]
return interpolated_mat
In [9]:
def interpolate_pixel(mat):
for i in range(mat.shape[0]):
for j in range(mat.shape[1]):
if i%2!=0 and j%2!=0:
mat[i,j]=np.mean([mat[i-1,j-1],mat[i-1,j+1],mat[i+1,j-1],mat[i+1,j+1]])
elif i%2!=0 and j%2==0:
mat[i,j]=np.mean([mat[i-1,j],mat[i+1,j]])
elif i%2==0 and j%2!=0:
mat[i,j]=np.mean([mat[i,j-1],mat[i,j+1]])
return mat
In [10]:
interpolated_img_r = place_pixel(interpolated_img_r,img_r)
interpolated_img_g = place_pixel(interpolated_img_g,img_g)
interpolated_img_b = place_pixel(interpolated_img_b,img_b)
In [12]:
interpolated_img_r_ = interpolate_pixel(interpolated_img_r)
interpolated_img_g_ = interpolate_pixel(interpolated_img_g)
interpolated_img_b_ = interpolate_pixel(interpolated_img_b)
In [13]:
new_img = np.zeros((img_r.shape[0]*2-1,img_r.shape[1]*2-1,3))
new_img[:,:,0] = interpolated_img_r_
new_img[:,:,1] = interpolated_img_g_
new_img[:,:,2] = interpolated_img_b_
In [14]:
cv2.imwrite('image.png',new_img)
Out[14]: