numpy - Python.. image processing -


i need store histograms of images database can later used compare histogram of image given user. problem: how can store histograms & how can compare histograms?

import numpy np import cv2 matplotlib import pyplot plt  def thresholded(center, pixels):     out = []     in pixels:         if >= center:             out.append(1)         else:             out.append(0)     return out  def get_pixel_else_0(l, idx, idy, default=0):     try:         return l[idx,idy]     except indexerror:         return default  img = cv2.imread('006a61.jpg', 0) transformed_img = cv2.imread('006a61.jpg', 0)  x in range(0, len(img)):     y in range(0, len(img[0])):         center        = img[x,y]         top_left      = get_pixel_else_0(img, x-1, y-1)         top_up        = get_pixel_else_0(img, x, y-1)         top_right     = get_pixel_else_0(img, x+1, y-1)         right         = get_pixel_else_0(img, x+1, y )         left          = get_pixel_else_0(img, x-1, y )         bottom_left   = get_pixel_else_0(img, x-1, y+1)         bottom_right  = get_pixel_else_0(img, x+1, y+1)         bottom_down   = get_pixel_else_0(img, x,   y+1 )          values = thresholded(center, [top_left, top_up, top_right, right, bottom_right,                                       bottom_down, bottom_left, left])          weights = [1, 2, 4, 8, 16, 32, 64, 128]         res = 0         in range(0, len(values)):             res += weights[a] * values[a]          transformed_img.itemset((x,y), res)      #print x  cv2.imshow('image', img) cv2.imshow('thresholded image', transformed_img)  hist,bins = np.histogram(img.flatten(),256,[0,256])  cdf = hist.cumsum() cdf_normalized = cdf * hist.max()/ cdf.max()  plt.plot(cdf_normalized, color = 'b') plt.hist(transformed_img.flatten(),256,[0,256], color = 'r') plt.xlim([0,256]) plt.legend(('cdf','histogram'), loc = 'upper left') plt.show()  cv2.waitkey(0) cv2.destroyallwindows() 

you can use again np.histogram values of histogram array, save file np.save , load later np.load. histogram comparison easy. np.linalg.norm(hist1 - hist2). see below (i changed cv2 scupy.misc/pyplot convenience).

import numpy np scipy.misc import imread matplotlib import pyplot plt  pic = r"c:\users\public\pictures\sample pictures\koala.jpg"   def thresholded(center_, pixels):     out = []     a_ in pixels:         if a_ >= center_:             out.append(1)         else:             out.append(0)     return out   def get_pixel_else_0(l, idx, idy, default=0):     try:         return l[idx, idy]     except indexerror:         return default  img = imread(pic, mode='i') transformed_img = imread(pic, mode='i')  x in range(0, len(img)):     y in range(0, len(img[0])):         center        = img[x, y]         top_left      = get_pixel_else_0(img, x-1, y-1)         top_up        = get_pixel_else_0(img, x, y-1)         top_right     = get_pixel_else_0(img, x+1, y-1)         right         = get_pixel_else_0(img, x+1, y)         left          = get_pixel_else_0(img, x-1, y)         bottom_left   = get_pixel_else_0(img, x-1, y+1)         bottom_right  = get_pixel_else_0(img, x+1, y+1)         bottom_down   = get_pixel_else_0(img, x,   y+1)          values = thresholded(center, [top_left, top_up, top_right, right, bottom_right,                                       bottom_down, bottom_left, left])          weights = [1, 2, 4, 8, 16, 32, 64, 128]         res = 0         in range(0, len(values)):             res += weights[a] * values[a]          transformed_img.itemset((x, y), res)      # print x plt.figure() plt.imshow(img) plt.title('image') plt.figure() plt.imshow(transformed_img) plt.title('thresholded image')  hist, bins = np.histogram(img.flatten(), 256, [0, 256])  cdf = hist.cumsum() cdf_normalized = cdf * hist.max() / cdf.max() plt.figure() plt.plot(cdf_normalized, color='b') hist_trans, bins_trans = np.histogram(transformed_img.flatten(), 256, [0, 256]) plt.bar(bins_trans[:-1], hist_trans, width=np.diff(bins_trans), color='r') plt.xlim([0, 256]) plt.legend(('cdf', 'histogram'), loc='upper left') plt.show()  file = r'd:\temp\1.npy' np.save(file, hist_trans)  # here...  hist_trans1 = np.load(file) print(np.linalg.norm((hist - hist_trans1))) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -