def my_canny(img, fn = None, sigma=6, with_mask=False, save=False, show=False): height = img.shape[0] width = img.shape[1] if with_mask: import numpy as np mymask = np.zeros((height, width),'uint8') y1, x1 = 200, 150 y2, x2 = 500, 350 mymask[y1: y2, x1: x2] = 1 ret = canny(img, sigma=sigma, mask=mymask) else: ret = canny(img, sigma) if show: from src.utils.io import showimage_pil showimage_pil(255*ret.astype('uint8')) if save: from src.utils.io import saveimage_pil if with_mask: feature = '_sigma' + str(sigma) + '_mask' else: feature = '_sigma' + str(sigma) saveimage_pil(255*ret.astype('uint8'), fn+feature+'.jpg',show=False) return ret
def save_all_sliding_windows(foldername, arr, step_size_W=30, step_size_H=30, winW=400, winH=400): j = 1 from src.utils.io import saveimage_pil for y in xrange(0, arr.shape[0], step_size_H): for x in xrange(0, arr.shape[1], step_size_W): if arr.dtype != 'uint8': a = (255*arr[y:y+winH, x:x+winW]).astype(np.uint8) else: a = arr[y:y+winH, x:x+winW] if a.shape[0] != winH or a.shape[1] != winW: continue saveimage_pil(a, foldername+str(j)+'.jpg') j += 1 return j
def generate_blur_canny_subfolders(folder, outputFolder): from src.utils.canny import my_canny from src.utils.canny import best_edges from src.utils.io import filename2arr from src.utils.io import saveimage_pil import os for dirName, subdirList, fileList in os.walk(folder): for subdir in subdirList: for dirName2, subdirList2, fileList in os.walk(dirName + '/' +subdir): for filename in fileList: parts = filename.split('.') if parts[0] == '': continue arr = filename2arr(dirName2 + '/' + filename) ret = best_edges(arr) if not os.path.exists(outputFolder + '/' + subdir): os.makedirs(outputFolder + '/' + subdir) outfn = outputFolder + '/' + subdir + '/'+ parts[0] + '.jpg' print "saving canny file " + outfn saveimage_pil(255 * ret.astype('uint8'), outfn, show=False)
def Harris_Corner(arr, show=False, save=False, fn=None): gray = np.float32(arr) dst = cv2.cornerHarris(gray, 2, 3, 0.04) dst = cv2.dilate(dst, None) ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0) dst = np.uint8(dst) ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001) corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria) res = np.hstack((centroids, corners)) res = np.int0(res) arr[res[:, 1], res[:, 0]] = 255 if show is True: showimage_pil(arr) if save is True: if fn is None: return else: saveimage_pil(arr, fn)
def HOG(arr, show=False, save=False, fn=None): from src.utils.util import normalize_array from skimage.feature import hog fd, hog_image = hog( arr, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=True, normalise=True ) if show is True: # from skimage import exposure # hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 1)) # showimage_pil(hog_image_rescaled) hog_image = normalize_array(hog_image, low=0.2, high=1) showimage_pil(hog_image) if save is True: if fn is None: print "filename in src.utils.features.HOG is None" return hog_image # from skimage import exposure # hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 1)) # saveimage_pil(hog_image_rescaled) saveimage_pil(hog_image) return fd
from src.utils.util import scale arr = scale(arr, sh, sw) showimage_pil(arr) # from src.utils.gabor import gabor_feature_real tao = 4. elongation = 8. sigmax = tao/2.35 sigmay = sigmax * elongation max_orientation, max_magnitude, max_frequency = gabor_feature_real(arr, sigmax=sigmax, sigmay=sigmay) from src.utils.gabor import normalize normal_gabor = normalize(max_magnitude) fn = '/Users/ruhansa/Desktop/4.jpg' saveimage_pil(normal_gabor, fn) arr = filename2arr(fn) from src.utils.canny import decide_sigma sigma = 0.0 for i in range(5): print "sigma: " + str(sigma) ret = my_canny(arr, sigma=sigma, save=False, show=True) s = decide_sigma(ret, ret.size) if s is False: sigma += 0.5 else: break