def dark_channel_priori(img, dark_channel_img): SIZE_OF_PATCH = 15 height, width, channels = img.shape for u in xrange(height - SIZE_OF_PATCH): for v in xrange(width - SIZE_OF_PATCH): min_pixel = 255 for i in xrange(SIZE_OF_PATCH): for j in xrange(SIZE_OF_PATCH): for c in xrange(channels): if img[u+i,v+j,c]<min_pixel: min_pixel = img[u+i,v+j,c] dark_channel_img[u,v] = min_pixel cv2.imshow('dark_channel',dark_channel_img) dark_channel_img_sorted = cv2.sort(dark_channel_img,cv2.SORT_DESCENDING) avg_pixel = dark_channel_img_sorted[3,2] # we choose a brighest dark channel value flag = 0 for u in xrange(height): for v in xrange(width): if ((dark_channel_img[u,v] == avg_pixel) & (flag == 0)): atmos_light = img[u,v] cv2.circle(img,(u,v),5,(0,0,255)) flag = 1 cv2.imshow('ImageWindow_with_atoms_light',img) print atmos_light t_img = np.zeros((height,width,1), np.float16) for u in xrange(height): for v in xrange(width): t_img[u,v] = 1 - 0.95*(dark_channel_img[u,v]/float(max(atmos_light[0],atmos_light[1],atmos_light[2]))) if t_img[u,v] < 0.20: t_img[u,v] = 0.20 atmos_light_avg = (float(atmos_light[0])+float(atmos_light[1])+float(atmos_light[2]))/3.0 print atmos_light_avg dehazed_image = np.zeros((height,width,3), np.float16) for u in xrange(height): for v in xrange(width): for c in xrange(channels): dehazed_image[u,v,c] = (float(img[u,v,c]) - float(atmos_light_avg)) / float(t_img[u,v]) + atmos_light_avg uchar_t_img = np.uint8(t_img*255) uchar_dehazed_image = np.uint8(dehazed_image) cv2.imshow('dehazed_image',uchar_dehazed_image) cv2.imshow('transition',uchar_t_img) cv2.imshow('dark_channel_sorted',dark_channel_img_sorted) cv2.waitKey() cv2.destroyAllWindows()
def sort(self, axis=1, desc=False): if axis == 0: sortFlags = cv.SORT_EVERY_COLUMN elif axis == 1: sortFlags = cv.SORT_EVERY_ROW if desc: sortFlags += cv.SORT_DESCENDING else: sortFlags += cv.SORT_ASCENDING out = _mimicMat(self) cv.sort(self._, sortFlags, out._) del self._ if self.UMat: del self.u self.u = out.u self._ = self.u else: del self._n self._n = out._n self._ = self._n return None
def doAutoColorBalance(self, imageData): """ automagically balance color by applying histogram equilization to the image channels. """ if (self.autoLevelsColorCorrection): # perform "simplest color balance" adapted from: # http://web.stanford.edu/~sujason/ColorBalancing/simplestcb.html halfSatLimit = self.autoLevelsSaturationLimit / 2.0 tempChan = np.zeros(shape=imageData[:, :, 0].shape) # loop through each image channel for i in range(3): # flatten out the image and sort all values from low to high flat = imageData[:, :, i].reshape(1, imageData[:, :, i].size) flat = cv2.sort(flat, cv2.SORT_EVERY_ROW + cv2.SORT_ASCENDING) # determine the bounds of our saturation limit in intensity values lowBound = flat[0, int(np.floor(flat.shape[1] * halfSatLimit))] highBound = flat[ 0, int(np.ceil(flat.shape[1] * (1.0 - halfSatLimit)))] # set all pixels below/above those limits to those bounds imageData[:, :, i][imageData[:, :, i] < lowBound] = lowBound imageData[:, :, i][imageData[:, :, i] > highBound] = highBound # now normalize the channel tempChan = cv2.normalize(imageData[:, :, i], tempChan, 0, 255, cv2.NORM_MINMAX) # and replace the existing data imageData[:, :, i] = tempChan elif (self.adaptiveColorCorrection): # perform adaptive auto color using Contrast Limited Adaptive Histogram Equalization imageData[:, :, 0] = self.colorCLAHE.apply(imageData[:, :, 0]) imageData[:, :, 1] = self.colorCLAHE.apply(imageData[:, :, 1]) imageData[:, :, 2] = self.colorCLAHE.apply(imageData[:, :, 2]) return imageData
feature_block = [[],[],[],[]] for index,coeff_list in enumerate(zigzag_points): for coeff in coeff_list: feature_block[index].append(dct_block[coeff[0],coeff[1]]) feature_block_np = np.array(feature_block) feature_vector = [] for quadrant,points in quadrants_points.iteritems(): summ = 0 for point in points: summ = summ + feature_block_np[point[0],point[1]] feature_vector.append(summ/PI) vectors_list.append(np.array(feature_vector)) vectors_list2 = cv2.sort(np.array(vectors_list),cv2.SORT_EVERY_ROW) print "vectors calculated" import json with open('data.json', 'w') as outfile: json.dump(vectors_list2.tolist(), outfile) i=0 blocks = [] for i in range(0,len(vectors_list)): if i%width == 0: print i/width posA = [i/width,i%width] j = i+1 for j in range(i+1,len(vectors_list)): posB = [j/width,j%width]
# 0511.py import cv2 import numpy as np # 1 src = cv2.imread('../data/fruits.jpg') hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) # 2 roi = cv2.selectROI(src) print('roi=', roi) roi_h = h[roi[1]:roi[1] + roi[3], roi[0]:roi[0] + roi[2]] hist = cv2.calcHist([roi_h], [0], None, [64], [0, 256]) backP = cv2.calcBackProject([h.astype(np.float32)], [0], hist, [0, 256], scale=1.0) ##minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(backP) ##T = maxVal - 1 # threshold # 3 hist = cv2.sort(hist, cv2.SORT_EVERY_COLUMN + cv2.SORT_DESCENDING) k = 1 T = hist[k][0] - 1 # threshold print('T=', T) ret, dst = cv2.threshold(backP, T, 255, cv2.THRESH_BINARY) cv2.imshow('dst', dst) cv2.waitKey() cv2.destroyAllWindows()
#행렬 원소 정렬 import numpy as np, cv2 m = np.random.randint(0, 100, 15).reshape(3, 5) # 임의 난수 생성 # 행렬 원소 정렬 sort1 = cv2.sort(m, cv2.SORT_EVERY_ROW) # 행단위 오름차순 sort2 = cv2.sort(m, cv2.SORT_EVERY_COLUMN) # 열단위(세로) 오름차순 sort3 = cv2.sort(m, cv2.SORT_EVERY_ROW + cv2.SORT_DESCENDING) # 행단위(가로) 내림차순 sort4 = np.sort(m, axis=1) # 세로축 정렬 sort5 = np.sort(m, axis=0) # 가로축 정렬 sort6 = np.sort(m, axis=1)[:, ::-1] # 가로축 내림차순 정렬 titles = ['m', 'sort1', 'sort2', 'sort3', 'sort4', 'sort5', 'sort6'] for title in titles: print("[%s] = \n%s\n" % (title, eval(title)))
# 행렬 원소 정렬 : cv2.sort(), np.sort() # 정렬 Index 반환 : cv2.sortIdx(), np.argsort() import cv2 import numpy as np # 0 ~ 100 까지 숫자 15개 random pick and reshape 3 x 5 matrix matrix = np.random.randint(0, 100, 15).reshape(3, 5) print(matrix.shape) # with cv2 # sort : row , ascending sort1 = cv2.sort(matrix, cv2.SORT_EVERY_ROW) # sort : row , descending sort2 = cv2.sort(matrix, cv2.SORT_EVERY_ROW + cv2.SORT_DESCENDING) # sort : col , ascending sort3 = cv2.sort(matrix, cv2.SORT_EVERY_COLUMN) # sort : col , descending sort4 = cv2.sort(matrix, cv2.SORT_EVERY_COLUMN + cv2.SORT_DESCENDING) # with Numpy # sort : x axis sort5 = np.sort(matrix, axis=1) # sort : y axis sort6 = np.sort(matrix, axis=0) # return sort Index - 행렬의 원소를 직접 정렬하지 않고, 정렬된 원소의 원 Index를 반환한다. # cv2 Idx_sort1 = cv2.sortIdx(matrix, cv2.SORT_EVERY_ROW) ''' [[2 0 1 4 3]