Advertisement
dan-masek

Untitled

Feb 15th, 2022
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. from cv2_45 import cv2
  2. import numpy as np
  3. import time
  4. from scipy.interpolate import interp1d
  5.  
  6.  
  7. def fillMissingValue(img_in):
  8.     img_out = np.copy(img_in)
  9.  
  10.     #x = np.arange(img_in.shape[0])
  11.  
  12.     #Proceed column by column
  13.     for i in range(img_in.shape[1]):
  14.         col = img_in[:,i]
  15.  
  16.         col_r = col[:,0]
  17.         col_g = col[:,1]
  18.         col_b = col[:,2]
  19.         r = interpolate(col_r)
  20.         g = interpolate(col_g)
  21.         b = interpolate(col_b)
  22.         img_out[:,i,0] = r
  23.         img_out[:,i,1] = g
  24.         img_out[:,i,2] = b
  25.  
  26.     return img_out
  27.  
  28. def interpolate(y):
  29.     x = np.arange(len(y))
  30.     idx = np.nonzero(y)
  31.     interp = interp1d(x[idx],y[idx],fill_value="extrapolate" )
  32.  
  33.     return interp(x)
  34.    
  35.    
  36.    
  37.    
  38.    
  39.    
  40. def fillMissingValue2(img_in):
  41.     rows, cols, _ = img_in.shape
  42.    
  43.     img_out = np.copy(img_in)
  44.  
  45.     x = np.arange(rows)
  46.     img2d = img.reshape(rows, cols * 3)
  47.     idx = np.nonzero(np.sum(img2d, axis=1))
  48.  
  49.     #Proceed column by column
  50.     for i in range(cols):
  51.         col = img_in[:,i]
  52.         rgb = interpolate2(x, col, idx)
  53.         img_out[:,i] = rgb
  54.  
  55.     return img_out
  56.    
  57. def interpolate2(x, y, idx):
  58.     interp = interp1d(x[idx],y[idx,:],axis=1, fill_value="extrapolate" )
  59.     return interp(x)
  60.    
  61.    
  62.  
  63. def fillMissingValue3(img_in):
  64.     rows, cols, _ = img_in.shape
  65.  
  66.     x = np.arange(rows)
  67.     img2d = img.reshape(rows, cols * 3)
  68.     idx = np.nonzero(np.sum(img2d, axis=1))
  69.  
  70.     interp = interp1d(x[idx],img2d[idx,:],axis=1, fill_value="extrapolate" )
  71.     return np.uint8(interp(x)).reshape(rows, cols, 3)
  72.  
  73.  
  74.  
  75.  
  76. if __name__ == "__main__":
  77.     img = cv2.imread("lena.jpg")
  78.     img = cv2.resize(img, (1024,1024))
  79.     img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  80.  
  81.     for row in [5,24,27,40,90,110,120,200,300,400,500,700,930]:
  82.         img[row,:,:] = 0
  83.  
  84.     start1 = time.time()
  85.     img2 = fillMissingValue(img)
  86.     end1 = time.time()
  87.    
  88.     start2 = time.time()
  89.     img3 = fillMissingValue2(img)
  90.     end2 = time.time()
  91.    
  92.     start3 = time.time()
  93.     img4 = fillMissingValue3(img)
  94.     end3 = time.time()
  95.    
  96.     print (img2 == img3).all()
  97.     print (img2 == img4).all()
  98.  
  99.     print("Process time (1): {}".format(np.round(end1-start1,3)))
  100.     print("Process time (2): {}".format(np.round(end2-start2,3)))
  101.     print("Process time (3): {}".format(np.round(end3-start3,3)))
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement