Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.image as mpimg
  3. import numpy as np
  4.  
  5. def get_hue(bgr):
  6.     #print bgr.shape
  7.     max_val = max(bgr)
  8.     min_val = min(bgr)
  9.     if max_val == bgr[0]: # Blue is max val
  10.         return 4.0 + (bgr[2] - bgr[1]) / (max_val - min_val)
  11.     if max_val == bgr[1]: # Green is max val
  12.         return 2.0 + (bgr[0] - bgr[2]) / (max_val - min_val)
  13.     if max_val == bgr[2]: # Red is max val
  14.         return 0.0 + (bgr[1] - bgr[2]) / (max_val - min_val)
  15.  
  16. def bubble_sort_step(row):
  17.     """row needs to have shape (x, 3)"""
  18.     for i in range(1,row.shape[0]):
  19.         if get_hue(row[i-1,:]) > get_hue(row[i,:]):
  20.             temp = np.copy(row[i,:])
  21.             row[i,:] = row[i-1,:]
  22.             row[i-1,:] = temp
  23.             #row[i-1], row[i] = row[i], row[i-1]
  24.     return row
  25.    
  26. plt.figure(figsize=(5,5))
  27. img = np.random.rand(100,100,3)
  28.  
  29. for i in range(img.shape[1]):
  30.     plt.imshow(img)
  31.     for j in range(img.shape[0]):
  32.         img[j,:,:] = bubble_sort_step(img[j,:,:])
  33.     plt.pause(0.001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement