def execute_insertion(sql, values): """Runs an insertion query and returns the result :param sql: sql query :param value: values for the query :returns: True """ with closing(mysql.connector.connect(**connetion_params)) as db: with closing(db.cursor(dictionary=True, buffered=True)) as cursor: cursor.execute(sql, values) db.commit() return True
def execute_selection(sql, values=None): """Runs a select query and returns the result :param sql: sql query :param value: values for the query :returns: query result """ with closing(mysql.connector.connect(**connetion_params)) as db: with closing(db.cursor(dictionary=True, buffered=True)) as cursor: if values == None: cursor.execute(sql) else: cursor.execute(sql, [values]) data = parse_result(cursor) return data
def ref_region( img: np.ndarray, selem: Any = disk(5), sigma: int = 3, opening_se: np.ndarray = np.ones((10, 10)), closing_se: np.ndarray = np.ones((5, 5)), verbose: bool = False ): """ """ # Perform histogram equalisation : _img_eq = rank.equalize(img, selem=selem) # Perform edge detection : _edges = canny(_img_eq, sigma=3) _filled = ndi.binary_fill_holes(_edges) # Morphological processing : _eroded = utils.closing( utils.opening(np.float64(_filled), opening_se), closing_se ) if verbose: utils.side_by_side(img, _img_eq, title1="Original", title2="Histogram Equalised") #plt.title('Lol') utils.side_by_side(_img_eq, _filled, title1="Histogram Equalised", title2="Canny Edge Detection + Filled image") #plt.title('Lal') utils.side_by_side(_filled, _eroded, title1="Canny Edge Detection + Filled image", title2="Opening, closing") #plt.title('Lel') return _eroded
def closing_mask(m, img=None): # we give some margin marg = 200 aux = np.zeros((m.shape[0] + marg*2, m.shape[1] + marg*2)) aux[marg:marg+m.shape[0], marg:marg+m.shape[1]] = m # relative to the shape of the mask to compute closing val = min(m.shape) size = (min(int(0.1 * val), 100), min(int(0.1*val), 100)) aux = closing(aux, size=size) return aux[marg:marg+m.shape[0], marg:marg+m.shape[1]], None
def darkText(img): """ Generates the textboxes candidated based on BLACKHAT morphological filter. Works well with dark text over bright background. Parameters ---------- img : ndimage to process Returns ------- mask: uint8 mask with regions of interest (possible textbox candidates) """ kernel = np.ones((30, 30), np.uint8) img_orig = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel) TH = 150 img_orig[(img_orig[:,:,0] < TH) | (img_orig[:,:,1] < TH) | (img_orig[:,:,2] < TH)] = (0,0,0) img_orig = closing(img_orig, size=(1, int(img.shape[1] / 8))) return (cv2.cvtColor(img_orig, cv2.COLOR_BGR2GRAY) != 0).astype(np.uint8)
def main(): img = cv2.imread('lena.bmp', 0) # img is now a 512 x 512 numpy.ndarray if not os.path.exists('Task-1'): os.makedirs('Task-1') if not os.path.exists('Task-2'): os.makedirs('Task-2') if not os.path.exists('Task-3'): os.makedirs('Task-3') if not os.path.exists('Task-4'): os.makedirs('Task-4') if not os.path.exists('Task-5'): os.makedirs('Task-5') # Generate and output an image with # additive white Gaussian noise # with amplitude = 10 img_gauss_10 = generate_Gaussian_noise(img, 0, 1, 10) cv2.imwrite('Task-1/lena.gaussian.10.bmp', img_gauss_10) # Generate and output an image with # additive white Gaussian noise # with amplitude = 30 img_gauss_30 = generate_Gaussian_noise(img, 0, 1, 30) cv2.imwrite('Task-1/lena.gaussian.30.bmp', img_gauss_30) # Generate and output an image with # salt-and-pepper noise with # threshold = 0.05 img_sp_05 = generate_salt_and_pepper_noise(img, 0, 1, 0.05) cv2.imwrite('Task-2/lena.sp.05.bmp', img_sp_05) # Generate and output an image with # salt-and-pepper noise with # threshold = 0.1 img_sp_10 = generate_salt_and_pepper_noise(img, 0, 1, 0.1) cv2.imwrite('Task-2/lena.sp.10.bmp', img_sp_10) # Run 3x3 box filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_box_3 = box_filter(img_gauss_10, 3) cv2.imwrite('Task-3/lena.gaussian.10.box.3x3.bmp', img_gauss_10_box_3) # Run 3x3 box filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_box_3 = box_filter(img_gauss_30, 3) cv2.imwrite('Task-3/lena.gaussian.30.box.3x3.bmp', img_gauss_30_box_3) # Run 3x3 box filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_box_3 = box_filter(img_sp_05, 3) cv2.imwrite('Task-3/lena.sp.05.box.3x3.bmp', img_sp_05_box_3) # Run 3x3 box filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_box_3 = box_filter(img_sp_10, 3) cv2.imwrite('Task-3/lena.sp.10.box.3x3.bmp', img_sp_10_box_3) # Run 5x5 box filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_box_5 = box_filter(img_gauss_10, 5) cv2.imwrite('Task-3/lena.gaussian.10.box.5x5.bmp', img_gauss_10_box_5) # Run 5x5 box filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_box_5 = box_filter(img_gauss_30, 5) cv2.imwrite('Task-3/lena.gaussian.30.box.5x5.bmp', img_gauss_30_box_5) # Run 5x5 box filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_box_5 = box_filter(img_sp_05, 5) cv2.imwrite('Task-3/lena.sp.05.box.5x5.bmp', img_sp_05_box_5) # Run 5x5 box filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_box_5 = box_filter(img_sp_10, 5) cv2.imwrite('Task-3/lena.sp.10.box.5x5.bmp', img_sp_10_box_5) # Run 3x3 median filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_med_3 = median_filter(img_gauss_10, 3) cv2.imwrite('Task-4/lena.gaussian.10.median.3x3.bmp', img_gauss_10_med_3) # Run 3x3 median filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_med_3 = median_filter(img_gauss_30, 3) cv2.imwrite('Task-4/lena.gaussian.30.median.3x3.bmp', img_gauss_30_med_3) # Run 3x3 median filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_med_3 = median_filter(img_sp_05, 3) cv2.imwrite('Task-4/lena.sp.05.median.3x3.bmp', img_sp_05_med_3) # Run 3x3 median filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_med_3 = median_filter(img_sp_10, 3) cv2.imwrite('Task-4/lena.sp.10.median.3x3.bmp', img_sp_10_med_3) # Run 5x5 median filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_med_5 = median_filter(img_gauss_10, 5) cv2.imwrite('Task-4/lena.gaussian.10.median.5x5.bmp', img_gauss_10_med_5) # Run 5x5 median filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_med_5 = median_filter(img_gauss_30, 5) cv2.imwrite('Task-4/lena.gaussian.30.median.5x5.bmp', img_gauss_30_med_5) # Run 5x5 median filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_med_5 = median_filter(img_sp_05, 5) cv2.imwrite('Task-4/lena.sp.05.median.5x5.bmp', img_sp_05_med_5) # Run 5x5 median filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_med_5 = median_filter(img_sp_10, 5) cv2.imwrite('Task-4/lena.sp.10.median.5x5.bmp', img_sp_10_med_5) # Use octagon as kernel and set the orgin is at the center kernel = [ [-2, -1], [-2, 0], [-2, 1], [-1, -2], [-1, -1], [-1, 0], [-1, 1], [-1, 2], [0, -2], [0, -1], [0, 0], [0, 1], [0, 2], [1, -2], [1, -1], [1, 0], [1, 1], [1, 2], [2, -1], [2, 0], [2, 1] ] # closing followed by opening on all noisy images img_gauss_10_close_open = opening(closing(img_gauss_10, kernel), kernel) img_gauss_30_close_open = opening(closing(img_gauss_30, kernel), kernel) img_sp_05_close_open = opening(closing(img_sp_05, kernel), kernel) img_sp_10_close_open = opening(closing(img_sp_10, kernel), kernel) cv2.imwrite('Task-5/lena.gaussian.10.close.open.bmp', img_gauss_10_close_open) cv2.imwrite('Task-5/lena.gaussian.30.close.open.bmp', img_gauss_30_close_open) cv2.imwrite('Task-5/lena.sp.05.close.open.bmp', img_sp_05_close_open) cv2.imwrite('Task-5/lena.sp.10.close.open.bmp', img_sp_10_close_open) # opening followed by closing on all noisy images img_gauss_10_open_close = closing(opening(img_gauss_10, kernel), kernel) img_gauss_30_open_close = closing(opening(img_gauss_30, kernel), kernel) img_sp_05_open_close = closing(opening(img_sp_05, kernel), kernel) img_sp_10_open_close = closing(opening(img_sp_10, kernel), kernel) cv2.imwrite('Task-5/lena.gaussian.10.open.close.bmp', img_gauss_10_open_close) cv2.imwrite('Task-5/lena.gaussian.30.open.close.bmp', img_gauss_30_open_close) cv2.imwrite('Task-5/lena.sp.05.open.close.bmp', img_sp_05_open_close) cv2.imwrite('Task-5/lena.sp.10.open.close.bmp', img_sp_10_open_close)
def closing_mask(m): # relative to the shape of the mask to compute closing val = min(m.shape) size = (min(int(0.1 * val), 100), min(int(0.1 * val), 100)) return closing(m, size=size)
kernel = np.ones((50, 2)) side_by_side(binaria, cv.morphologyEx(binaria, cv.MORPH_OPEN, kernel), title1='Original', title2=f'cv.MORPH_OPEN with Kernel {kernel.shape}') # As Gonzalez explained in the book, an opening is nothing but an erosion followed by a dilation. # Our custom function ```opening(image, kernel)``` yields the same result as executing ```cv.morphologyEx(image, cv.MORPH_OPEN, kernel)``` # # Closing # In[17]: kernel = np.ones((10, 10)) side_by_side(binaria, closing(binaria, kernel), title1='Original', title2=f'closing() with Kernel {kernel.shape}') # In[18]: kernel = np.ones((10, 10)) side_by_side(binaria, cv.morphologyEx(binaria, cv.MORPH_CLOSE, kernel), title1='Original', title2=f'cv.MORPH_CLOSE with Kernel {kernel.shape}') # In[19]: kernel = np.ones((2, 50)) side_by_side(binaria,