def blur_loss(y_true, y_pred): #weighting of blur loss alpha = 0.5 mae = mean_absolute_error(y_true, y_pred) trueLap = Laplacian(y_true, CV_8U).var() predLap = Laplacian(y_pred, CV_8U).var() return mae + alpha * (trueLap - predLap) * (trueLap - predLap)
def frames_mono_blurred_laplacian(self, index): """ Look up a Laplacian-of-Gaussian of a frame object with a given index. :param index: Frame index :return: LoG of a frame with index "index". """ if not 0 <= index < self.number: raise ArgumentError("Frame index " + str(index) + " is out of bounds") # print("Accessing LoG number " + str(index)) # The LoG frames are buffered, and this frame has been stored before. Just return the frame. if self.frames_monochrome_blurred_laplacian[index] is not None: return self.frames_monochrome_blurred_laplacian[index] # If the blurred frame is cached, just return it. if self.laplacian_available_index == index: return self.laplacian_available # The frame has not been stored for re-use, compute it. else: # Get the monochrome frame. If it is not cached, this involves I/O. frame_monochrome_blurred = self.frames_mono_blurred(index) # Compute a version of the frame with Gaussian blur added. frame_monochrome_laplacian = convertScaleAbs(Laplacian( frame_monochrome_blurred[::self.configuration. align_frames_sampling_stride, ::self. configuration. align_frames_sampling_stride], CV_32F), alpha=self.alpha) # If the blurred frames are buffered, store the current frame at the current index. if self.buffer_laplacian: self.frames_monochrome_blurred_laplacian[ index] = frame_monochrome_laplacian # If frames are not buffered, cache the current frame. else: self.laplacian_available_index = index self.laplacian_available = frame_monochrome_laplacian return frame_monochrome_laplacian
def tile_is_background_1(image, threshold=0.85): ''' returns True if tile (np array) is background. An <image> is classified as background if the proportion of pixel colors (gray scale, 0-255) within a range of +-10 from the histogram mode is over <threshold>. inputs: - image: numpy array corresponding to RGB image - threshold: if (pixels in rng / total pixels) is higher than threshold, images is classified as background ''' is_bkgnd = False hist = calcHist(images=[cvtColor(image, COLOR_RGB2GRAY)], channels=[0], mask=None, histSize=[256], ranges=[0, 256]) mode = np.argmax(hist) if np.sum(hist[mode-10:mode+10])/np.sum(hist) > threshold: is_bkgnd = True abs_lap = np.absolute(Laplacian(cvtColor(image, COLOR_RGB2GRAY), CV_64F)) if abs_lap.max() < 40: is_bkgnd = True return hist, is_bkgnd
def add_monochrome(self, color): """ Create monochrome versions of all frames. Add a list of monochrome frames "self.frames_mono". If the original frames are monochrome, just point the monochrome frame list to the original images (no deep copy!). Also, add a blurred version of the frame list (using a Gaussian filter) "self.frames_mono_blurred", and the Laplacian of that image. :param color: Either "red" or "green", "blue", or "panchromatic" :return: - """ if self.color: colors = ['red', 'green', 'blue', 'panchromatic'] if not color in colors: raise ArgumentError("Invalid color selected for channel extraction") self.frames_monochrome = [] elif self.depth == 8: self.frames_monochrome = self.frames_original self.frames_monochrome_blurred = [] self.frames_monochrome_blurred_laplacian = [] for frame_index, frame in enumerate(self.frames_original): # After every "signal_step_size"th frame, send a progress signal to the main GUI. if self.progress_signal is not None and frame_index % self.signal_step_size == 0: self.progress_signal.emit("Gaussians / Laplacians", int((frame_index / self.number) * 100.)) # If frames are in color mode, or the depth is 16bit, produce a 8bit B/W version. if self.color or self.depth != 8: # If frames are in color mode, create a monochrome version with same depth. if self.color: if color == 'panchromatic': frame_mono = cvtColor(frame, COLOR_BGR2GRAY) else: frame_mono = frame[:, :, colors.index(color)] else: frame_mono = frame # If depth is larger than 8bit, reduce the depth to 8bit. if self.depth != 8: frame_mono = ((frame_mono) / 255.).astype(np.uint8) self.frames_monochrome.append(frame_mono) # Add a version of the frame with Gaussian blur added. frame_monochrome_blurred = GaussianBlur(self.frames_monochrome[frame_index], ( self.configuration.frames_gauss_width, self.configuration.frames_gauss_width), 0) self.frames_monochrome_blurred.append(frame_monochrome_blurred) # Compute the scaling factor for "convertScaleAbs" depending on the data type of the # monochrome image. The Laplacian is 8bit. If the monochrome image is 16bit, values # have to be scaled down. # Add the Laplacian of the down-sampled blurred image. if self.configuration.rank_frames_method == "Laplace": self.frames_monochrome_blurred_laplacian.append(convertScaleAbs(Laplacian( frame_monochrome_blurred[::self.configuration.align_frames_sampling_stride, ::self.configuration.align_frames_sampling_stride], CV_32F), alpha=1)) if self.progress_signal is not None: self.progress_signal.emit("Gaussians / Laplacians", 100)
from cv2 import cvtColor, inRange, COLOR_BGR2GRAY, CV_64F, COLOR_BGR2HSV from cv2 import threshold, THRESH_BINARY, THRESH_OTSU, erode from cv2 import bitwise_and, bitwise_not, bitwise_or from numpy import dstack, absolute, uint8, full, array from matplotlib.pyplot import imshow INPUT_FOLDER = '/home/jovyan/work/INPUT/' OUTPUT_FOLDER = '/home/jovyan/work/OUTPUT/' list_of_files = listdir(INPUT_FOLDER) for filename in list_of_files: # read image original = imread(INPUT_FOLDER + filename) gray = cvtColor(original, COLOR_BGR2GRAY) # create threshold for hairs and hairy clothes filtered = uint8(absolute(Laplacian(gray, CV_64F))) r, light_thresh = threshold(filtered, 0, 255, THRESH_BINARY + THRESH_OTSU) # create threshold for skin areas lower = array([0, 10, 80], dtype="uint8") upper = array([20, 255, 255], dtype="uint8") hsv = cvtColor(original, COLOR_BGR2HSV) skin = inRange(hsv, lower, upper) ret, medium_thresh = threshold(skin, 0, 255, THRESH_BINARY + THRESH_OTSU) # create threshold for dark areas blur = GaussianBlur(gray, (7, 7), 1) ret, dark_thresh = threshold(blur, 110, 255, THRESH_BINARY) dark_thresh = bitwise_not(dark_thresh) dark_thresh = erode(dark_thresh, (5, 5), iterations=3) ellipse = getStructuringElement(MORPH_ELLIPSE, (35, 35))
from cv2 import imread, cvtColor, COLOR_BGR2RGB, Canny, Laplacian, Sobel, CV_8U, medianBlur, GaussianBlur, blur from numpy import array import matplotlib.pyplot as plt img = imread('../resource/inception.jpg') img_rgb = cvtColor(img, COLOR_BGR2RGB) # img_rgb_blur = blur(img_rgb, (3, 3)) img_rgb_blur = GaussianBlur(img_rgb, (3, 3), 8) # Gaussian # img_rgb_blur = medianBlur(img_rgb, 3) # median img_can = Canny(img_rgb, 180, 220, apertureSize=3) img_can_blur = Canny(img_rgb_blur, 180, 220, apertureSize=3) img_lap = Laplacian(img_rgb, CV_8U) img_sob = Sobel(img_rgb, CV_8U, 1, 1) plt.subplot(231) plt.title('Original image') plt.imshow(img_rgb) plt.subplot(232) plt.title('Canny') plt.imshow(img_can) plt.subplot(233) plt.title('Canny (with blur)') plt.imshow(img_can_blur) plt.subplot(223) plt.title('Laplacian') plt.imshow(img_lap)
def compute_blurriness_metric(image_channel: torch.Tensor) -> float: """ compute bluriness metric as variance of laplacian """ lapl = Laplacian(image_channel.cpu().numpy(), 5) bluriness = lapl.var() return bluriness
from cv2 import imread, cvtColor, COLOR_BGR2RGB, Laplacian, Sobel, CV_32F import matplotlib.pyplot as plt img = imread('../resource/target.png') img_rgb = cvtColor(img, COLOR_BGR2RGB) img_lap = Laplacian(img_rgb, CV_32F) img_sob = Sobel(img_rgb, CV_32F, 1, 1) plt.subplot(131) plt.title('Original image') plt.imshow(img_rgb) plt.subplot(132) plt.title('Laplacian') plt.imshow(img_lap) plt.subplot(133) plt.title('Sobel') plt.imshow(img_sob) plt.show()
def calculate_variance_of_laplacian(self, image): """ The function calculates the Laplacian of the source image. It highlights regions of rapid intensity change and is therefore often used for edge detection. """ # CV_64F bc we need a floating point data type for negative values. An unsigned 8-bit integer cannot handle negative values. return Laplacian(image, CV_64F).var()
def laplacian(self, image): img_gray = cvtColor(image, COLOR_RGB2GRAY) img_sobel = Laplacian(img_gray, CV_16U) return mean(img_sobel)[0]
def image_blurness(pixels): pixels = cvtColor(pixels, COLOR_BGR2GRAY) fm = Laplacian(pixels, CV_64F).var() return fm