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 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
# fn = '/Users/ruhansa/Desktop/train/positive/flat/4/527.jpg' img = Image.open(fn) img.show() cur_arr_3 = np.array(img.getdata(), dtype=np.uint8).reshape(img.size[1], img.size[0], -1) cur_arr = cur_arr_3[:, :, 0] from src.utils import preprocessing import cv2 from src.utils.canny import decide_sigma for sigma in [5]: for neighbor in [30]: blur = cv2.bilateralFilter(cur_arr.copy(), neighbor, sigma, sigma) showimage_pil(blur) cur_arr = blur sigma = 0.0 r = 1 threshold = 0.025 while r > threshold: print "sigma: " + str(sigma) ret = my_canny(cur_arr, sigma=sigma, save=False, show=False) s, r = decide_sigma(ret, ret.size, threshold=threshold,show=True) if s is False: sigma += 0.5 else: if sigma == 0.0 and r < 0.01: sharpen = preprocessing.sharpen(blur, sigma=2) cur_arr= sharpen showimage_pil(sharpen)
import os, sys, inspect from src.utils.io import showimage_pil, filename2arr tests_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory src/ src_dir = os.path.dirname(tests_dir) xray_dir = os.path.dirname(src_dir) # xray directory os.chdir(xray_dir) sys.path.append(src_dir) fn = xray_dir + "/data/LL/1.jpg" arr = filename2arr(fn) ratio_w = 300 / 450.0 ratio_h = 200 / 376.0 sw = arr.shape[1] * ratio_w sh = arr.shape[0] * ratio_h from src.utils.util import scale arr_new = scale(arr, sh, sw) showimage_pil(arr) showimage_pil(arr_new)
halves = non_max_suppression_merge(halves, overlapThresh=0) if halves is not None: # 1. detect ROI bbox = get_huge_bounding_box(halves) img1 = Image.fromarray(arr) # fn = result_dir + '/' + testn + '_half_boxes.pkl' # pickle.dump(halves, open(fn, "wb")) img1=add_boxes(halves, img1) img1.show() img1.save(result_dir +'/' + testn +'_half_boxes.jpg') # 2. detect edges ROI = arr[bbox[1]: bbox[3], bbox[0]: bbox[2]] edges, sigma, cur_ROI= best_edges(ROI, threshold=0.02) showimage_pil(edges) # ## 3. using hough_transform to find initial horizontal line # # from src.utils.hough import hough_horizontal # fn = result_dir + '/' + testn + 'canny_hough' # img2 = Image.fromarray(ROI) # lines_init, _ = hough_horizontal(edges, fn, hough_line_len=30, line_gap=40, save=True, show=True, raw=img2, xdiff=0, ydiff=0) # # # ## 4. Extend line using smaller sigma # from src.utils.util import extend_line_canny_points # from src.utils.canny import my_canny # fn = fn = result_dir + '/' + testn + 'canny_extend' # showimage_pil(cur_ROI)
xray_dir = os.path.dirname(src_dir) #xray directory os.chdir(xray_dir) sys.path.append(src_dir) """ ############################### set input/output path ############################### """ from src.utils import features from src.utils import preprocessing lns = ['L3'] test_n = 1 max_test=1 for ln in lns: input_dir = xray_dir + '/data/train/' + str(ln) for dirName, subdirList, fileList in os.walk(input_dir): for filename in fileList: parts = filename.split('.') if parts[0] == '': continue if parts[1] == 'jpg' and test_n<= max_test: test_n += 1 img = Image.open(dirName + '/'+ filename) arr_3 = np.array(img.getdata(), dtype=np.uint8).reshape(img.size[1], img.size[0], 3) arr = arr_3[:, :, 0] # arr = preprocessing.normalize(arr) # arr = preprocessing.sharpen(arr) arr = preprocessing.deskew(arr) showimage_pil(arr)
def drawMatches(img1, kp1, img2, kp2, matches): """ My own implementation of cv2.drawMatches as OpenCV 2.4.9 does not have this function available but it's supported in OpenCV 3.0.0 This function takes in two images with their associated keypoints, as well as a list of DMatch data structure (matches) that contains which keypoints matched in which images. An image will be produced where a montage is shown with the first image followed by the second image beside it. Keypoints are delineated with circles, while lines are connected between matching keypoints. img1,img2 - Grayscale images kp1,kp2 - Detected list of keypoints through any of the OpenCV keypoint detection algorithms matches - A list of matches of corresponding keypoints through any OpenCV keypoint matching algorithm """ # Create a new output image that concatenates the two images together # (a.k.a) a montage rows1 = img1.shape[0] cols1 = img1.shape[1] rows2 = img2.shape[0] cols2 = img2.shape[1] out = np.zeros((max([rows1, rows2]), cols1 + cols2, 3), dtype="uint8") # Place the first image to the left out[:rows1, :cols1, :] = np.dstack([img1, img1, img1]) # Place the next image to the right of it out[:rows2, cols1 : cols1 + cols2, :] = np.dstack([img2, img2, img2]) # For each pair of points we have between both images # draw circles, then connect a line between them for mat in matches: # Get the matching keypoints for each of the images img1_idx = mat.queryIdx img2_idx = mat.trainIdx # x - columns # y - rows (x1, y1) = kp1[img1_idx].pt (x2, y2) = kp2[img2_idx].pt # Draw a small circle at both co-ordinates # radius 4 # colour blue # thickness = 1 cv2.circle(out, (int(x1), int(y1)), 4, (255, 0, 0), 1) cv2.circle(out, (int(x2) + cols1, int(y2)), 4, (255, 0, 0), 1) # Draw a line in between the two points # thickness = 1 # colour blue cv2.line(out, (int(x1), int(y1)), (int(x2) + cols1, int(y2)), (255, 0, 0), 1) # Show the image from src.utils.io import showimage_pil showimage_pil(out)