def process_image(image): #grayscaleImage = grayscale(image) blurredImage = gaussian_blur(image, 11) edgesImage = canny(blurredImage, 40, 50) #cv2.imshow('Canny', edgesImage) height = image.shape[0] width = image.shape[1] vertices = np.array( [[[3 * width / 4, 3 * height / 5], [width / 4, 3 * height / 5], [40, height], [width - 40, height]]], dtype=np.int32) regionInterestImage = region_of_interest(edgesImage, vertices) #test_roi = region_of_interest(image, vertices) #cv2.imshow('test_ROI', test_roi) lineMarkedImage = hough_lines(regionInterestImage, 1, np.pi / 180, 40, 30, 200) return weighted_img(lineMarkedImage, image)
def jp_unsharp_mask(image, amount, radius, threshold): sigma = 0.5 copy = image.copy() cv2.imshow("copy", copy) f = cv2.cvtColor(copy, cv2.COLOR_RGB2LAB) fg1 = filters.gaussian_blur(f, 2 * radius + 1, sigma) fg2 = filters.gaussian_blur(fg1, 2 * radius + 1, sigma) (fg1_l, dummy, dummy) = cv2.split(fg1) cv2.imshow("fg1_l", fg1_l) (fg2_l, dummy, dummy) = cv2.split(fg2) cv2.imshow("fg2_l", fg2_l) mask = cv2.absdiff(fg1_l, fg2_l) print 255.0 / mask.max() mask = np.array(mask * (255.0 / mask.max()), dtype="uint8") # mask = np.array(mask * 7, dtype='uint8') (dummy, mask) = cv2.threshold(mask, threshold, 255, cv2.THRESH_TOZERO) cv2.imshow("mask", mask) print mask mask = cv2.merge((mask, mask * 0, mask * 0)) g = cv2.add(f, mask) result = cv2.cvtColor(g, cv2.COLOR_LAB2RGB) return result
def jp_unsharp_mask(image, amount, radius, threshold): sigma = 0.5 copy = image.copy() cv2.imshow('copy', copy) f = cv2.cvtColor(copy, cv2.COLOR_RGB2LAB) fg1 = filters.gaussian_blur(f, 2 * radius + 1, sigma) fg2 = filters.gaussian_blur(fg1, 2 * radius + 1, sigma) (fg1_l, dummy, dummy) = cv2.split(fg1) cv2.imshow('fg1_l', fg1_l) (fg2_l, dummy, dummy) = cv2.split(fg2) cv2.imshow('fg2_l', fg2_l) mask = cv2.absdiff(fg1_l, fg2_l) print 255.0 / mask.max() mask = np.array(mask * (255.0 / mask.max()), dtype='uint8') #mask = np.array(mask * 7, dtype='uint8') (dummy, mask) = cv2.threshold(mask, threshold, 255, cv2.THRESH_TOZERO) cv2.imshow('mask', mask) print mask mask = cv2.merge((mask, mask * 0, mask * 0)) g = cv2.add(f, mask) result = cv2.cvtColor(g, cv2.COLOR_LAB2RGB) return result
def hls_unsharp_mask(image, amount, radius, threshold): sigma = 0.2 copy = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) (h, l, s) = cv2.split(copy) blurred = filters.gaussian_blur(l, 2 * radius + 1, sigma) mask = cv2.absdiff(blurred, l) print 255.0 / mask.max() mask = np.array(mask * (255.0 / mask.max()), dtype="uint8") (dummy, mask) = cv2.threshold(mask, threshold, 255, cv2.THRESH_TOZERO) cv2.imshow("mask", mask) cv2.imshow("l before", l) corrected = filters.simplest_color_balance(l, 10, 10) # corrected = l * (1 + amount) - blurred * amount cv2.imshow("corrected", corrected) l = blend(l, corrected, mask) cv2.imshow("l after", l) copy = cv2.merge((h, l, s)) result = cv2.cvtColor(copy, cv2.COLOR_HLS2RGB) return result
def unsharp_mask(image, amount, radius, threshold): sigma = 0.15 copy = image.copy() cv2.imshow("copy", copy) converted = cv2.cvtColor(copy, cv2.COLOR_RGB2GRAY) blurred = filters.gaussian_blur(converted, 2 * radius + 1, sigma) cv2.imshow("blurred", blurred) # blurred = cv2.blur(converted, tuple([2*radius + 1]*2)) mask = cv2.absdiff(blurred, converted) mask = np.array(mask * (255.0 / mask.max()), dtype="uint8") (dummy, mask) = cv2.threshold(mask, threshold, 255, cv2.THRESH_TOZERO) cv2.imshow("mask", mask) corrected = filters.simplest_color_balance(copy, 15, 15, mask) # corrected = image.copy() * 0 print corrected # cv2.imshow('corrected', corrected) result = blend(copy, corrected, mask) # result = cv2.resize(image, (width, height)) return result
def hls_unsharp_mask(image, amount, radius, threshold): sigma = 0.2 copy = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) (h, l, s) = cv2.split(copy) blurred = filters.gaussian_blur(l, 2 * radius + 1, sigma) mask = cv2.absdiff(blurred, l) print 255.0 / mask.max() mask = np.array(mask * (255.0 / mask.max()), dtype='uint8') (dummy, mask) = cv2.threshold(mask, threshold, 255, cv2.THRESH_TOZERO) cv2.imshow('mask', mask) cv2.imshow('l before', l) corrected = filters.simplest_color_balance(l, 10, 10) #corrected = l * (1 + amount) - blurred * amount cv2.imshow('corrected', corrected) l = blend(l, corrected, mask) cv2.imshow('l after', l) copy = cv2.merge((h, l, s)) result = cv2.cvtColor(copy, cv2.COLOR_HLS2RGB) return result
def unsharp_mask(image, amount, radius, threshold): sigma = 0.15 copy = image.copy() cv2.imshow('copy', copy) converted = cv2.cvtColor(copy, cv2.COLOR_RGB2GRAY) blurred = filters.gaussian_blur(converted, 2 * radius + 1, sigma) cv2.imshow('blurred', blurred) #blurred = cv2.blur(converted, tuple([2*radius + 1]*2)) mask = cv2.absdiff(blurred, converted) mask = np.array(mask * (255.0 / mask.max()), dtype='uint8') (dummy, mask) = cv2.threshold(mask, threshold, 255, cv2.THRESH_TOZERO) cv2.imshow('mask', mask) corrected = filters.simplest_color_balance(copy, 15, 15, mask) #corrected = image.copy() * 0 print corrected #cv2.imshow('corrected', corrected) result = blend(copy, corrected, mask) #result = cv2.resize(image, (width, height)) return result
def bisect_unsharp_mask(image, amount, radius, threshold): sigma = 0.5 copy = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) (h, l, s) = cv2.split(copy) blurred = filters.gaussian_blur(l, 2 * radius + 1, sigma) positive_mask = cv2.addWeighted(l, 1, blurred, -1, 0) positive_mask = np.array(positive_mask * (255.0 / positive_mask.max()), dtype="uint8") negative_mask = cv2.addWeighted(l, -1, blurred, 1, 0) negative_mask = np.array(negative_mask * (255.0 / negative_mask.max()), dtype="uint8") cv2.imshow("positive_mask", positive_mask) cv2.imshow("negative_mask", negative_mask) cv2.imshow("l before", l) positive = l * (1 + amount) # cv2.imshow('positive', positive) l = blend(l, positive, positive_mask) negative = l * (1 - amount) l = blend(l, negative, negative_mask) # cv2.imshow('negative', negative) cv2.imshow("l after", l) copy = cv2.merge((h, l, s)) result = cv2.cvtColor(copy, cv2.COLOR_HLS2RGB) return result
def bisect_unsharp_mask(image, amount, radius, threshold): sigma = 0.5 copy = cv2.cvtColor(image, cv2.COLOR_RGB2HLS) (h, l, s) = cv2.split(copy) blurred = filters.gaussian_blur(l, 2 * radius + 1, sigma) positive_mask = cv2.addWeighted(l, 1, blurred, -1, 0) positive_mask = np.array(positive_mask * (255.0 / positive_mask.max()), dtype='uint8') negative_mask = cv2.addWeighted(l, -1, blurred, 1, 0) negative_mask = np.array(negative_mask * (255.0 / negative_mask.max()), dtype='uint8') cv2.imshow('positive_mask', positive_mask) cv2.imshow('negative_mask', negative_mask) cv2.imshow('l before', l) positive = l * (1 + amount) #cv2.imshow('positive', positive) l = blend(l, positive, positive_mask) negative = l * (1 - amount) l = blend(l, negative, negative_mask) #cv2.imshow('negative', negative) cv2.imshow('l after', l) copy = cv2.merge((h, l, s)) result = cv2.cvtColor(copy, cv2.COLOR_HLS2RGB) return result
def run_analysis(): global label, root, filter_choices input_np = cv.imread(original_image_path, 0) total_filters = 1 sorted_choices = [] # Make a new array of the filters that need to be used for f in filter_choices: if f.use_filter.get(): sorted_choices.append(f) next_plot = 0 total_filters = len(sorted_choices) #Run through filters and apply analysis for i in range(len(sorted_choices)): f = sorted_choices[i] #use if filter being applied if f.use_filter.get(): original_np = [] if f.use_previous.get() and i > 0: original_np = sorted_choices[i - 1].image else: original_np = input_np #calculuate fourier and shifts fourier_np = np.fft.fft2(original_np) shifted_np = np.fft.fftshift(fourier_np) analysis = f.selection.get() #Scaled image radius based on image size scaled_size = (f.param.get() / 200) * original_np.shape[0] if analysis == 'Ideal LPF': #LOW PASS FILTER: Original, fourier, Fourier shift, LPF mask, Inverse graph_np = shifted_np * filters.idealFilterLP( scaled_size, original_np.shape) spatial_image = filters.inverseFFT(np.fft.ifft2(graph_np)) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( np.log(1 + np.abs(fourier_np)), "gray"), plt.title('fourier') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( np.log(1 + np.abs(shifted_np)), "gray"), plt.title('zero shift') plt.subplot(total_filters, 5, next_plot * 5 + 4), plt.imshow( np.log(1 + np.abs(graph_np)), "gray"), plt.title('mask') plt.subplot(total_filters, 5, next_plot * 5 + 5), plt.imshow( spatial_image, "gray"), plt.title('LPF') f.image = spatial_image elif analysis == 'Ideal HPF': #HIGH PASS FILTER: Original, fourier, Fourier shift, HPF mask, Inverse graph_np = shifted_np * filters.idealFilterHP( scaled_size, original_np.shape) spatial_image = filters.inverseFFT(np.fft.ifft2(graph_np)) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( np.log(1 + np.abs(fourier_np)), "gray"), plt.title('fourier') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( np.log(1 + np.abs(shifted_np)), "gray"), plt.title('zero shift') plt.subplot(total_filters, 5, next_plot * 5 + 4), plt.imshow( np.log(1 + np.abs(graph_np)), "gray"), plt.title('mask') plt.subplot(total_filters, 5, next_plot * 5 + 5), plt.imshow( spatial_image, "gray"), plt.title('HPF') f.image = spatial_image elif analysis == 'Gaussian LPF': #Gaussian LPF: Original, fourier, Fourier Shift, Mask, Inverse graph_np = shifted_np * filters.gaussianLP( scaled_size, original_np.shape) spatial_image = filters.inverseFFT(np.fft.ifft2(graph_np)) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( np.log(1 + np.abs(fourier_np)), "gray"), plt.title('fourier') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( np.log(1 + np.abs(shifted_np)), "gray"), plt.title('zero shift') plt.subplot(total_filters, 5, next_plot * 5 + 4), plt.imshow( np.log(1 + np.abs(graph_np)), "gray"), plt.title('mask') plt.subplot(total_filters, 5, next_plot * 5 + 5), plt.imshow( spatial_image, "gray"), plt.title('Gaussian LPF') f.image = spatial_image elif analysis == 'Gaussian HPF': #Gaussian HPF: Original, fourier, Fourier Shift, Mask, Inverse graph_np = shifted_np * filters.gaussianHP( scaled_size, original_np.shape) spatial_image = filters.inverseFFT(np.fft.ifft2(graph_np)) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( np.log(1 + np.abs(fourier_np)), "gray"), plt.title('fourier') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( np.log(1 + np.abs(shifted_np)), "gray"), plt.title('zero shift') plt.subplot(total_filters, 5, next_plot * 5 + 4), plt.imshow( np.log(1 + np.abs(graph_np)), "gray"), plt.title('mask') plt.subplot(total_filters, 5, next_plot * 5 + 5), plt.imshow( spatial_image, "gray"), plt.title('Gaussian HPF') f.image = spatial_image elif analysis == 'Fourier': #Fourier plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( np.log(1 + np.abs(fourier_np)), "gray"), plt.title('fourier') f.image = original_np elif analysis == 'Fourier Zero Shifted': #Fourier, zero shift plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( np.log(1 + np.abs(fourier_np)), "gray"), plt.title('fourier') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( np.log(1 + np.abs(shifted_np)), "gray"), plt.title('zero shift') f.image = original_np elif analysis == 'Intensity Inverse': #Transformation => T(x) = 255 - f transformed = np.copy(original_np) for i in range(original_np.shape[0]): for j in range(original_np.shape[1]): transformed[i][j] = 255 - original_np[i][j] #Transform graph x = np.array(range(0, 255)) y = eval('255-x') plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.plot( x, y), plt.title('pixel transform') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( transformed, "gray"), plt.title('inverse') f.image = transformed elif analysis == 'Intensity Quantize': #number of bits to quantize levels = int(f.param.get()) transformed = np.copy(original_np) delta = 256 / levels for i in range(original_np.shape[0]): for j in range(original_np.shape[1]): val = original_np[i][j] #print(val) transformed[i][j] = math.floor((val / delta) + 0.5) * delta #transform graph x = np.array(range(0, 255)) y = np.floor((x / delta) + 0.5) * delta plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.plot( x, y), plt.title('pixel transform') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( transformed, "gray"), plt.title('quantized') f.image = transformed elif analysis == 'Sobel Gradient Combined': x_img, y_img = filters.sobel_edge(original_np) combined = np.array(np.sqrt((x_img**2) + (y_img**2)), dtype='uint8') plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( x_img, "gray"), plt.title('sobel x gradient') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.imshow( y_img, "gray"), plt.title('sobel y gradient') plt.subplot(total_filters, 5, next_plot * 5 + 4), plt.imshow( combined, "gray"), plt.title('combined') f.image = combined elif analysis == 'Sobel Horizontal Gradient': x_img, _ = filters.sobel_edge(original_np) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( x_img, "gray"), plt.title('sobel x gradient') f.image = x_img elif analysis == 'Sobel Vertical Gradient': _, y_img = filters.sobel_edge(original_np) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( y_img, "gray"), plt.title('sobel y gradient') f.image = y_img elif analysis == 'Laplacian': laplace = filters.laplace_filter(original_np) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( laplace, "gray"), plt.title('laplace edge') f.image = laplace elif analysis == 'Gaussian Blur': sigma = int(f.param.get()) blurred = filters.gaussian_blur(original_np, sigma) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( blurred, "gray"), plt.title('gaussian blur') f.image = blurred elif analysis == 'Histogram Equalization': normalized, histogram, _, _ = filters.histogram_equalization( original_np, 256) new_histogram, _ = np.histogram(normalized.flatten(), 256, density=True) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot( total_filters, 5, next_plot * 5 + 2), plt.hist(histogram), plt.title('original histogram') plt.subplot(total_filters, 5, next_plot * 5 + 3), plt.hist( new_histogram), plt.title('normailized histogram') plt.subplot(total_filters, 5, next_plot * 5 + 4), plt.imshow( normalized, "gray"), plt.title('normalized') f.image = normalized elif analysis == 'Flat Filter': flat_fitered = filters.flat_filter(original_np) plt.subplot(total_filters, 5, next_plot * 5 + 1), plt.imshow( original_np, "gray"), plt.title('original') plt.subplot(total_filters, 5, next_plot * 5 + 2), plt.imshow( flat_fitered, "gray"), plt.title('flat_filtered 3x3') f.image = flat_fitered next_plot = next_plot + 1 plt.show()
def blur_processing(gray_img, tam_kernel = 32): blur = flt.gaussian_blur(tam_kernel) return ndimage.convolve(gray_img, blur)