Python Apply Image Threshold on Block Size? -


i have 2d array shown below:

[[118 127 133 ..., 213 211 211]  [125 128 130 ..., 213 213 213]  [119 124 130 ..., 214 213 213]  ...,  [ 36  54  44 ..., 109 101 101]  [ 37  52  47 ..., 112 101 101]  [ 39  50  51 ..., 104  99  99]] 

i need apply threshold on matrix locally , without overlapping them. need break 2d matrix smaller 2d matrix. , compute new threshold smaller 2d matrix , apply threshold smaller 2d matrix , same smaller matrices. have combine them in end. there python function easily? thank you.

edit

import sys numpy import *  import scipy.misc matplotlib import pyplot  def otsu1( hist, total ):      no_of_bins = len( hist ) # should 256      intra_class_variances = []      threshold in range( 0, no_of_bins ):         # first try find weight , variance on background         sum_background      = float(sum( hist[0:threshold] ))         weight_background   = sum_background / total         mean_background     = 0.0         variance_background = 0.0          # print weight_background          if sum_background > 0.0: # avoid division 0             x in range( 0, threshold ):                 mean_background += x * hist[x]             mean_background /= sum_background              x in range( 0, threshold ):                 variance_background += (x - mean_background) ** 2 * hist[x]             variance_background /= sum_background              # foreground         sum_foreground      = float(sum( hist[threshold:no_of_bins] ))         weight_foreground   = sum_foreground / total         mean_foreground     = 0.0         variance_foreground = 0.0          if sum_foreground > 0.0:             x in range( threshold, no_of_bins ):                 mean_foreground += x * hist[x]             mean_foreground /= sum_foreground              x in range( threshold, no_of_bins ):                 variance_foreground += (x - mean_foreground) ** 2 * hist[x]             variance_foreground /= sum_foreground             # print variance_foreground, mean_foreground          # find variances within these 2 classes         intra_class_variances.append( weight_background * variance_background + weight_foreground * variance_foreground )      print argmin( intra_class_variances ) - 1      # use threshold has minimum intra class variance     return argmin( intra_class_variances ) - 1   def main():     img = scipy.misc.imread( 'otsu_test_1.jpg' )     # print img     # resize more managable size     # img = scipy.misc.imresize( img, (1944 / 4, 2592 / 4) )       # convert grayscale     # grayscale = img.dot( [0.299, 0.587, 0.144] )      rows, cols = shape( img )      # create 256 bins histogram     hist = histogram( img, 256 )[0]     # print len(hist)     # print hist      # apply otsu thresholding     thresh = otsu1( hist, rows * cols )     # print thresh      figure  = pyplot.figure( figsize=(14, 6) )     figure.canvas.set_window_title( 'otsu thresholding' )      axes    = figure.add_subplot(121)     axes.set_title('original')     axes.get_xaxis().set_visible( false )     axes.get_yaxis().set_visible( false )     axes.imshow( img, cmap='greys_r' )      axes    = figure.add_subplot(122)     axes.set_title('otsu thresholding')     axes.get_xaxis().set_visible( false )     axes.get_yaxis().set_visible( false )     axes.imshow( img >= thresh, cmap='greys_r' )      pyplot.show()  if __name__ == '__main__':     main() 

otsu's threshold

x = blockshaped(img, 4, 10)  i, j in enumerate(x):     hist = np.histogram(j, bins=256)[0]     otsu_thres = otsu_threshold(hist, size)     print otsu_thres     x[i] = j >= otsu_thres  p = unblockshaped(x, 188, 250) 


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -