def do_magic(self, img_path, upper, lower): bound = BoundingBox([upper[0], upper[1], upper[0], lower[1], lower[0], lower[1], lower[0], upper[1]]) f = Fix() retMsk, retCorr, sumPts, ptsXY = f.register_mask(img_path) pts = f.pts.flatten() self_transform = self.avrg - pts _, indexes = self.nbrs_clf.kneighbors(pts, n_neighbors=5) indexes = indexes[0] lbp = LocalBinaryPatternsDescriptor() hist = HistDescriptor() p = np.ndarray((4, 2), buffer=bound.data(), dtype=float) orig_img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) orig_lbp_desc = lbp.calculate_descriptor(orig_img_part) orig_hist_desc = hist.calculate_descriptor(orig_img_part) # draw_points_on_img(img_path, np.ndarray((4, 2), buffer=bound, dtype=float)) for ind in indexes: pts = np.ndarray((4, 2), buffer=bound.data(), dtype=float).flatten() p = new_points(pts, self_transform, self.transform_matrix[ind], self.avrg) p = p.reshape(4, 2) img_path = os.path.join(DATABASE_LOCATION, '%03d.png' % self.img_ids[ind]) img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) lbp_desc = lbp.calculate_descriptor(img_part) hist_desc = hist.calculate_descriptor(img_part) lbp_diff = np.linalg.norm(orig_lbp_desc - lbp_desc) hist_diff = np.linalg.norm(orig_hist_desc - hist_desc) head, filename = os.path.split(img_path) yield draw_points_on_img(os.path.join(DATABASE_LOCATION, '%03d.png' % self.img_ids[ind]), p, show=False), lbp_diff, hist_diff, filename
def do_magic_v2(self, img_path, upper, lower, heap_size=3): bound = BoundingBox([upper[0], upper[1], upper[0], lower[1], lower[0], lower[1], lower[0], upper[1]]) f = Fix() retMsk, retCorr, sumPts, ptsXY = f.register_mask(img_path) pts = f.pts.flatten() self_transform = self.avrg - pts lbp = LocalBinaryPatternsDescriptor() hist = HistDescriptor() p = np.ndarray((4, 2), buffer=bound.data(), dtype=float) img = cv2.imread(img_path, 1) orig_img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], img) orig_lbp_desc = lbp.calculate_descriptor(orig_img_part) orig_hist_desc = hist.calculate_descriptor(orig_img_part) orig_desc = orig_lbp_desc # select scale area = bound.area() # finding closest scale. we sup closest scale - scale with closest area size scale = min(self.descriptors_db.cfg.sizes, key=lambda x: abs(area - x[0] * x[1])) center = bound.center() descriptor_coordinates = (center / scale).astype(np.int) orig_desc = self.descriptors_db.calculate_one_descriptor(img, scale, descriptor_coordinates[0], descriptor_coordinates[1]) heap = Heap(heap_size) for ind, descriptor_value in self.descriptors_db.get_descriptors(scale, descriptor_coordinates): descriptor_value = np.asarray(descriptor_value) dist = np.linalg.norm(orig_desc - descriptor_value) ind = int(ind.split('.')[0]) heap.push((-dist, ind)) for desc_value, ind in heap.data(): pts = np.ndarray((4, 2), buffer=bound.data(), dtype=float).flatten() trans_ind = self.img_ids.index(ind) p = new_points(pts, self_transform, self.transform_matrix[trans_ind], self.avrg) p = p.reshape(4, 2) img_path = os.path.join(DATABASE_LOCATION, '%03d.png' % self.img_ids[trans_ind]) img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) lbp_desc = lbp.calculate_descriptor(img_part) hist_desc = hist.calculate_descriptor(img_part) lbp_diff = np.linalg.norm(orig_lbp_desc - lbp_desc) hist_diff = np.linalg.norm(orig_hist_desc - hist_desc) head, filename = os.path.split(img_path) yield draw_points_on_img(os.path.join(DATABASE_LOCATION, '%03d.png' % self.img_ids[trans_ind]), p, show=False), lbp_diff, hist_diff, filename
def calculate_one_descriptor(self, img, size, i, j): descriptor = self._get_descriptor() w, h = size descriptor.w = w descriptor.h = h img_part = crop_image(w * i, h * j, w * (i + 1), h * (j + 1), img) res = descriptor.calculate_descriptor(img_part, mean=img.mean()) return res
def magic_method(img_path): transform_matrix, nbrs_clf, img_ids, avrg = load_database(200) f = Fix() retMsk, retCorr, sumPts, ptsXY = f.register_mask(img_path) pts = f.pts.flatten() self_transform = avrg - pts _, indexes = nbrs_clf.kneighbors(pts, n_neighbors=5) indexes = indexes[0] draw_points_on_img(img_path, np.ndarray((4, 2), buffer=np.array([64.0, 64, 64, 128, 128, 128, 128, 64]), dtype=float)) lbp = HistDescriptor() for ind in indexes: pts = np.ndarray((4, 2), buffer=np.array([64.0, 64, 64, 128, 128, 128, 128, 64]), dtype=float).flatten() p = new_points(pts, self_transform, transform_matrix[ind], avrg) p = p.reshape(4, 2) img_path = os.path.join(DATABASE_LOCATION, '%03d.png' % img_ids[ind]) draw_points_on_img(img_path, p) img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) print lbp.calculate_descriptor(img_part) return pathImgMask = "%s_mask.png" % img_path pathImgMasked = "%s_masked.png" % img_path pathImgOnMask = "%s_onmask.png" % img_path pathPtsCSV = "%s_pts.csv" % img_path print f.pts if retCorr > 0.7: cv2.imwrite(pathImgMask, f.newMsk) cv2.imwrite(pathImgMasked, f.newImgMsk) cv2.imwrite(pathImgOnMask, f.newImgOnMsk) np.savetxt(pathPtsCSV, f.pts, delimiter=',', newline='\n', fmt='%0.1f') else: tmpNewImgMsk = cv2.imread(img_path, 1) # cv2.IMREAD_COLOR) tmpImgOnMsk = np.zeros((tmpNewImgMsk.shape[0], tmpNewImgMsk.shape[1]), np.uint8) p00 = (0, 0) p01 = (0, tmpNewImgMsk.shape[0]) p10 = (tmpNewImgMsk.shape[1], 0) p11 = (tmpNewImgMsk.shape[1], tmpNewImgMsk.shape[0]) cv2.line(tmpNewImgMsk, p00, p11, (0, 0, 255), 4) cv2.line(tmpNewImgMsk, p01, p10, (0, 0, 255), 4) f.newMsk[:] = 0 cv2.imwrite(pathImgMask, f.newMsk) cv2.imwrite(pathImgMasked, tmpNewImgMsk) cv2.imwrite(pathImgOnMask, tmpImgOnMsk) tmpPts = np.zeros((7, 2), np.float64) np.savetxt(tmpPts, delimiter=',', newline='\n') fnErr = "%s.err" % img_path f = open(fnErr, 'w') f.close() fzip = "%s.zip" % img_path zObj = zipfile.ZipFile(fzip, 'w') zipDir = '%s_dir' % os.path.basename(img_path) lstFimg = (img_path, pathImgMask, pathImgMasked, pathImgOnMask, pathPtsCSV) for ff in lstFimg: ffbn = os.path.basename(ff) zObj.write(ff, "%s/%s" % (zipDir, ffbn)) print "retCorr = %s" % retCorr
def do_magic(self, img_path, upper, lower): bound = BoundingBox([ upper[0], upper[1], upper[0], lower[1], lower[0], lower[1], lower[0], upper[1] ]) f = Fix() retMsk, retCorr, sumPts, ptsXY = f.register_mask(img_path) pts = f.pts.flatten() self_transform = self.avrg - pts _, indexes = self.nbrs_clf.kneighbors(pts, n_neighbors=5) indexes = indexes[0] lbp = LocalBinaryPatternsDescriptor() hist = HistDescriptor() p = np.ndarray((4, 2), buffer=bound.data(), dtype=float) orig_img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) orig_lbp_desc = lbp.calculate_descriptor(orig_img_part) orig_hist_desc = hist.calculate_descriptor(orig_img_part) # draw_points_on_img(img_path, np.ndarray((4, 2), buffer=bound, dtype=float)) for ind in indexes: pts = np.ndarray((4, 2), buffer=bound.data(), dtype=float).flatten() p = new_points(pts, self_transform, self.transform_matrix[ind], self.avrg) p = p.reshape(4, 2) img_path = os.path.join(DATABASE_LOCATION, '%03d.png' % self.img_ids[ind]) img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) lbp_desc = lbp.calculate_descriptor(img_part) hist_desc = hist.calculate_descriptor(img_part) lbp_diff = np.linalg.norm(orig_lbp_desc - lbp_desc) hist_diff = np.linalg.norm(orig_hist_desc - hist_desc) head, filename = os.path.split(img_path) yield draw_points_on_img(os.path.join( DATABASE_LOCATION, '%03d.png' % self.img_ids[ind]), p, show=False), lbp_diff, hist_diff, filename
def calculate_descriptors(self, img_path): descriptor_template = self._get_descriptor() img = cv2.imread(img_path, 1) mean = img.mean() for w, h in self.cfg.sizes: descriptor = deepcopy(descriptor_template) descriptor.w = w descriptor.h = h calculated_descriptors = [] for i in range(0, img.shape[0] / w): for j in range(0, img.shape[1] / h): img_part = crop_image(w * i, h * j, w * (i + 1), h * (j + 1), img) res = descriptor.calculate_descriptor(img_part, mean=mean) calculated_descriptors.append(res) yield w, h, calculated_descriptors
def run(self): self.running = True img = cv2.imread(self.imageFilename, cv2.IMREAD_GRAYSCALE) if img is None: self.fail_signal.emit('Failed to load image: {:s}'.format( self.imageFilename)) img = crop_image(img) img = cv2.bilateralFilter(img, self.d, self.sigma_color, self.sigma_space) Z = img.astype(np.float32).reshape((-1, 1)) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1.0) ret, label, center = cv2.kmeans(Z, 2, None, criteria, 10, cv2.KMEANS_PP_CENTERS) center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape(img.shape) # Apply closing then opening. p3 = np.array(res2, copy=True) p3[np.where(res2 != min(set(p3.flatten())))] = 255 p3 = cv2.morphologyEx(p3, cv2.MORPH_CLOSE, kernel9) p3 = cv2.morphologyEx(p3, cv2.MORPH_OPEN, kernel9) # Apply closing then opening. p2 = np.array(res2, copy=True) p2[np.where(p2 != min(set(p2.flatten())))] = 255 p2 = cv2.morphologyEx(p2, cv2.MORPH_OPEN, kernel9) p2 = cv2.morphologyEx(p2, cv2.MORPH_CLOSE, kernel3) p2[np.where(p3 != 255)] = 255 seg_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) seg_img[(p2 == 255) * (p3 == 255)] = colors[0] seg_img[p2 != 255] = colors[1] seg_img[p3 != 255] = colors[2] seg_img = cv2.cvtColor(seg_img, cv2.COLOR_BGR2RGB) basename = '.'.join( os.path.basename(self.imageFilename).split('.')[:-1]) self.outputFilename = os.path.join(self.outputPath, basename + '_segmentation.png') if self.running: cv2.imwrite(self.outputFilename, seg_img) self.succeed_signal.emit()
def magic_method(img_path): transform_matrix, nbrs_clf, img_ids, avrg = load_database(200) f = Fix() retMsk, retCorr, sumPts, ptsXY = f.register_mask(img_path) pts = f.pts.flatten() self_transform = avrg - pts _, indexes = nbrs_clf.kneighbors(pts, n_neighbors=5) indexes = indexes[0] draw_points_on_img( img_path, np.ndarray((4, 2), buffer=np.array([64.0, 64, 64, 128, 128, 128, 128, 64]), dtype=float)) lbp = HistDescriptor() for ind in indexes: pts = np.ndarray( (4, 2), buffer=np.array([64.0, 64, 64, 128, 128, 128, 128, 64]), dtype=float).flatten() p = new_points(pts, self_transform, transform_matrix[ind], avrg) p = p.reshape(4, 2) img_path = os.path.join(DATABASE_LOCATION, '%03d.png' % img_ids[ind]) draw_points_on_img(img_path, p) img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) print lbp.calculate_descriptor(img_part) return pathImgMask = "%s_mask.png" % img_path pathImgMasked = "%s_masked.png" % img_path pathImgOnMask = "%s_onmask.png" % img_path pathPtsCSV = "%s_pts.csv" % img_path print f.pts if retCorr > 0.7: cv2.imwrite(pathImgMask, f.newMsk) cv2.imwrite(pathImgMasked, f.newImgMsk) cv2.imwrite(pathImgOnMask, f.newImgOnMsk) np.savetxt(pathPtsCSV, f.pts, delimiter=',', newline='\n', fmt='%0.1f') else: tmpNewImgMsk = cv2.imread(img_path, 1) # cv2.IMREAD_COLOR) tmpImgOnMsk = np.zeros((tmpNewImgMsk.shape[0], tmpNewImgMsk.shape[1]), np.uint8) p00 = (0, 0) p01 = (0, tmpNewImgMsk.shape[0]) p10 = (tmpNewImgMsk.shape[1], 0) p11 = (tmpNewImgMsk.shape[1], tmpNewImgMsk.shape[0]) cv2.line(tmpNewImgMsk, p00, p11, (0, 0, 255), 4) cv2.line(tmpNewImgMsk, p01, p10, (0, 0, 255), 4) f.newMsk[:] = 0 cv2.imwrite(pathImgMask, f.newMsk) cv2.imwrite(pathImgMasked, tmpNewImgMsk) cv2.imwrite(pathImgOnMask, tmpImgOnMsk) tmpPts = np.zeros((7, 2), np.float64) np.savetxt(tmpPts, delimiter=',', newline='\n') fnErr = "%s.err" % img_path f = open(fnErr, 'w') f.close() fzip = "%s.zip" % img_path zObj = zipfile.ZipFile(fzip, 'w') zipDir = '%s_dir' % os.path.basename(img_path) lstFimg = (img_path, pathImgMask, pathImgMasked, pathImgOnMask, pathPtsCSV) for ff in lstFimg: ffbn = os.path.basename(ff) zObj.write(ff, "%s/%s" % (zipDir, ffbn)) print "retCorr = %s" % retCorr
def do_magic_v2(self, img_path, upper, lower, heap_size=3): bound = BoundingBox([ upper[0], upper[1], upper[0], lower[1], lower[0], lower[1], lower[0], upper[1] ]) f = Fix() retMsk, retCorr, sumPts, ptsXY = f.register_mask(img_path) pts = f.pts.flatten() self_transform = self.avrg - pts lbp = LocalBinaryPatternsDescriptor() hist = HistDescriptor() p = np.ndarray((4, 2), buffer=bound.data(), dtype=float) img = cv2.imread(img_path, 1) orig_img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], img) orig_lbp_desc = lbp.calculate_descriptor(orig_img_part) orig_hist_desc = hist.calculate_descriptor(orig_img_part) orig_desc = orig_lbp_desc # select scale area = bound.area() # finding closest scale. we sup closest scale - scale with closest area size scale = min(self.descriptors_db.cfg.sizes, key=lambda x: abs(area - x[0] * x[1])) center = bound.center() descriptor_coordinates = (center / scale).astype(np.int) orig_desc = self.descriptors_db.calculate_one_descriptor( img, scale, descriptor_coordinates[0], descriptor_coordinates[1]) heap = Heap(heap_size) for ind, descriptor_value in self.descriptors_db.get_descriptors( scale, descriptor_coordinates): descriptor_value = np.asarray(descriptor_value) dist = np.linalg.norm(orig_desc - descriptor_value) ind = int(ind.split('.')[0]) heap.push((-dist, ind)) for desc_value, ind in heap.data(): pts = np.ndarray((4, 2), buffer=bound.data(), dtype=float).flatten() trans_ind = self.img_ids.index(ind) p = new_points(pts, self_transform, self.transform_matrix[trans_ind], self.avrg) p = p.reshape(4, 2) img_path = os.path.join(DATABASE_LOCATION, '%03d.png' % self.img_ids[trans_ind]) img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], cv2.imread(img_path, 1)) lbp_desc = lbp.calculate_descriptor(img_part) hist_desc = hist.calculate_descriptor(img_part) lbp_diff = np.linalg.norm(orig_lbp_desc - lbp_desc) hist_diff = np.linalg.norm(orig_hist_desc - hist_desc) head, filename = os.path.split(img_path) yield draw_points_on_img(os.path.join( DATABASE_LOCATION, '%03d.png' % self.img_ids[trans_ind]), p, show=False), lbp_diff, hist_diff, filename
def search(descriptors_db, img_template, upper, lower, heap_size=3): # print upper, lower bound = BoundingBox(map(float, [upper[0], upper[1], upper[0], lower[1], lower[0], lower[1], lower[0], upper[1]])) img_path = img_template.format(IMAGE_TO_USE) lbp = LocalBinaryPatternsDescriptor() hist = HistDescriptor() p = np.ndarray((4, 2), buffer=bound.data(), dtype=float) img = cv2.imread(img_path, 1) orig_img_part = crop_image(p[0][0], p[0][1], p[2][0], p[2][1], img) # orig_lbp_desc = lbp.calculate_descriptor(orig_img_part) # orig_hist_desc = hist.calculate_descriptor(orig_img_part) area = bound.area() # finding closest scale. we sup closest scale - scale with closest area size scale = min(descriptors_db.cfg.sizes, key=lambda x: abs(area - x[0] * x[1])) center = bound.center() descriptor_coordinates = (center / scale).astype(np.int) orig_desc = descriptors_db.calculate_one_descriptor(img, scale, descriptor_coordinates[0], descriptor_coordinates[1]) heap = Heap(heap_size) cor_method = 3 _min2 = [100000000000 for i in xrange(4)] _max2 = [-100000000000 for i in xrange(4)] # _min = [-0.5799584919619982, -12151325.719406169, 0.011764706578105702, -0.9999839563885284] # _max = [1.0, -0.0, 4097.904150336981, -0.0] _min = [-0.7530618036971718, -2386860.0396204507, -2386737.0085002366, -2386737.2483462547] _max = [1.0593303816872124, 1.0593303816872124, 1509.8481579194934, 1509.8481579194934] for ind, descriptor_value, (x_pos, y_pos) in descriptors_db.get_descriptors(scale, descriptor_coordinates, bounding_size=BOUNDING_SIZE): descriptor_value = np.asarray(descriptor_value) adding = 0 # sum(orig_desc[6:]) / 2 # dist = scipy.stats.chisquare(f_exp=[x + adding for x in orig_desc[6:]], # f_obs=[x + adding for x in descriptor_value[6:]]) dist = 0.0 # for i in xrange(3): # cor_method = i # _dist = cv2.compareHist(np.float32(orig_desc), np.float32(descriptor_value), OPENCV_METHODS[cor_method][1]) # _dist *= OPENCV_METHODS[cor_method][2] # dist += _dist / abs((_max[i] - _min[i])) # if dist < _min2[i]: # _min2[i] = dist # if dist > _max2[i]: # _max2[i] = dist cor_method = 3 dist = cv2.compareHist(np.float32(orig_desc), np.float32(descriptor_value), OPENCV_METHODS[cor_method][1]) dist *= OPENCV_METHODS[cor_method][2] try: dist = hist_distance(orig_desc, descriptor_value) * -1 except: continue # dist = sum([- abs(x - y) * abs(x - y) for x, y in zip(descriptor_value, orig_desc)]) # print dist # raise 1 # print dist # dist = dist[0] # abs(sum([w * abs(x - y) for w, (x, y) in zip(weights, zip(orig_desc, descriptor_value))])) # print '2', dist ind = ind.split('.')[0] heap.push((dist, ind, (x_pos, y_pos), descriptor_value)) print _min2, _max2 show_u = tuple(descriptor_coordinates * scale) show_l = tuple(descriptor_coordinates * scale + scale) show(IMAGE_TO_USE, show_u, show_l) for dist, ind, (x_pos, y_pos), descriptor_value in reversed(sorted(heap.data())): # check descriptor is ok img = cv2.imread(img_template.format(ind), 1) descriptor_value2 = descriptors_db.calculate_one_descriptor(img, scale, x_pos, y_pos) s = sum(descriptor_value2 - descriptor_value) # if abs(s) > 0.0001: # raise Exception('wrong descr loaded') show(ind, (x_pos * scale[0], y_pos * scale[1]), ((x_pos + 1) * scale[0], (y_pos + 1) * scale[1])) print dist, ind print ''
def calc(descr, img_path, coords, size=[32, 32]): img = cv2.imread(img_path, 1) x, y = coords x_move, y_move = [_ / 2 for _ in size] part = crop_image(x - x_move, y - y_move, x + x_move, y + y_move, img) return descr.calculate_descriptor(part)