def formatted_spectrum(self): """ Cleans up a measured spectrum and format it such that it is directly readable by the CNN. Args: spectrum_name: filename of the spectrum that is being considered Returns: ys: Processed XRD spectrum in 4501x1 form. """ ## Load data data = np.loadtxt('%s/%s' % (self.spectra_dir, self.spectrum_fname)) x = data[:, 0] y = data[:, 1] ## Convert to Cu K-alpha radiation if needed if str(self.wavelen) != 'CuKa': Cu_x, Cu_y = [], [] for (two_thet, intens) in zip(x, y): scaled_x = self.convert_angle(two_thet) if scaled_x is not None: Cu_x.append(scaled_x) Cu_y.append(intens) x, y = Cu_x, Cu_y ## Fit to 4,501 values as to be compatible with CNN f = ip.CubicSpline(x, y) xs = np.linspace(10, 80, 4501) ys = f(xs) ## Smooth out noise ys = self.smooth_spectrum(ys) ## Map to integers in range 0 to 255 so cv2 can handle ys = [val - min(ys) for val in ys] ys = [255 * (val / max(ys)) for val in ys] ys = [int(val) for val in ys] ## Perform baseline correction with cv2 pixels = [] for q in range(10): pixels.append(ys) pixels = np.array(pixels) img, background = subtract_background_rolling_ball( pixels, 800, light_background=False, use_paraboloid=True, do_presmooth=False) yb = np.array(background[0]) ys = np.array(ys) - yb ## Normalize from 0 to 100 ys = np.array(ys) - min(ys) ys = list(100 * np.array(ys) / max(ys)) return ys
def rolling_ball_subtract(img): nobackground_img, background = subtract_background_rolling_ball( img, 20, light_background=False, use_paraboloid=False, do_presmooth=False) return nobackground_img
def ContrastNormalized(image): """ Background subtraction method Subtracts background around each pixel over a large ball """ img, background = subtract_background_rolling_ball(image, 86, light_background=False, use_paraboloid=False, do_presmooth=False) return img
def rolling_ball_subtract(img): z_size = img.shape[0] nobackground_img = np.zeros(shape=img.shape) background = np.zeros(shape=img.shape) for z in range(z_size): temp_img = img[z, :, :] nobackground_img[z, :, :], background[ z, :, :] = subtract_background_rolling_ball(temp_img, 20, light_background=False, use_paraboloid=False, do_presmooth=False) return nobackground_img
def draw_comprison_images(img, mask): img, background = subtract_background_rolling_ball(img, 10, light_background=True, use_paraboloid=True, do_presmooth=False) blur_img = cv2.blur(img, (4, 4)) blur_mask = cv2.blur(mask, (4, 4)) # ----------------------- blur_sobel_x, blur_sobel_y, blur_sobel_xy = sobel_image(blur_img) ''' scharr = cv2.Scharr(img,cv2.CV_64F,dx=0,dy=1) abs_scharr = np.absolute(scharr) scharr_8u = np.uint8(abs_scharr) laplacian = cv2.Laplacian(img,cv2.CV_64F) abs_laplacian = np.absolute(laplacian) lapacian_8u = np.uint8(abs_laplacian) ''' blur_canny_auto = auto_canny(blur_img) #----------------------- mask_sobel_x, mask_sobel_y, mask_sobel_xy = sobel_image(mask) mask_canny_auto = auto_canny(mask) # ---------------------- blur_mask_sobel_x, blur_mask_sobel_y, blur_mask_sobel_xy = sobel_image( blur_mask) blur_mask_canny_auto = auto_canny(blur_mask) # ---------------------- edge = img & auto_canny_with_dillation(mask) plt.subplot(1, 5, 1), plt.imshow(img, cmap='gray') plt.title('image'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 2), plt.imshow(mask, cmap='gray') plt.title('mask'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 3), plt.imshow(edge, cmap='gray') plt.title('Image Cropped'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 4), plt.imshow(auto_canny(edge), cmap='gray') plt.title('Canny Image'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 5), plt.imshow(auto_canny_with_dillation(mask, dillation_size=8), cmap='gray') plt.title('Canny Mask'), plt.xticks([]), plt.yticks([]) plt.tight_layout(pad=1.0) plt.savefig('edge_image.png', dpi=300) plt.close() blur_edge = cv2.blur(edge, (4, 4)) edge_sobel_x, edge_sobel_y, edge_sobel_xy = sobel_image(blur_edge) plt.subplot(1, 5, 1), plt.imshow(blur_edge, cmap='gray') plt.title('Blur Edge'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 2), plt.imshow(auto_canny(blur_edge), cmap='gray') plt.title('Canny'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 3), plt.imshow(edge_sobel_xy, cmap='gray') plt.title('Sobel X+Y'), plt.xticks([]), plt.yticks([]) edge_sobel_xy_mean = np.mean(edge_sobel_xy[edge_sobel_xy != 0]) edge_sobel_xy_std = np.std(edge_sobel_xy[edge_sobel_xy != 0]) threshold_edge_sobel_xy = np.ndarray(edge_sobel_xy.shape) print(edge_sobel_xy_mean, edge_sobel_xy_std) for x_ind, columns in enumerate(edge_sobel_xy): for y_ind, column in enumerate(columns): if column > edge_sobel_xy_mean + edge_sobel_xy_std or column < edge_sobel_xy_mean: column = 0 threshold_edge_sobel_xy[x_ind, y_ind] = column threshold_edge_sobel_xy = threshold_edge_sobel_xy.astype(np.uint8) new_edge = threshold_edge_sobel_xy & auto_canny_with_dillation( mask, dillation_size=8) plt.subplot(1, 5, 4), plt.imshow(threshold_edge_sobel_xy, cmap='gray') plt.title('Threshold'), plt.xticks([]), plt.yticks([]) plt.subplot(1, 5, 5), plt.imshow(new_edge, cmap='gray') plt.title('Cropped'), plt.xticks([]), plt.yticks([]) plt.tight_layout(pad=1.0) plt.savefig('edge_image2.png', dpi=300) plt.close()
# https://pypi.org/project/opencv-rolling-ball/ import cv2 as cv titleWindow = 'Rollingball.py' # be sure to add the package opencv-rolling-ball with Tools/Python/Environment/Packages from cv2_rolling_ball import subtract_background_rolling_ball img = cv.imread(f'../Data/rolling-ball-input.png', 0) cv.imshow("Python input", img) cv.waitKey(100) print("Working on the image...This can take a while even on small images.") img, background = subtract_background_rolling_ball(img, 30, light_background=True, use_paraboloid=False, do_presmooth=True) cv.imshow("background", background) cv.waitKey()
def get_bg_fg(img): softmask, background = subtract_background_rolling_ball(img,20, light_background=False,use_paraboloid=False, do_presmooth=True) mask = cv2.threshold(softmask,127, 255,cv2.THRESH_BINARY_INV| cv2.THRESH_OTSU)[1] background = cv2.bitwise_and(img, img, mask = mask) foreground = cv2.bitwise_and(img, img, mask = cv2.bitwise_not(mask)) return background, foreground
# In[164]: from cv2_rolling_ball import subtract_background_rolling_ball img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) # In[183]: plt.imshow(img_gray) plt.show() # In[180]: img_subt = img_gray img_subt, img_bkg = subtract_background_rolling_ball(img_subt, 4, light_background=False, use_paraboloid=True, do_presmooth=True) # In[216]: img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) plt.imshow(img_gray) plt.show() plt.imshow(img_bkg) plt.show() plt.imshow(img_subt) plt.show() # In[217]:
img = cv2.imread(writeFileName2) img = img.astype('uint8') grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) grey = cv2.medianBlur(grey, 7) grey = cv2.GaussianBlur(grey, (3, 3), 0) #cv2.imwrite(writeFileName2, grey) #Flatten the Data with a rolling ball if rBall == 1: ballRadius = 200 print("Applying rolling ball") print("Please wait...") rollingBallIm, background = subtract_background_rolling_ball( grey, ballRadius, light_background=False, use_paraboloid=False, do_presmooth=False) writeFileName3 = prefix + 'P' + '.jpg' print("Rolling ball applied...") print("") rollingBallImSave = rollingBallIm / np.max(rollingBallIm[:, :]) #greyScaleIm = greyScaleIm/np.max(greyScaleIm[:,:]) rollingBallImSave = rollingBallImSave * 255 rollingBallImSave = rollingBallImSave.astype('uint8') cv2.imwrite(writeFileName3, rollingBallImSave) elif rBall == 0: #ballRadius = 200 rollingBallIm = (grey)