def get_filtered_CSF_img(img_in): img_dft = cv2.dft(np.float32(img_in), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(img_dft) height = img_dft.shape[0] weight = img_dft.shape[1] M = weight / 2 N = height / 2 H_matrix = np.zeros((height, weight)) for h_idx in range(height): for w_idx in range(weight): m = -M + w_idx + 0.5 n = -N + h_idx + 0.5 freq, theta = get_freq_dirc(m, n, weight, height) multiVal = freq_trans_func(freq, theta) H_matrix[h_idx][w_idx] = multiVal img_magi = cv2.magnitude(img_dft[:, :, 0], img_dft[:, :, 1]) img_magi *= H_matrix img_phase = cv2.phase(img_dft[:, :, 0], img_dft[:, :, 1]) img_re = img_magi * np.cos(img_phase) img_im = img_magi * (np.sin(img_phase)) img_dft2 = np.dstack((img_re, img_im)) imgback = cv2.idft(img_dft2) imgback = cv2.magnitude(imgback[:, :, 0], imgback[:, :, 1]) return imgback
def doDFT(): # read as gray image. img = cv2.imread('1.jpg', 0) # cv2.imwrite('gray.jpg', img) # t1 = dct(dct(img.T, norm='ortho').T, norm='ortho') dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) rows, cols = img.shape crow,ccol = rows/2 , cols/2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows,cols,2),np.uint8) mask[crow-5:crow+5, ccol-5:ccol+5] = 1 # apply mask and inverse DFT fshift = dft_shift*mask print fshift[:, :, 0] print fshift[:, :, 0].shape f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) plt.subplot(121),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(img_back, cmap = 'gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() return
def test_dft(self): img = self.get_sample('samples/data/rubberwhale1.png', 0) eps = 0.001 #test direct transform refDft = np.fft.fft2(img) refDftShift = np.fft.fftshift(refDft) refMagnitide = np.log(1.0 + np.abs(refDftShift)) testDft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) testDftShift = np.fft.fftshift(testDft) testMagnitude = np.log(1.0 + cv2.magnitude(testDftShift[:,:,0], testDftShift[:,:,1])) refMagnitide = cv2.normalize(refMagnitide, 0.0, 1.0, cv2.NORM_MINMAX) testMagnitude = cv2.normalize(testMagnitude, 0.0, 1.0, cv2.NORM_MINMAX) self.assertLess(cv2.norm(refMagnitide - testMagnitude), eps) #test inverse transform img_back = np.fft.ifft2(refDft) img_back = np.abs(img_back) img_backTest = cv2.idft(testDft) img_backTest = cv2.magnitude(img_backTest[:,:,0], img_backTest[:,:,1]) img_backTest = cv2.normalize(img_backTest, 0.0, 1.0, cv2.NORM_MINMAX) img_back = cv2.normalize(img_back, 0.0, 1.0, cv2.NORM_MINMAX) self.assertLess(cv2.norm(img_back - img_backTest), eps)
def fft(img,x,y,w,h): rows, cols = img.shape crow,ccol = rows/2 , cols/2 dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) mask = np.zeros((rows,cols,2),np.uint8) mask[crow-x:crow+w, ccol-y:ccol+h] = 1 mag_mask = copy.copy(magnitude_spectrum) mag_mask[crow-x:crow+w, ccol-y:ccol+h] = 0 fshift = dft_shift*mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) plt.subplot(121),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() plt.subplot(121),plt.imshow(img_back, cmap = 'gray') plt.title('Output Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(mag_mask, cmap = 'gray') plt.title('Cut Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()
def inverse_fourier_transform(spectral_image): """ Method computes the inverse Fourier transform of a given image """ f_ishift = np.fft.ifftshift(spectral_image) img_back = cv2.idft(f_ishift) return cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
def measure_blurriness_DFT(img): """ More complex blurriness measure averaging top 90% of frequencies in image """ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur_img = cv2.GaussianBlur(img, (3, 3), 0) dftHeight = cv2.getOptimalDFTSize(blur_img.shape[0]) dftWidth = cv2.getOptimalDFTSize(blur_img.shape[1]) complexImg = np.zeros([dftHeight, dftWidth, 2], dtype=float) complexImg[0:img.shape[0], 0:img.shape[1], 0] = img / 255.0 dft_img = cv2.dft(complexImg) dft_img = cv2.magnitude(dft_img[:, :, 0], dft_img[:, :, 1]) dft_img = cv2.log(dft_img + 1) cv2.normalize(dft_img, dft_img, 0, 1, cv2.NORM_MINMAX) dft_img_h, dft_img_w = dft_img.shape[:2] win_size = dft_img_w * 0.55 window = dft_img[dft_img_h / 2 - win_size:dft_img_h / 2 + win_size, dft_img_w / 2 - win_size:dft_img_w / 2 + win_size] return np.mean(np.abs(window))
def phaseCorrelation(img_orig, img_transformed): # Step 3.1 - Initialize complex conjugates for original image and magnitudes orig_conj = np.copy(img_orig) orig_conj[:,:,IM_IDX] = -orig_conj[:,:,IM_IDX] orig_mags = cv2.magnitude(img_orig[:,:,RE_IDX],img_orig[:,:,IM_IDX]) img_trans_mags = cv2.magnitude(img_transformed[:,:,RE_IDX],img_transformed[:,:,IM_IDX]) # Step 3.2 - Do deconvolution # multiplication compex numbers ===> (x + yi) * (u + vi) = (xu - yv) + (xv + yu)i # deconvolution ( H* x G ) / |H x G| realPart = (orig_conj[:,:,RE_IDX] * img_transformed[:,:,RE_IDX] - orig_conj[:,:,IM_IDX] * img_transformed[:,:,IM_IDX]) / (orig_mags * img_trans_mags) imaginaryPart = (orig_conj[:,:,RE_IDX] * img_transformed[:,:,IM_IDX] + orig_conj[:,:,IM_IDX] * img_transformed[:,:,RE_IDX]) / ( orig_mags * img_trans_mags) result = np.dstack((realPart, imaginaryPart)) result_idft = cv2.idft(result) # Step 3.3 - Find Max value (angle and scaling factor) result_mags = cv2.magnitude(result_idft[:,:,RE_IDX],result_idft[:,:,IM_IDX]) return np.unravel_index( np.argmax(result_mags), result_mags.shape)
def maxdouble_sobel(image): gx = cv2.Sobel(image, cv2.CV_32F, 1, 0) gy = cv2.Sobel(image, cv2.CV_32F, 0, 1) #dnorm = numpy.sqrt(gx**2 + gy**2) dm = cv2.magnitude(gx, gy) return numpy.average(dm)
def get_center_map(eye_img): f_x, f_y, f_xy, f_xx, f_yy = get_gradients(eye_img) # Calculate the curved-ness and weighting function curvedness = np.sqrt(f_xx ** 2 + 2 * f_xy ** 2 + f_yy ** 2) curvedness_norm = cv2.normalize(curvedness, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8) cv2.imshow("curvedness", curvedness_norm) weight_edge = cv2.normalize(curvedness, 0, 255 * __weight_ratio_edge, norm_type=cv2.NORM_MINMAX) # higher weight to stronger edges weight_middle = cv2.normalize((255 - eye_img), 0, 255 * __weight_ratio_darkness, norm_type=cv2.NORM_MINMAX) # higher center weight to darker areas # Calculate the displacement vectors temp_top = f_x ** 2 + f_y ** 2 temp_bot = ((f_y ** 2) * f_xx) - (2 * f_x * f_xy * f_y) + ((f_x ** 2) * f_yy) + 0.0001 # hack to offset against division by 0 d_vec_mul = -temp_top / temp_bot d_vec_x = d_vec_mul * f_x d_vec_y = d_vec_mul * f_y # Remove infinite displacements for straight lines d_vec_x = np.nan_to_num(d_vec_x) d_vec_y = np.nan_to_num(d_vec_y) mag_d_vec = cv2.magnitude(d_vec_x, d_vec_y) # Prevent using weights with bad radius sizes weight_edge[mag_d_vec < __min_rad] = 0 weight_edge[mag_d_vec > __max_rad] = 0 # Calculate curvature to ensure we use gradients which point in right direction curvature = temp_bot / (0.0001 + (temp_top ** 1.5)) weight_edge[curvature < 0] = 0 weight_edge[curvedness_norm < 20] = 0 # Make indexes into accumulator using basic grid and vector offsets grid = np.indices(eye_img.shape[:2], np.uint8) acc_inds_x = grid[1] + d_vec_x.astype(int) acc_inds_y = grid[0] + d_vec_y.astype(int) # Prevent indexing outside of accumulator acc_inds_x[acc_inds_x < 0] = 0 acc_inds_y[acc_inds_y < 0] = 0 acc_inds_x[acc_inds_x >= eye_img.shape[1]] = 0 acc_inds_y[acc_inds_y >= eye_img.shape[0]] = 0 # Make center map accumulator accumulator = np.zeros(eye_img.shape[:2]) # Use numpy fancy indexing to add weights in one go # (This is fast as it avoids python loops) accumulator[acc_inds_y, acc_inds_x] += weight_edge accumulator += weight_middle # Post-processing morph_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) accumulator = cv2.morphologyEx(accumulator, cv2.MORPH_DILATE, morph_kernel) # accumulator = cv2.blur(accumulator, (10, 10)) accumulator = cv2.GaussianBlur(accumulator, (13, 13), 0) return accumulator
def main(): displayer = Displayer() orig = cv2.cvtColor(fetch_image(sys.argv[1]), cv2.COLOR_BGR2GRAY) displayer.add_image(orig, 'Original') img = cv2.bilateralFilter(np.float32(orig), 9, 75, 75) x_kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) y_kernel = np.array([[ 1, 2, 1], [ 0, 0, 0], [-1, -2, -1]]) """ x_kernel = np.array([[3, 10, 3], [0, 0, 0], [-3, -10, -3]]) y_kernel = np.array([[ -3, -0, 3], [ -10, 0, 10], [ -3, -0, 3]]) """ """ y_kernel = x_kernel = np.array([[ 0.5, 1, 0.5], [ 1, -6, 1], [ 0.5, 1, 0.5]]) """ x_gradient = cv2.filter2D(img, -1, x_kernel) abs_x = np.absolute(x_gradient) x_grad_u8 = np.uint8(abs_x) displayer.add_image(x_grad_u8, 'manual x gradient') y_gradient = cv2.filter2D(img, -1, y_kernel) abs_y = np.absolute(y_gradient) y_grad_u8 = np.uint8(abs_y) displayer.add_image(y_grad_u8, 'manual y gradient') magnitude = cv2.magnitude(abs_x, abs_y) magnitude_u8 = np.uint8(magnitude) displayer.add_image(magnitude_u8, 'magnitude') """ lap = cv2.Laplacian(orig, -1, cv2.CV_64F) displayer.add_image(np.uint8(lap), 'laplacian') """ canny = cv2.Canny(orig, 100, 200) displayer.add_image(np.uint8(canny), 'Canny') displayer.display(cmap='gray')
def plot_mag(self): magnitude_spectrum = 20*np.log(cv2.magnitude(self.dft_shift[:,:,0],self.dft_shift[:,:,1])) # Displays frequency domain view of image - DC centered plt.subplot(121),plt.imshow(self.img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()
def run(in_file, out_file): img = cv2.imread(in_file) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) rows, cols = gray.shape dft = cv2.dft(np.float32(gray),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) (_, thresh) = cv2.threshold(magnitude_spectrum, 230, 255, cv2.THRESH_BINARY) thresh = np.uint8(thresh) lines = cv2.HoughLines(thresh,1,np.pi/180,30) magnitude_spectrum_lines = np.copy(magnitude_spectrum) for rho,theta in lines[0]: a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) m_numerator = y2 - y1 m_denominator = x2 - x1 angle = np.rad2deg(math.atan2(m_numerator, m_denominator)) M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1) if cols > rows: out_dims = (cols, cols) else: out_dims = (rows, rows) rotated_img = cv2.warpAffine(img, M, out_dims) cv2.line(magnitude_spectrum_lines,(x1,y1),(x2,y2),(0,0,255),2) b,g,r = cv2.split(rotated_img) rotated_img = cv2.merge([r,g,b]) rotated_img = Image.fromarray(rotated_img) rotated_img.save(out_file) magnitude_spectrum = Image.fromarray(magnitude_spectrum).convert('RGB') magnitude_spectrum.save('results/fourier.png') magnitude_spectrum_lines = Image.fromarray(magnitude_spectrum_lines).convert('RGB') magnitude_spectrum_lines.save('results/fourier_lines.png') """
def flow_magnitude(self, prev_frame, frame): '''Calculates the magnitude of the Farneback Optical flow vectors. Both frames are greyscale.''' flow = cv2.calcOpticalFlowFarneback(prev_frame, frame, pyr_scale=0.5, levels=3, winsize=15, iterations=2, poly_n=5, poly_sigma=1.1, flags=0) return cv2.magnitude(flow[..., 0], flow[..., 1])
def spectrum(img): """Calcula y muestra el módulo logartímico de la DFT de img.""" #img=optimalDFTImg(img) imgf=cv.dft(np.float32(img),flags=cv.DFT_COMPLEX_OUTPUT) modulo = np.log(cv.magnitude(imgf[:,:,0],imgf[:,:,1])+1) modulo = np.fft.fftshift(modulo) modulo=cv.normalize(modulo, modulo, 0, 1, cv.NORM_MINMAX) plt.figure() plt.imshow(modulo,cmap='gray') plt.show()
def HPF(self,im_source,im_mask): if len(im_source.shape) == 3: return None im_float32 = np.float32(im_source) dft = cv2.dft(im_float32, flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) fshift = dft_shift*im_mask f_ishift = np.fft.ifftshift(fshift) im_back = cv2.idft(f_ishift) im_back = cv2.magnitude(im_back[:,:,0],im_back[:,:,1]) Pmax = np.max(im_back) im_pow = im_back/Pmax*255 return np.uint8(im_pow)
def GET(self): image_path = os.getcwd() + '/images/' img_list = os.listdir(image_path) img = cv2.imread(image_path + img_list[0], cv2.IMREAD_COLOR) dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) _, data = cv2.imencode('.jpg', magnitude_spectrum) jpeg_base64 = base64.b64encode(data.tostring()) return jpeg_base64
def gradient_map(self, images): gmaps = [] for i, image in enumerate(images): sx = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=-1) sy = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=-1) mag = cv2.magnitude(sx, sy) ori = cv2.phase(sx, sy, angleInDegrees=True) gmaps.append((ori, mag)) (gmap, weight) = self.calc_gradient_map(gmaps) return (gmap, weight)
def apply_mask(mask, img): # apply mask and inverse DFTrum dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) rows, cols = img.shape crow,ccol = rows/2 , cols/2 # fshift = dft_shift*mask # for low pass filter dft_shift[crow-30:crow+30, ccol-30:ccol+30] = 0 #for high pass filter #f_ishift = np.fft.ifftshift(fshift) #fshift for low pass code f_ishift = np.fft.ifftshift(dft_shift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) return img_back
def frequency_filtering(self, **kwargs): #title = kwargs.get('title', 'Freuency filtering') img = kwargs.get('img', None) fig = plt.figure(figsize=(16, 5)) fig.subplots_adjust(hspace=.03, wspace=.03) #fig.suptitle( title, fontsize=24, y=1) #core img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img = self._optimize_shape(img) dft = cv2.dft( np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) _mask = kwargs.get('mask', self.squre_mask(img, 0.05)) fshift = dft_shift*_mask f_ishift = np.fft.ifftshift(fshift) img_idft = cv2.idft(f_ishift) img_idft = cv2.magnitude( img_idft[:,:,0], img_idft[:,:,1]) self._plot3(fig, 1, 'Original', img) self._plot3(fig, 2, 'Frequency Spectrum', magnitude_spectrum) self._plot3(fig, 3, 'Mask', 1- _mask[:,:,0]) self._plot3(fig, 4, 'Result', img_idft) return fig
def run(in_file, n=2): """ Computes a inclination correction process with DFT transformation and Hough lines. Input: in_file: RGB Image input path. n: Process iterations. Default: 2. Output: Corrected image. """ image = cv2.imread(in_file) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) result = np.copy(image) angle = -1 for i in range(0, n): dft = cv2.dft(np.float32(result), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) (_, thresh) = cv2.threshold( magnitude_spectrum, 230, 255, cv2.THRESH_BINARY) thresh = np.uint8(thresh) lines = cv2.HoughLines(thresh, 1, np.pi / 180, 30) if lines is None or angle == 0: break for rho, theta in lines[0]: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000 * (-b)) y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) m_numerator = y2 - y1 m_denominator = x2 - x1 angle = np.rad2deg(math.atan2(m_numerator, m_denominator)) result = functions.rotate_about_center(result, angle) return result
def to_image(self,filename,color=True): #convert back into image f_ishift = np.fft.ifftshift(self.unreshape_arr) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) # plt.subplot(121),plt.imshow(img_back, cmap = 'gray') # plt.title('Image'), plt.xticks([]), plt.yticks([]) # plt.show() #save images with pyplot if color: plt.imsave(filename + "_color.png", img_back) plt.imsave(filename + "_gray.png",img_back,cmap='gray')
def fourier_back_transfrom(img, dft_shift): rows, cols, dim = img.shape crow,ccol = rows/2 , cols/2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows,cols,2),np.uint8) mask[crow-30:crow+30, ccol-30:ccol+30] = 1 # apply mask and inverse DFT fshift = dft_shift*mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) return img_back
def __main__(): img = cv2.imread('test.png',0) dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dft2d_img = dft2d(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) dft_img = splitComplex(dft2d_img) dft_shift = np.fft.fftshift(dft_img) dft_ishift = LPF(dft_shift,img) #print dft_ishift dft_ishift = np.fft.ifftshift(dft_ishift) img_back = cv2.idft(dft_ishift) magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) # use plt show result plt.subplot(131),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(132),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.subplot(133),plt.imshow(img_back, cmap = 'gray') plt.title('Inverse DFT Image'), plt.xticks([]), plt.yticks([]) plt.show()
def preprocess_image(self, img, rect): """""" offset = 50 # x_1, y_1, x_2, y_2 rect #TODO check if its needed to add a try except env x_1, y_1, x_2, y_2 = abs(rect[0] - offset), abs(rect[1] - offset), rect[2] + offset, rect[3] + offset tmp = img[y_1 :y_2, x_1: x_2] norm = cv2.normalize(tmp, tmp, 0, 255, cv2.NORM_MINMAX , cv2.CV_8UC1) cv2.imwrite('/tmp/tpm_{}.jpg'.format("lero"), norm) grad_y = cv2.Sobel(norm, cv2.CV_32F, 0, 1, ksize=3) grad_x = cv2.Sobel(norm, cv2.CV_32F, 1, 0, ksize=3) mag = cv2.magnitude(grad_x, grad_y) #print np.max(mag), mag.argmax(axis=0) return mag, [x_1, y_1, x_2, y_2]
def cv_fft(img): """ OpenCV provides the functions cv2.dft() and cv2.idft() It returns the same result as numpy, but with two channels. First channel will have the real part of the result and second channel will have the imaginary part of the result. The input image should be converted to np.float32 first. """ dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) a = dft_shift[:,:,0] b = dft_shift[:,:,1] magnitude_spectrum = 20*np.log(cv2.magnitude(a, b)) return magnitude_spectrum
def update_butterworth_win(self, dummy=None): """ Update Butterworth filter param and the result image. """ sb = cv2.getTrackbarPos("stopband**2", self.ctrl_panel_winname) if sb == 0: sb = 1 print "Stopband should be more than 0. Reset to 1." bw_filter = self.get_butterworth_filter(stopband2=sb, showdft=True) dst_complex = bw_filter * self.dft4img#cv2.multiply(self.dft4img, bw_filter) dst_complex = cv2.idft(np.fft.ifftshift(dst_complex)) dst = np.uint8(cv2.magnitude(dst_complex[:,:,0], dst_complex[:,:,1])) self.tmp = dst self.get_dft(self.tmp, showdft=True) self.hist_lines(dst, showhist=True) cv2.imshow(self.test_winname, dst)
def get_butterworth_filter(self, stopband2=10, order=3, showdft=False): """ Get Butterworth filter in frequency domain. """ h, w = self.dft4img.shape[0], self.dft4img.shape[1] P = h/2 Q = w/2 dst = np.zeros((h, w, 2), np.float64) for i in range(h): for j in range(w): r2 = float((i-P)**2+(j-Q)**2) if r2 == 0: r2 = 1.0 dst[i,j] = 1/(1+(r2/stopband2)**order) dst = np.float64(dst) if showdft: cv2.imshow("butterworth", cv2.magnitude(dst[:,:,0], dst[:,:,1])) return dst
def getOrientationAndMagnitude(image, show=False): ''' Calculate orientation and magnitude of the gradient image and return it as vector arrays Uses cv2.fastAtan2 for fast orientation calc, cv2.magnitude for fast magnitude calculation Params: image (numpy array): grayscale image to compute this on show (bool): show intermediate steps Returns: (orientation, magnitude): numpy arrays ''' sobelHorizontal = cv2.Sobel(image, cv2.CV_32F, 1, 0) sobelVertical = cv2.Sobel(image, cv2.CV_32F, 0, 1) h = sobelHorizontal v = sobelVertical orientation = np.empty(image.shape) magnitude = np.empty(image.shape) height, width = h.shape for y in range(height): for x in range(width): orientation[y][x] = cv2.fastAtan2(h[y][x], v[y][x]) magnitude = cv2.magnitude(h, v) if show: fig = figure() imshow(magnitude) matplotlib.pyplot.show() fig2 = figure() res = 7 quiver(h[::res, ::res], -v[::res, ::res]) imshow(image[::res, ::res], cmap=gray()) matplotlib.pyplot.show() return orientation, magnitude
def __getGradientImageInfo(self, I): # Use sobel on the x and y axis sobelX = cv2.Sobel(I, cv2.CV_64F, 1, 0) sobelY = cv2.Sobel(I, cv2.CV_64F, 0, 1) # Arrays for holding information about orientation and magnitude og each gradient #orientation = np.zeros(I.shape) #magnitude = np.zeros(I.shape) # Calculate orientation and magnitude of each gradient #for x in range(I.shape[0]): #for y in range(I.shape[1]): #orientation[x][y] = np.arctan2(sobelY[x][y], sobelX[x][y]) * (180 / m.pi) #magnitude[x][y] = m.sqrt(sobelY[x][y] ** 2 + sobelX[x][y] ** 2) magnitude = cv2.magnitude(sobelX, sobelY) orientation = cv2.phase(sobelX, sobelY) return sobelX, sobelY, magnitude, orientation
def _fft(self, batch): for i in range(len(batch)): temp = batch[i] temp = temp[ :, :, 0] x = len(temp[0]) dft = cv2.dft(np.float32(temp),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) rows, cols = temp.shape crow,ccol = rows/2 , cols/2 crow = int(crow) ccol = int(ccol) mask = np.zeros((rows,cols,2),np.uint8) mask[crow-30:crow+30, ccol-30:ccol+30] = 1 fshift = dft_shift*mask f_ishift = np.fft.ifftshift(fshift) a = cv2.idft(f_ishift) a = cv2.magnitude(a[:,:,0],a[:,:,1]) batch[i] = a.reshape([x, x, 1]) return batch
def complex_abs(spectrum): return cv2.magnitude(spectrum[:, :, 0], spectrum[:, :, 1])
# 0709.py import cv2 import numpy as np #1 src = np.zeros(shape=(512, 512), dtype=np.uint8) cv2.rectangle(src, (50, 200), (450, 300), (255, 255, 255), -1) #2 dist = cv2.distanceTransform(src, distanceType=cv2.DIST_L1, maskSize=3) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(dist) print('src:', minVal, maxVal, minLoc, maxLoc) dst = cv2.normalize(dist, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U) ret, dst2 = cv2.threshold(dist, maxVal - 1, 255, cv2.THRESH_BINARY) #3 gx = cv2.Sobel(dist, cv2.CV_32F, 1, 0, ksize=3) gy = cv2.Sobel(dist, cv2.CV_32F, 0, 1, ksize=3) mag = cv2.magnitude(gx, gy) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(mag) print('src:', minVal, maxVal, minLoc, maxLoc) ret, dst3 = cv2.threshold(mag, maxVal - 2, 255, cv2.THRESH_BINARY_INV) cv2.imshow('src', src) cv2.imshow('dst', dst) cv2.imshow('dst2', dst2) cv2.imshow('dst3', dst3) cv2.waitKey() cv2.destroyAllWindows()
# -*- coding: utf-8 -*- # Ref: # Org: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.html # JPN: http://lang.sist.chukyo-u.ac.jp/classes/OpenCV/py_tutorials/py_imgproc/py_transforms/py_fourier_transform/py_fourier_transform.html import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('data/baboon.jpg', 0) dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) isize = 10 # マスクの半分のサイズ # ローパスフィルタ用マスクの作成,中心正方領域は1,それ以外は0 mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - isize:crow + isize, ccol - isize:ccol + isize] = 1 # apply mask and inverse DFT fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) img_back1 = cv2.idft(f_ishift) img_back1 = cv2.magnitude(img_back1[:, :, 0], img_back1[:, :, 1])
ax[0].axis("off"), ax[1].axis("off") # plt.savefig("result.jpg", dpi = 300, bbox_inches = "tight") plt.show() """ import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread( "C:\\Users\\mnicd\\Documents\\CellDetectionSystem\\dgp\\2.15.png", 0) dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dftshift = np.fft.fftshift(dft) # result = 20 * np.log(cv2.magnitude(dftshift[:,:, 0], dftshift[:, :, 1])) idftshift = np.fft.ifftshift(dftshift) idft = cv2.idft(idftshift) #idft:二维复数数组 i_img = cv2.magnitude(idft[:, :, 0], idft[:, :, 1]) #转换到[0, 255] fig, ax = plt.subplots(1, 2) #row = 1, col = 2 ax[0].imshow(img, cmap="gray") ax[1].imshow(i_img, cmap="gray") ax[0].set_title("original"), ax[1].set_title("i_img") ax[0].axis("off"), ax[1].axis("off") # plt.savefig("result.jpg", dpi = 300, bbox_inches = "tight") plt.show()
def question1(path): # Gaussian blurring : image = cv2.imread(path, cv2.IMREAD_GRAYSCALE) blur = cv2.GaussianBlur(image, ((2 * 10) + 1, (2 * 10) + 1), 0) plt.subplot(121), plt.imshow(image), plt.title('Original Image') plt.subplot(122), plt.imshow(blur), plt.title('Gaussian blurred image') plt.show() # Gradient of an image : sobelX = cv2.Sobel(blur, cv2.CV_64F, ksize=2 * 3 + 1, dx=1, dy=0) sobelY = cv2.Sobel(blur, cv2.CV_64F, ksize=2 * 3 + 1, dx=0, dy=1) plt.subplot(321), plt.imshow(image, cmap='gray'), plt.title(' image') plt.subplot(322), plt.imshow(sobelX, cmap='gray'), plt.title('XGradient') plt.subplot(323), plt.imshow(sobelY, cmap='gray'), plt.title('YGradient') combinedimage = cv2.addWeighted(sobelX, .5, sobelY, .5, 0) plt.subplot(324), plt.imshow(combinedimage, cmap='gray'), plt.title('combined edges') magnitude = cv2.magnitude(sobelX, sobelY) plt.subplot(325), plt.imshow(magnitude, cmap='gray'), plt.title('magnitude') plt.show() print("Magnitude is : \n") print(magnitude) edges = np.argwhere(magnitude[:, :]) length, width = image.shape # size of radius starts at 10 pixels and goes upto quarter the image size in width or length, whichever is greater Rmax = max((length / 4), (width / 4)) Rmin = 60 RangeR = int(Rmax - Rmin) print(RangeR) # create accumulator accumulator = np.zeros((length, width, int(Rmax)), dtype=np.uint64) accumdict = {} for rowval in range(len(magnitude)): for colval in range(len(magnitude[rowval])): if magnitude[rowval][colval] > 0: radius = int(Rmin) while radius < int(Rmax): a, b = find_ab(radius, rowval, colval) print("a and b returned are : ", a, b) if a and b > 0: accumulator[int(a), int(b), int(radius)] += magnitude[rowval][colval] if (int(a), int(b), int(radius)) in accumdict.keys(): accumdict[( int(a), int(b), int(radius))] += magnitude[rowval][colval] else: accumdict[(int(a), int(b), int(radius))] = 1 radius += 10 img2 = ndimage.maximum_filter(accumulator, size=(5, 5, 5)) img_thresh = img2.mean() + img2.std() * 6 labels, num_labels = ndimage.label(accumulator > img_thresh) coords = ndimage.measurements.center_of_mass(accumulator, labels=labels, index=np.arange( 1, num_labels + 1))[:5] sortedpeaks = sorted(accumdict.items(), key=lambda x: x[1], reverse=True)[:50] print("Printing sorted peaks") print(coords) for tupleinstance in coords: print(tupleinstance) x, y, radius = tupleinstance print("the x, y, radius values are : ", x, y, radius) image = cv2.circle(image, (int(x), int(y)), int(radius), (0, 0, 255), 5) cv2.imshow('Final_circles', image) cv2.waitKey(0) cv2.destroyWindow('Final_circles') plt.subplot(111), plt.imshow(image, cmap='gray'), plt.title('circles') plt.show()
from matplotlib import pyplot as plt #读取图像 img = cv2.imread('Na.png', 0) #傅里叶变换 dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) fshift = np.fft.fftshift(dft) #设置低通滤波器 rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) #中心位置 mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 #掩膜图像和频谱图像乘积 f = fshift * mask print(f.shape, fshift.shape, mask.shape) #傅里叶逆变换 ishift = np.fft.ifftshift(f) iimg = cv2.idft(ishift) res = cv2.magnitude(iimg[:, :, 0], iimg[:, :, 1]) #显示原始图像和低通滤波处理图像 plt.subplot(121), plt.imshow(img, 'gray'), plt.title('Original Image') plt.axis('off') plt.subplot(122), plt.imshow(res, 'gray'), plt.title('Result Image') plt.axis('off') plt.show()
def dft2d(img): dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) return dft_shift, magnitude_spectrum
magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) return dft_shift, magnitude_spectrum img = cv2.imread('fractal.png', 0) dft_shift, magnitude_spectrum = dft2d(img) rows, cols = img.shape mask_r5 = generateMask(cols, rows, 5) mask_r50 = generateMask(cols, rows, 50) mask_r100 = generateMask(cols, rows, 100) mask_blur = generateGaussianMask(cols, rows, 10) mask_sharpen = generateSharpenMask(cols, rows, 50) mask_r5_magnitude = (cv2.magnitude(mask_r5[:, :, 0], mask_r5[:, :, 1])) mask_r50_magnitude = (cv2.magnitude(mask_r50[:, :, 0], mask_r50[:, :, 1])) mask_r100_magnitude = (cv2.magnitude(mask_r100[:, :, 0], mask_r100[:, :, 1])) mask_blur_magnitude = (cv2.magnitude(mask_blur[:, :, 0], mask_blur[:, :, 1])) mask_sharpen_magnitude = (cv2.magnitude(mask_sharpen[:, :, 0], mask_sharpen[:, :, 1])) img_r5 = applyFilter(dft_shift, mask_r5) img_r50 = applyFilter(dft_shift, mask_r50) img_r100 = applyFilter(dft_shift, mask_r100) img_blur = applyFilter(dft_shift, mask_blur) img_sharpen = applyFilter(dft_shift, mask_sharpen) plt.subplot(241), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(242), plt.imshow(magnitude_spectrum, cmap='gray')
import numpy as np import cv2 from matplotlib import pyplot as plt import csv img = cv2.imread( r'C:\ADITYA\Computervision\UCF50_videos\BaseballPitch_roi\file_79.jpg', 0) dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) mag_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) A = cv2.cartToPolar(dft_shift[:, :, 0], dft_shift[:, :, 1]) magnitude_spectrum = 20 * np.log(A[0]) Phase_spectrum = A[1] print(magnitude_spectrum) print('phase spectrum') print(Phase_spectrum) plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray') plt.subplot(122), plt.imshow(Phase_spectrum, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() with open('vid_fft1.csv', 'w') as out: csv_out = csv.writer(out) csv_out.writerow(A) #for row in A: # csv_out.writerow(row)
def ContrastMeasure(image): img = cv2.Laplacian(image, cv2.CV_64F) sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) mag = cv2.magnitude(sobelx, sobely) return cv2.sumElems(mag)[0] / img.shape[0] / img.shape[1]
# 在前面的部分我们实现了一个 HPF(高通滤波), # 现在我们来做 LPF(低通滤波)将高频部分去除。 # 其实就是对图像进行模糊操作。 # 首先我们需要构建一个掩模, # 与低频区域对应的地方设置为 1, # 与高频区域 对应的地方设置为 0。 import cv2 import numpy as np from matplotlib import pyplot as plt img_rd = cv2.imread("../../photos/people_1.jpg", 0) rows, cols = img_rd.shape crow, ccol = int(rows / 2), int(cols / 2) dft = cv2.dft(np.float32(img_rd), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) # create a mask with center square=1 and remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8) mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1 # apply mask and inverse DFT fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) plt.imshow(img_back) plt.show()
# read image img = cv2.imread('tomat-single.jpg', 0) # ukuran filter size = 30 # change image to discrete fourier transform dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) # shift hasil shifting dft_shift = np.fft.fftshift(dft) print dft, dft_shift # perhitungan spectrum magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) # draw image awal plt.subplot(121), plt.imshow(img, cmap='gray') # labelling plt.title('Input Image'), plt.xticks([]), plt.yticks([]) # draw magnitude spectrum plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() rows, cols = img.shape crow, ccol = rows / 2, cols / 2 # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8)
def main(argv): print_help() filename = argv[0] if len(argv) > 0 else "data/lena.jpg" I = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) if I is None: print('Error opening image') return -1 ## [expand] rows, cols = I.shape m = cv2.getOptimalDFTSize(rows) n = cv2.getOptimalDFTSize(cols) padded = cv2.copyMakeBorder(I, 0, m - rows, 0, n - cols, cv2.BORDER_CONSTANT, value=[0, 0, 0]) ## [expand] ## [complex_and_real] planes = [np.float32(padded), np.zeros(padded.shape, np.float32)] complexI = cv2.merge( planes) # Add to the expanded another plane with zeros ## [complex_and_real] ## [dft] cv2.dft(complexI, complexI) # this way the result may fit in the source matrix ## [dft] # compute the magnitude and switch to logarithmic scale # = > log(1 + sqrt(Re(DFT(I)) ^ 2 + Im(DFT(I)) ^ 2)) ## [magnitude] cv2.split(complexI, planes) # planes[0] = Re(DFT(I), planes[1] = Im(DFT(I)) cv2.magnitude(planes[0], planes[1], planes[0]) # planes[0] = magnitude magI = planes[0] ## [magnitude] ## [log] matOfOnes = np.ones(magI.shape, dtype=magI.dtype) cv2.add(matOfOnes, magI, magI) # switch to logarithmic scale cv2.log(magI, magI) ## [log] ## [crop_rearrange] magI_rows, magI_cols = magI.shape # crop the spectrum, if it has an odd number of rows or columns magI = magI[0:(magI_rows & -2), 0:(magI_cols & -2)] cx = int(magI_rows / 2) cy = int(magI_cols / 2) q0 = magI[0:cx, 0:cy] # Top-Left - Create a ROI per quadrant q1 = magI[cx:cx + cx, 0:cy] # Top-Right q2 = magI[0:cx, cy:cy + cy] # Bottom-Left q3 = magI[cx:cx + cx, cy:cy + cy] # Bottom-Right tmp = np.copy(q0) # swap quadrants (Top-Left with Bottom-Right) magI[0:cx, 0:cy] = q3 magI[cx:cx + cx, cy:cy + cy] = tmp tmp = np.copy(q1) # swap quadrant (Top-Right with Bottom-Left) magI[cx:cx + cx, 0:cy] = q2 magI[0:cx, cy:cy + cy] = tmp ## [crop_rearrange] ## [normalize] cv2.normalize( magI, magI, 0, 1, cv2.NORM_MINMAX) # Transform the matrix with float values into a ## viewable image form(float between values 0 and 1). ## [normalize] cv2.imshow("Input Image", I) # Show the result cv2.imshow("spectrum magnitude", magI) cv2.waitKey()
def Tag_using_FFT(im_org): """ Find a tag from a imageframe using FFT pattern noise removal methods. """ # convert to grayscale and blur im = cv2.cvtColor(im_org, cv2.COLOR_BGR2GRAY) im = cv2.GaussianBlur(im,(3,3),0.2) # find FFT of the image and shift it. dft = cv2.dft(np.float32(im),flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) # find the magnitude spectrum to visualise it. magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) # get a circular mask to remove the central spot mask = getCircularMask(dft_shift.shape, radius=30) # apply the mask on FFT fshift = dft_shift*mask # find the magnitude spectrum to visualise masked FFT. magnitude_spectrum_lp = 20*np.log(cv2.magnitude(fshift[:,:,0],fshift[:,:,1])) # inverse shift the FFT f_ishift = np.fft.ifftshift(fshift) # find magnitude of inverse FFT im_ = cv2.idft(f_ishift) im_ = cv2.magnitude(im_[:,:,0],im_[:,:,1]) # scale the inverse FFT to 0-255 to reconstruct the filtered output im_ = cv2.normalize(im_, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8) # perform thresholding to obtain binary map of the image _, binary = cv2.threshold(im_, 20, 255, cv2.THRESH_BINARY) kernel = np.ones((3,3),np.uint8) binary = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) # find the contour of the largest area in the image and extract the region where White space of Tag is present. contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) Area_List = [] for c in contours: Area_List.append(cv2.contourArea(c)) i = np.argmax(np.array(Area_List)) rect = cv2.boundingRect(contours[i]) x,y,w,h = rect margin = 10 # get the AR Tag with white space with 10pixel pad margin AR_tag = im_org[y-margin:y+h+margin,x-margin:x+w+margin] # find the contour of the AR block inside the Tag with white space _, binary = cv2.threshold(cv2.cvtColor(AR_tag, cv2.COLOR_BGR2GRAY), 240, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) AR_contour = contours[1] # get the AR's contour # get the corresponding corner. AR_corners = cv2.approxPolyDP(AR_contour,0.11*cv2.arcLength(AR_contour,True),True) size = 20 reference_corners = np.array([[0,0],[size,0],[size,size],[0,size]]).reshape(-1,1,2) H = Homography(np.float32(AR_corners), np.float32(reference_corners)) # warp the AR from the ground to an arbitrarily chosen plane of dimensions size x size imOut = np.zeros((size,size,3),dtype = np.uint8) warped_AR_tag = Warp(AR_tag, H, imOut) warped_AR_tag = cv2.resize(warped_AR_tag, (128,128)) # resizing to larger size to perform interpolation warped_AR_tag = warped_AR_tag[margin:-margin,margin:-margin] return magnitude_spectrum, magnitude_spectrum_lp, im_, AR_tag, warped_AR_tag
#Output is a 2D complex array. 1st channel real and 2nd imaginary #For fft in opencv input image needs to be converted to float32 dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) #Rearranges a Fourier transform X by shifting the zero-frequency #component to the center of the array. #Otherwise it starts at the tope left corenr of the image (array) dft_shift = np.fft.fftshift(dft) #FFT of image ##Magnitude of the function is 20.log(abs(f)) #For values that are 0 we may end up with indeterminate values for log. #So we can add 1 to the array to avoid seeing a warning. magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) # Circular HPF mask, center circle is 0, remaining all ones #Can be used for edge detection because low frequencies at center are blocked #and only high frequencies are allowed. Edges are high frequency components. #Amplifies noise. rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) #Center point mask = np.ones((rows, cols, 2), np.uint8) #Crate mask fo 2 channels r = 80 # Radius of circular mask center = [crow, ccol] x, y = np.ogrid[:rows, :cols] mask_area = (x - center[0])**2 + (y - center[1])**2 <= r * r
import os if __name__ == '__main__': path = '../../img/aerials/2.2.02.tiff' if os.path.isfile(path): img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) print("[INFO] Image has been read successfully...") else: print("[INFO] The file '" + path + "' does not exist.") sys.exit(0) dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitudeSpectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) rows, cols = img.shape crow, ccol = int(rows / 2), int(cols / 2) # create a mask first, center square is 1, remaining all zeros mask = np.zeros((rows, cols, 2), np.uint8) outputImgArr = [img, magnitudeSpectrum] maskSize = [5, 15, 25, 35, 50, 80] titles = ['Input Image', "Magnitude Spectrum" ] + ['Mask Size: ' + str(i) for i in maskSize] for n in maskSize: mask[crow - n:crow + n, ccol - n:ccol + n] = 1 # apply mask and inverse DFT fshift = dft_shift * mask
# -*- coding: utf-8 -*- """ Created on Tue Jul 17 18:04:55 2018 李立宗 """ import numpy as np import cv2 import matplotlib.pyplot as plt img = cv2.imread(r'../image/lena.bmp',0) dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) dftShift = np.fft.fftshift(dft) rows, cols = img.shape crow,ccol = int(rows/2) , int(cols/2) mask = np.zeros((rows,cols,2),np.uint8) #两个通道,与频谱图像匹配 mask[crow-30:crow+30, ccol-30:ccol+30] = 1 fShift = dftShift*mask ishift = np.fft.ifftshift(fShift) iImg = cv2.idft(ishift) iImg= cv2.magnitude(iImg[:,:,0],iImg[:,:,1]) plt.subplot(121),plt.imshow(img, cmap = 'gray') plt.title('original'), plt.axis('off') plt.subplot(122),plt.imshow(iImg, cmap = 'gray') plt.title('result'), plt.axis('off') plt.show()
sharp = cv2.addWeighted(gray, 1, mask, 0.03, 0) #ajoute l'original avec le mask pondéré #laplacian shapening laplacien = cv2.Laplacian(sharp, cv2.CV_64F) laplacienScaled = cv2.convertScaleAbs(laplacien) image = cv2.addWeighted(sharp, 1, laplacienScaled, -0.2, 0) #changer a true pour afficher le spectre frequenciel if (False): #transformation en fréquence dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT) #transforme en fréquence dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 * np.log( cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) #filtrage des frequences rows, cols = image.shape crow, ccol = math.floor(rows / 2), math.floor(cols / 2) mask = np.ones((rows, cols, 2), np.uint8) mask[crow - 500:crow + 500, ccol - 100:ccol + 100] = 0 fshift = dft_shift * mask magnitude_spectrum2 = 20 * np.log( cv2.magnitude(fshift[:, :, 0], fshift[:, :, 1])) #reconstruction de l'image f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
def OverLayFFT(img1, img2): global ang1, ang2, img1DFT, img2DFT, DFTShift1, DFTShift2, img1_magnitudeSpectrum, img2_magnitudeSpectrum, overlayFFT_mag, img1_LPF_iFFT_mag, img2_LPF_iFFT_mag, img1_HPF_iFFT_mag, img2_HPF_iFFT_mag, overlayFFT_phase, overlayFFT_phase2, CartDiff, overlayFFT, overlayiFFT_mag, overlayiFFT, img2Corrected, img2PhaseCorrected row1, col1 = img1.shape row2, col2 = img2.shape centerRow1, centerCol1 = row1 / 2, col1 / 2 centerRow2, centerCol2 = row2 / 2, col2 / 2 centerRectangle = 15 # Calculating 2D FFT img1FFT2 = np.fft.fft2(img1) img2FFT2 = np.fft.fft2(img2) # WORK IN PROGRESS: CartDiff = img2FFT2 - img1FFT2 # print ("Cart Diff: ", CartDiff) img2FFT2 = img1FFT2 - CartDiff # shifting the DC component to the center of the image FFT2Shift1 = np.fft.fftshift(img1FFT2) FFT2Shift2 = np.fft.fftshift(img2FFT2) # Need to verify DFTShift1[:,:,1] is angle or DFTShift1[:,:,0]? DFTShift1[:,:,1] is the angle ang1 = np.angle(img1FFT2) ang2 = np.angle(img2FFT2) overlayFFT_phase = np.angle(img1FFT2) - np.angle(img2FFT2) # print("overlay FFT phase shift", overlayFFT_phase) img2PhaseCorrected = np.angle(FFT2Shift2) - overlayFFT_phase # ang1 = cv2.phase(img1DFT[:,:,0], img1DFT[:,:,1]) # ang2 = cv2.phase(img2DFT[:,:,0], img2DFT[:,:,1]) # overlayFFT_phase2 # m,n = overlayFFT_phase2.shape # print(m,n) # calculating the magnitute of the complex numbers of DFTShift img1_magnitudeSpectrum = 20 * np.log(np.abs(FFT2Shift1)) img2_magnitudeSpectrum = 20 * np.log(np.abs(FFT2Shift2)) #TODO: try adding the manitudes of the two image in freq domain for overlay FFT overlayFFT_mag = img2_magnitudeSpectrum - img1_magnitudeSpectrum # img2MagCorrected = img1_magnitudeSpectrum + overlayFFT_mag # print(overlayFFT_phase) # Convert back to space domain and then graph the overlay diff # cv2.determinant(ang1) - cv2.determinant(ang2) overlayFFT = [img2_magnitudeSpectrum + img2PhaseCorrected * j] # print("overlay FFT: ", overlayFFT) overlayFFT_ishift = np.fft.ifftshift(overlayFFT) # overlayiFFT = np.fft.ifft2(overlayFFT_ishift) overlayiFFT = np.fft.ifft2(img2FFT2) img2Corrected = np.abs(overlayiFFT) # print("image 2 corrected: ", img2Corrected.shape, img2Corrected[:,:]) # x,y = cv2.polarToCart(overlayFFT_ishift[0,:,:],overlayFFT_ishift[1,:,:]) # overlayiFFT_mag = np.abs(overlayiFFT[0,:,:]) # row4, col4 =overlayiFFT_mag.shape # print(row4, col4) # print("overlay iFFT Mag: ", overlayiFFT_mag) # print (overlayFFT_phase) # END OF WORK IN PROGRESS # ------------------------------------------------------------------------------ # Low pass filtering: creating a mask with high value of 1 at low freq and # 0 at high freq freqMask1_LPF = np.zeros((row1, col1, 2), np.float32) freqMask2_LPF = np.zeros((row2, col2, 2), np.float32) freqMask1_LPF[centerRow1 - centerRectangle:centerRow1 + centerRectangle, centerCol1 - centerRectangle:centerCol1 + centerRectangle] = 1 freqMask2_LPF[centerRow2 - centerRectangle:centerRow2 + centerRectangle, centerCol2 - centerRectangle:centerCol2 + centerRectangle] = 1 img1DFT = cv2.dft(np.float32(img1), flags=cv2.DFT_COMPLEX_OUTPUT) img2DFT = cv2.dft(np.float32(img2), flags=cv2.DFT_COMPLEX_OUTPUT) DFTShift1 = np.fft.fftshift(img1DFT) DFTShift2 = np.fft.fftshift(img2DFT) #applying the mask for LPF DFTShift1_LPF_Masked = DFTShift1 * freqMask1_LPF DFTShift2_LPF_Masked = DFTShift2 * freqMask2_LPF #Inverse FFT img1_LPF_iFFT_shift = np.fft.ifftshift(DFTShift1_LPF_Masked) img2_LPF_iFFT_shift = np.fft.ifftshift(DFTShift2_LPF_Masked) img1_LPF_iFFT = cv2.idft(img1_LPF_iFFT_shift) img2_LPF_iFFT = cv2.idft(img2_LPF_iFFT_shift) img1_LPF_iFFT_mag = cv2.magnitude(img1_LPF_iFFT[:, :, 0], img1_LPF_iFFT[:, :, 1]) img2_LPF_iFFT_mag = cv2.magnitude(img2_LPF_iFFT[:, :, 0], img2_LPF_iFFT[:, :, 1]) # High Pass filtering: setting a 15x15 center rectangle to block low freq DFTShift1[centerRow1 - centerRectangle:centerRow1 + centerRectangle, centerCol1 - centerRectangle:centerCol1 + centerRectangle] = 0 DFTShift2[centerRow2 - centerRectangle:centerRow2 + centerRectangle, centerCol2 - centerRectangle:centerCol2 + centerRectangle] = 0 #inverse FFT img1_HPF_iFFT_shift = np.fft.ifftshift(DFTShift1) img2_HPF_iFFT_shift = np.fft.ifftshift(DFTShift2) img1_HPF_iFFT = cv2.idft(img1_HPF_iFFT_shift) img2_HPF_iFFT = cv2.idft(img2_HPF_iFFT_shift) img1_HPF_iFFT_mag = cv2.magnitude(img1_HPF_iFFT[:, :, 0], img1_HPF_iFFT[:, :, 1]) img2_HPF_iFFT_mag = cv2.magnitude(img2_HPF_iFFT[:, :, 0], img2_HPF_iFFT[:, :, 1]) return ang1, ang2, img1DFT, img2DFT, DFTShift1, DFTShift2, img1_magnitudeSpectrum, img2_magnitudeSpectrum, overlayFFT_mag, img1_LPF_iFFT_mag, img2_LPF_iFFT_mag, img1_HPF_iFFT_mag, img2_HPF_iFFT_mag, overlayFFT_phase, CartDiff, overlayFFT, overlayiFFT, img2Corrected, img2PhaseCorrected
def get_center_map(eye_img): f_x, f_y, f_xy, f_xx, f_yy = get_gradients(eye_img) # Calculate the curved-ness and weighting function curvedness = np.sqrt(f_xx**2 + 2 * f_xy**2 + f_yy**2) curvedness_norm = cv2.normalize(curvedness, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8) cv2.imshow("curvedness", curvedness_norm) weight_edge = cv2.normalize( curvedness, 0, 255 * __weight_ratio_edge, norm_type=cv2.NORM_MINMAX) # higher weight to stronger edges weight_middle = cv2.normalize( (255 - eye_img), 0, 255 * __weight_ratio_darkness, norm_type=cv2.NORM_MINMAX) # higher center weight to darker areas # Calculate the displacement vectors temp_top = f_x**2 + f_y**2 temp_bot = ((f_y**2) * f_xx) - (2 * f_x * f_xy * f_y) + ( (f_x**2) * f_yy) + 0.0001 # hack to offset against division by 0 d_vec_mul = -temp_top / temp_bot d_vec_x = d_vec_mul * f_x d_vec_y = d_vec_mul * f_y # Remove infinite displacements for straight lines d_vec_x = np.nan_to_num(d_vec_x) d_vec_y = np.nan_to_num(d_vec_y) mag_d_vec = cv2.magnitude(d_vec_x, d_vec_y) # Prevent using weights with bad radius sizes weight_edge[mag_d_vec < __min_rad] = 0 weight_edge[mag_d_vec > __max_rad] = 0 # Calculate curvature to ensure we use gradients which point in right direction curvature = temp_bot / (0.0001 + (temp_top**1.5)) weight_edge[curvature < 0] = 0 weight_edge[curvedness_norm < 20] = 0 # Make indexes into accumulator using basic grid and vector offsets grid = np.indices(eye_img.shape[:2], np.uint8) acc_inds_x = grid[1] + d_vec_x.astype(int) acc_inds_y = grid[0] + d_vec_y.astype(int) # Prevent indexing outside of accumulator acc_inds_x[acc_inds_x < 0] = 0 acc_inds_y[acc_inds_y < 0] = 0 acc_inds_x[acc_inds_x >= eye_img.shape[1]] = 0 acc_inds_y[acc_inds_y >= eye_img.shape[0]] = 0 # Make center map accumulator accumulator = np.zeros(eye_img.shape[:2]) # Use numpy fancy indexing to add weights in one go # (This is fast as it avoids python loops) accumulator[acc_inds_y, acc_inds_x] += weight_edge accumulator += weight_middle # Post-processing morph_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) accumulator = cv2.morphologyEx(accumulator, cv2.MORPH_DILATE, morph_kernel) # accumulator = cv2.blur(accumulator, (10, 10)) accumulator = cv2.GaussianBlur(accumulator, (13, 13), 0) return accumulator
import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('./Lena.png', 0).astype(np.float32) / 255 fft = cv2.dft(image, flags=cv2.DFT_COMPLEX_OUTPUT) shifted = np.fft.fftshift(fft, axes=[0, 1]) magnitude = cv2.magnitude(shifted[:, :, 0], shifted[:, :, 1]) magnitude = np.log(magnitude) plt.axis('off') plt.imshow(magnitude, cmap='gray') plt.tight_layout(True) plt.show() restored = cv2.idft(fft, flags=cv2.DFT_SCALE | cv2.DFT_REAL_OUTPUT) cv2.imshow('restored', restored) cv2.waitKey() cv2.destroyAllWindows()
gaussian_noise_sigma15 = img+np.uint8(np.random.normal(mean,sigma15,(row,col))) sigma30 = 30 gaussian_noise_sigma30 = img+np.uint8(np.random.normal(mean,sigma30,(row,col))) sigma45 = 45 gaussian_noise_sigma45 = img+np.uint8(np.random.normal(mean,sigma45,(row,col))) img_float32_sigma15 = np.float32(gaussian_noise_sigma15) img_float32_sigma30 = np.float32(gaussian_noise_sigma30) img_float32_sigma45 = np.float32(gaussian_noise_sigma45) # DFT, shift and magnitude spectrum at sigma equals 15 dft_sigma15 = cv2.dft(img_float32_sigma15, flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift_sigma15 = np.fft.fftshift(dft_sigma15) magnitude_spectrum_sigma15 = 20*np.log(cv2.magnitude(dft_shift_sigma15[:,:,0],dft_shift_sigma15[:,:,1])) # DFT, shift and magnitude spectrum at sigma equals 30 dft_sigma30 = cv2.dft(img_float32_sigma30, flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift_sigma30 = np.fft.fftshift(dft_sigma30) magnitude_spectrum_sigma30 = 20*np.log(cv2.magnitude(dft_shift_sigma30[:,:,0],dft_shift_sigma30[:,:,1])) # DFT, shift and magnitude spectrum at sigma equals 45 dft_sigma45 = cv2.dft(img_float32_sigma45, flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift_sigma45 = np.fft.fftshift(dft_sigma45) magnitude_spectrum_sigma45 = 20*np.log(cv2.magnitude(dft_shift_sigma45[:,:,0],dft_shift_sigma45[:,:,1])) ########################################### rows, cols =img.shape crow,ccol = int(rows/2), int(cols/2) # Center
import numpy as np import cv2 as cv from matplotlib import pyplot as plt img = cv.imread("riya.jpg", 0) f = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT) fshift = np.fft.fftshift(f) magnitude_spectrum = 20 * np.log(cv.magnitude(fshift[:, :, 0], fshift[:, :, 1])) plt.figure(figsize=(10, 10)) plt.subplot(221), plt.imshow(img, cmap="gray") plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(222), plt.imshow(magnitude_spectrum, cmap='gray') plt.title("Magnitude Image"), plt.xticks([]), plt.yticks([]) # 去掉高频信号 row, col = img.shape cx, cy = col // 2, row // 2 zeros = np.zeros((row, col, 2)) zeros[cy - 30:cy + 30, cx - 30:cy + 30] = fshift[cy - 30:cy + 30, cx - 30:cy + 30] # 傅里叶逆变换 fshift_b = np.fft.ifftshift(zeros) f_b = cv.idft(np.float32(fshift_b), flags=cv.DFT_COMPLEX_OUTPUT) img_back = cv.magnitude(f_b[:, :, 0], f_b[:, :, 1]) print(f_b)
filtered_img = cv2.dft(dft, flags=cv2.DFT_INVERSE) # normalized the filtered image into 0 -> 255 (8-bit grayscale) so we # can see the output min_val, max_val, min_loc, max_loc = \ cv2.minMaxLoc(filtered_img[:, :, 0]) filtered_img_normalized = filtered_img[:, :, 0] * ( 1.0 / (max_val - min_val)) + ((-min_val) / (max_val - min_val)) filtered_img_normalized = np.uint8(filtered_img_normalized * 255) # calculate the magnitude spectrum and log transform + scale it for # visualization magnitude_spectrum = np.log(cv2.magnitude( dft_filtered[:, :, 0], dft_filtered[:, :, 1])) # create a 8-bit image to put the magnitude spectrum into magnitude_spectrum_normalized = np.zeros( (nheight, nwidth, 1), np.uint8) # normalized the magnitude spectrum into 0 -> 255 (8-bit grayscale) so # we can see the output cv2.normalize( np.uint8(magnitude_spectrum), magnitude_spectrum_normalized, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
# recover the original image via the inverse DFT filtered_img = cv2.dft(dft, flags=cv2.DFT_INVERSE) # normalized the filtered image into 0 -> 255 (8-bit grayscale) so we can see the output minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(filtered_img[:, :, 0]) filtered_img_normalized = filtered_img[:, :, 0] * ( 1.0 / (maxVal - minVal)) + ((-minVal) / (maxVal - minVal)) filtered_img_normalized = np.uint8(filtered_img_normalized * 255) # calculate the magnitude spectrum and log transform + scale it for visualization magnitude_spectrum = np.log( cv2.magnitude(dft_filtered[:, :, 0], dft_filtered[:, :, 1])) # create a 8-bit image to put the magnitude spectrum into magnitude_spectrum_normalized = np.zeros((nheight, nwidth, 1), np.uint8) # normalized the magnitude spectrum into 0 -> 255 (8-bit grayscale) so we can see the output cv2.normalize(np.uint8(magnitude_spectrum), magnitude_spectrum_normalized, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) # display images
print dft1.shape dft_shift1 = np.fft.fftshift(dft1) dftmag1, dftphase1 = cv2.cartToPolar(dft_shift1[:, :, 0], dft_shift1[:, :, 1]) invdftmag = np.ones(dftmag1.shape) invdftphase = np.zeros(dftphase1.shape) for i in range(-240, 241): for j in range(-240, 241): if np.exp(-0.0025 * (i * i + j * j)**(5 / 6)) >= 0.01: invdftmag[i, j] = np.exp(0.0025 * (i * i + j * j)**(5 / 6)) print invdftmag dftmag2 = invdftmag * dftmag1 dftphase2 = dftphase1 - invdftphase dft_shift1[:, :, 0], dft_shift1[:, :, 1] = cv2.polarToCart(dftmag2, dftphase2) f_ishift = np.fft.ifftshift(dft_shift1) img2 = cv2.idft(f_ishift) img2 = cv2.magnitude(img2[:, :, 0], img2[:, :, 1]) print img1 print img2 / np.max(img2) * 255 plt.subplot(121), plt.imshow(img1, cmap='gray') plt.title('Input'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(np.uint8(img2 / np.max(img2) * 255), cmap='gray') plt.title('Restored'), plt.xticks([]), plt.yticks([]) plt.show()
plt.imshow(img_back, cmap='gray') plt.title('Image after HPF') plt.xticks([]), plt.yticks([]) plt.subplot(133) plt.imshow(img_back) plt.title('Result in JET') plt.xticks([]), plt.yticks([]) plt.show() dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) magnitude_spectrum = 20 * np.log(cv.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1])) plt.subplot(121) plt.imshow(img, cmap='gray') plt.title('Input Image') plt.xticks([]), plt.yticks([]) plt.subplot(122) plt.imshow(magnitude_spectrum, cmap='gray') plt.title('Magnitude Spectrum') plt.xticks([]), plt.yticks([]) plt.show() mask = np.zeros((rows, cols, 2), np.uint8)
def kspace_to_image(kspace): assert(len(kspace.shape) == 3 and kspace.shape[-1] == 2) fft = cv.idft(kspace, flags=cv.DFT_SCALE) img = cv.magnitude(fft[:,:,0], fft[:,:,1]) return cv.normalize(img, dst=None, alpha=255, beta=0, norm_type=cv.NORM_MINMAX, dtype=cv.CV_8U)
import sys import numpy as np import cv2 src = cv2.imread('.\\ch06\\lenna.bmp', cv2.IMREAD_GRAYSCALE) if src is None: print('Image load failed!') sys.exit() dx = cv2.Sobel(src, cv2.CV_32F, 1, 0) dy = cv2.Sobel(src, cv2.CV_32F, 0, 1) mag = cv2.magnitude(dx, dy) mag = np.clip(mag, 0, 255).astype(np.uint8) dst = np.zeros(src.shape[:2], np.uint8) dst[mag > 120] = 255 #_, dst = cv2.threshold(mag, 120, 255, cv2.THRESH_BINARY) cv2.imshow('src', src) cv2.imshow('mag', mag) cv2.imshow('dst', dst) cv2.waitKey() cv2.destroyAllWindows()
def magnitude(i, j): return cv2.magnitude(i, j)