def process(img_name): """ 处理一张图片 """ bak_path = os.path.join(bak_dir, img_name) img_path = os.path.join(img_dir, img_name) # 备份 with open(img_path, 'rb') as fr, open(bak_path, 'wb') as fw: fw.write(fr.read()) # 读取 img = cv2.imread(img_path) # 缩小 h, w = img.shape[: 2] if w > W: h = h * W // w w = W img = cv2.resize(img, (w, h),interpolation=cv2.INTER_CUBIC) # 滤波 img = cv2.bilateralFilter(img, 40, 30, 75) # 转为灰度 img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # 过滤背景 img[img > 200] = 255 # 文字增强 img[img < 30] = 0 # 展示 cv2.imshow('im', img) cv2.waitKey(0) # 写入 cv2.imwrite(img_path, img)
def cartoon(self, image): ''' Cartoonise Image! Datatypes: image:nparray format BGR ''' numdown, numbilateral = 2, 7 color = image for _ in range(numdown): color = cv2.pyrDown(color) for _ in range(numbilateral): color = cv2.bilateralFilter(color, d=9, sigmaColor=9, sigmaSpace=7) for _ in range(numdown): color = cv2.pyrUp(color) cartoon = cv2.bitwise_and( color, cv2.cvtColor( cv2.adaptiveThreshold(cv2.medianBlur( cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), 7), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2), cv2.COLOR_GRAY2RGB)) cartoon = cv2.cvtColor(cartoon, cv2.COLOR_BGR2RGB) return cartoon
def __findNumberPlate__(self, image): ''' Handles all the pre-processing of image before passing it to Tesseract OCR ''' resizedImage = imutils.resize(image, width=1000) grayImage = cv2.cvtColor(resizedImage, cv2.COLOR_BGR2GRAY) filteredImage = cv2.bilateralFilter(grayImage, 11, 17, 17) cannyEdges = cv2.Canny(filteredImage, 170, 200) contours, _ = cv2.findContours(cannyEdges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) sortedContours = sorted(contours, key=cv2.contourArea, reverse=True)[:30] #cv2.drawContours(cannyEdges, sortedContours, 0, 255, -1) #cv2.imshow("Top 30 Contours", cannyEdges) #Show the top 30 contours. #cv2.waitKey(0) NumberPlateCount = 0 for contour in sortedContours: perimeter = cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, 0.02*perimeter, True) if len(approx) == 4: NumberPlateCount = approx break imageMask = np.zeros(grayImage.shape, np.uint8) x = cv2.drawContours(imageMask, [NumberPlateCount], 0, 255, -1) finalImage = cv2.bitwise_and(resizedImage, resizedImage, mask=imageMask) cv2.imwrite('numberPlateImage.jpg', finalImage) #finalGrayImage = cv2.cvtColor(finalImage, cv2.COLOR_BGR2GRAY) _, thresh2 = cv2.threshold(finalImage, 127, 255, cv2.THRESH_BINARY) cv2.imshow("Detected Number Plate", imutils.resize(finalImage, width=200)) cv2.waitKey(0) return thresh2
def data_augment(xb, yb): if np.random.random() < 0.25: xb, yb = rotate(xb, yb, 90) if np.random.random() < 0.25: xb, yb = rotate(xb, yb, 180) if np.random.random() < 0.25: xb, yb = rotate(xb, yb, 270) if np.random.random() < 0.25: xb = cv2.flip(xb, 1) # flipcode > 0:沿y轴翻转 yb = cv2.flip(yb, 1) if np.random.random() < 0.25: xb = random_gamma_transform(xb, 1.0) if np.random.random() < 0.25: xb = blur(xb) # 双边过滤 if np.random.random() < 0.25: xb = cv2.bilateralFilter(xb, 9, 75, 75) # 高斯滤波 if np.random.random() < 0.25: xb = cv2.GaussianBlur(xb, (5, 5), 1.5) if np.random.random() < 0.2: xb = add_noise(xb) return xb, yb
def process_frame(self, frame): # Normalizing our image frame = frame / 255 frame[frame > 255] = 255 # Convert to a type we can threshold with frame = frame.astype(np.uint8) frame = cv2.bilateralFilter(frame, 9, 70, 70) # Convert to image converted_frame_img = Image.fromarray(np.uint8(frame)) frame_enhanced = ImageEnhance.Contrast(converted_frame_img) # Enhance contrast contrast = 3 frame = frame_enhanced.enhance(contrast) sharpness = 3 frame = ImageEnhance.Sharpness(frame) frame = frame.enhance(sharpness) frame = Image.fromarray(np.uint8(frame)) return frame
def cartonize_image(our_image): new_img = np.array(our_image.convert('RGB')) img = cv2.cvtColor(new_img, 1) gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray, 5) edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9) color = cv2.bilateralFilter(img, 9, 300, 300) cartoon = cv2.bitwise_and(color, color, mask=edges) return cartoon
def Bilateral_filter(self): img = cv.imread('Cat.png') blur = cv.bilateralFilter(img,9,90,90) plt_img = img [:,:,::-1] plt.figure('origin') plt.imshow(plt_img) plt_blur = blur[:,:,::-1] plt.figure('Bilateral') plt.imshow(plt_blur) plt.show()
def get_corners(painting_roi, draw=False): gray = cv2.cvtColor(painting_roi, cv2.COLOR_BGR2GRAY) blur = cv2.bilateralFilter(gray, 9, 75, 75) erosion = cv2.erode(blur, np.ones((9, 9), np.uint8), iterations=2) dilation = cv2.dilate(erosion, np.ones((9, 9), np.uint8), iterations=2) _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # edges = auto_canny(thresh) h, w = thresh.shape[:2] mask = np.zeros((h + 2, w + 2), np.uint8) flood = thresh.copy() cv2.floodFill(flood, mask, (0, 0), 255) im_floodfill_inv = cv2.bitwise_not(flood) im_out = thresh | im_floodfill_inv contours, _ = cv2.findContours(im_out, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # find the biggest countour (c) by the area c = max(contours, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(c) bbox = x, y, w, h corners = cv2.goodFeaturesToTrack(im_out[y - 10:y + h + 10, x:x + w + 10], 4, 0.01, painting_roi.shape[0] / 3, useHarrisDetector=True) corners = np.int0(corners) corners = np.squeeze(corners) corners_img = painting_roi.copy() # draw the biggest contour (c) in green cv2.rectangle(corners_img, (x, y), (x + w + 10, y + h + 10), (0, 255, 0), 2) if draw: for i, corner in enumerate(corners): x_corner, y_corner = corner.ravel() cv2.circle(corners_img, (x_corner, y_corner), 3, 255, -1) cv2.putText(corners_img, f'{i}', (x_corner + 3, y_corner + 3), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color=(255, 0, 0)) cv2.imshow("corners", corners_img) # for corner in corners: # corner[0] += x # corner[1] += y return corners, bbox
def toCarttonStyle(picturePath): # 设置输入输出路径和文件名称 imgInput_FileName = picturePath imgOutput_FileName = 'res.jpg' # 属性设置 num_down = 2 # 缩减像素采样的数目 num_bilateral = 7 # 定义双边滤波的数目 # 读取图片 img_rgb = cv2.imread(imgInput_FileName) # 用高斯金字塔降低取样 img_color = img_rgb for _ in range(num_down): img_color = cv2.pyrDown(img_color) # 重复使用小的双边滤波代替一个大的滤波 for _ in range(num_bilateral): img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7) # 升采样图片到原始大小 for _ in range(num_down): img_color = cv2.pyrUp(img_color) # 转换为灰度并且使其产生中等的模糊 img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY) img_blur = cv2.medianBlur(img_gray, 7) # 检测到边缘并且增强其效果 img_edge = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2) # 算法处理后,照片的尺寸可能会不统一 # 把照片的尺寸统一化 height=img_rgb.shape[0] width = img_rgb.shape[1] img_color=cv2.resize(img_color,(width,height)) # 转换回彩色图像 img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) img_cartoon = cv2.bitwise_and(img_color, img_edge) # 保存转换后的图片 cv2.imwrite(imgOutput_FileName, img_cartoon) print('文件转换成漫画成功,保存在' + imgOutput_FileName)
def cartoonize(image): """ convert image into cartoon-like image image: input PIL image """ output = np.array(image) x, y, c = output.shape # hists = [] for i in range(c): output[:, :, i] = cv2.bilateralFilter(output[:, :, i], 5, 50, 50) # hist, _ = np.histogram(output[:, :, i], bins=np.arange(256+1)) # hists.append(hist) edge = cv2.Canny(output, 100, 200) output = cv2.cvtColor(output, cv2.COLOR_RGB2HSV) hists = [] # H hist, _ = np.histogram(output[:, :, 0], bins=np.arange(180 + 1)) hists.append(hist) # S hist, _ = np.histogram(output[:, :, 1], bins=np.arange(256 + 1)) hists.append(hist) # V hist, _ = np.histogram(output[:, :, 2], bins=np.arange(256 + 1)) hists.append(hist) C = [] for h in hists: C.append(k_histogram(h)) print("centroids: {0}".format(C)) output = output.reshape((-1, c)) for i in range(c): channel = output[:, i] index = np.argmin(np.abs(channel[:, np.newaxis] - C[i]), axis=1) output[:, i] = C[i][index] output = output.reshape((x, y, c)) output = cv2.cvtColor(output, cv2.COLOR_HSV2RGB) contours, _ = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) # for i in range(len(contours)): # tmp = contours[i] # contours[i] = cv2.approxPolyDP(tmp, 2, False) cv2.drawContours(output, contours, -1, 0, thickness=1) return output
def get_image_contours(image): """ Extracts contours from the image. Image is converted to a grayscale and a bilateral filter is applied to reduce noise and preserve edges. Then a canny filter is used to catch strong edges, contours are grabbed and the ten largest contours are retrieved based on area. """ image_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) image_filtered = cv.bilateralFilter(image_gray, 15, 75, 75) image_edges = cv.Canny(image_filtered, 30, 200) contours = cv.findContours(image_edges.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(contours) contours = sorted(contours, key=cv.contourArea, reverse=True)[:10] return contours
def detectCircles(img, param1=200, param2=90, minRadius=10, maxRadius=130, bias=15): roi = img.copy() img = cv.bilateralFilter(img, 9, 75, 75) # Smoothing gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # gray = cv.medianBlur(gray, 5) # gray = cv.equalizeHist(gray) circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, 20, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius) if circles is not None: circles = np.int32(np.around(circles)) rs = [] for c in circles[0, :]: rs.append(c[2]) max_id = rs.index(max(rs)) for c in circles[0, :]: if rs[max_id] == c[2]: # cv.circle(img, (c[0],c[1]), c[2], (0,0,255), 2) # cv.circle(img, (c[0],c[1]), 2, (255,0,0), 2) # 矩形框选 ix = c[0] - c[2] - bias iy = c[1] - c[2] - bias ex = c[0] + c[2] + bias ey = c[1] + c[2] + bias if ix == -bias and iy == -bias and ex == bias and ey == bias: return None, None, None if c[2] > 30: # > 90 return 'locked', roi[iy:ey + 1, ix:ex + 1], [ix, iy, ex, ey] else: return 'captured', roi[iy:ey + 1, ix:ex + 1], [ix, iy, ex, ey] else: cv.imshow('camera', img) return None, None, None
def __blur(src, type, radius): """Softens an image using one of several filters. Args: src: The source mat (numpy.ndarray). type: The blurType to perform represented as an int. radius: The radius for the blur as a float. Returns: A numpy.ndarray that has been blurred. """ if (type is BlurType.Box_Blur): ksize = int(2 * round(radius) + 1) return cv2.blur(src, (ksize, ksize)) elif (type is BlurType.Gaussian_Blur): ksize = int(6 * round(radius) + 1) return cv2.GaussianBlur(src, (ksize, ksize), round(radius)) elif (type is BlurType.Median_Filter): ksize = int(2 * round(radius) + 1) return cv2.medianBlur(src, ksize) else: return cv2.bilateralFilter(src, -1, round(radius), round(radius))
def get_paper_contour(self, orig): # resize the image for faster computation. image = orig.copy() ratio = image.shape[0] / 500.0 image = imutils.resize(image, height = 500) # Process the image to find edges gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.bilateralFilter(gray, 11, 17, 17) edges = cv2.Canny(gray, 50, 150) if self.PRINT_PROCESS: self.showarr(gray) self.showarr(edges) # Get the ballotContour from the edges -- the largest connected rectangle. _, contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = sorted(contours, key = cv2.contourArea, reverse=True)[:5] ballotContour = None for c in contours: # approximate the contour perimeter = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * perimeter, True) # if our approximated contour has four points, then # we can assume that we have found our screen if len(approx) == 4: ballotContour = approx break if ballotContour is None: raise ValueError('Could not determine ballot border. Make sure the ballot is on a solid dark background, fully in the image.') # Draw the contour if self.PRINT_PROCESS: cv2.drawContours(image, [ballotContour], -1, (0, 255, 0), 1) self.showarr(image) # Reshape, scale, and return ballotContour = ballotContour.reshape(4,2) * ratio return ballotContour
def get_diamonds(image): """get the points of the diamonds from the supplied GRAYSCALE image :param image: image in GRAYSCALE :type image: cv2 image :return: list of diamonds as contours :rtype: float32 """ # threshold the image thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 47, 1) # blur the image blurred = filters.apply_gaussian_blur(thresh, 9) bil = cv2.bilateralFilter(blurred, 11, 60, 60) # detect the edges canny = apply_canny(bil, 175, 190) # find the contours, in this case it should only find the diamonds return apply_find_contours(canny, 4, 80000, 5)
def clahe(img, disp=False): """ Apply CLACHE algorithm to image :param img: Input image of RGB format :param disp: Control display flag :output: filtered image """ res = [] clahe = cv2.createCLAHE(18, tileGridSize=(21, 21)) for channel in np.dsplit(img, img.shape[-1]): res.append(clahe.apply(channel)) res = np.dstack(res) k = 5 t = 5 res = cv2.GaussianBlur(res, (k, k), 0) # Apply filter res = cv2.bilateralFilter(res, t, 75, 75) if disp: cv2.imshow("result", res) return res
def clahe_tune(img): """ Apply CLACHE algorithm to image with trackbars to tune :param img: Input image of RGB format :output: filtered image """ res = [] tg = cv2.getTrackbarPos("tg", "track") + 1 clahe = cv2.createCLAHE(clipLimit=cv2.getTrackbarPos("clip", "track"), tileGridSize=(tg, tg)) for channel in np.dsplit(img, img.shape[-1]): res.append(clahe.apply(channel)) res = np.dstack(res) k = 2 * cv2.getTrackbarPos("kernel", "track") - 1 t = cv2.getTrackbarPos("t", "track") res = cv2.GaussianBlur(res, (k, k), 0) # Apply filter res = cv2.bilateralFilter(res, t, 75, 75) cv2.imshow("result", res) return res
def cartoonizer(img, num_down=2, num_bi=5): #Params #num_down = 2 #DOWNSAMPLE STEPS #num_bi = 5 # BILATERAL FILTERING STEPS img_c = img for ix in range(num_down): img_c = cv2.pyrDown(img) # Pyramid Down : Downsampling # print(img_c.shape) for iy in range(num_bi): img_c = cv2.bilateralFilter(img_c, d=9, sigmaColor=9, sigmaSpace=7) #Filtering # print(img_c.shape) #UPSAMPLING for ix in range(num_down): img_c = cv2.pyrUp(img_c) # Pyramid Down : Downsampling # print(img_c.shape) #BLUR and Threshold img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # GRAY SCALE img_blur = cv2.medianBlur(img_gray, 7) #MEDIAN BLUR img_edge = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2) img_c = cv2.resize(img_c, (800, 800)) #RGB CONVERSION + BITWISE & img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) # print(img_c.shape) # print(img_edge.shape) img_cartoon = cv2.bitwise_and(img_c, img_edge) stack = np.hstack([img, img_cartoon]) return stack
def processImage(image): sketch = cv.imread(image) #sketch_flipped = cv.flip(sketch,0) imgray = cv.cvtColor(sketch, cv.COLOR_BGR2GRAY) imgray = cv.GaussianBlur(imgray, (5, 5), 0) imgray = cv.bilateralFilter(imgray, 11, 17, 17) cannyImg = getCanny(imgray) ret, thresh = cv.threshold(cannyImg, 127, 255, 0) # opencv function that finds all the edges (contours) in the image contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # list of coordinates generated by the findCountours function. # there will be an even number of numbers in the coords list, since each coordinate's # x coordinate is placed in the list, then the y coordinate is placed in the list coords = list() numVertices = len(contours[0]) for i in range(0, len(contours[0])): # x coordinate coords.append(contours[0][i][0][0]) # y coordinate coords.append(contours[0][i][0][1]) cv.waitKey(0) cv.destroyAllWindows() return coords
def detect(self, img,\ lower_bound=np.array([110, 55, 5]),\ upper_bound=np.array([130, 255, 255])): ''' Detect switch ROI in an image. @img: np.array, input image. @lower_bound: np.array, lower bound for HSV color cut. @upper_bound: np.array, upper bound for HSV color cut. @return: (np.array, list), ROI image with bounding box and ROI bounding boxes, each is a tuple like (x, y, dx, dy). ''' # Replace R and B channel blurred_img = cv.GaussianBlur( img, (5, 5), 5) # Gaussian smoothing to remove noisy points b, g, r = cv.split(blurred_img) inv_img = cv.merge([r, g, b]) hsv = cv.cvtColor(inv_img, cv.COLOR_BGR2HSV) # Crop for color mask mask = cv.inRange(hsv, lower_bound, upper_bound) # Crop for ROI h, s, v = cv.split(hsv) v = cv.bilateralFilter(v, 0, 15, 15) _, v = cv.threshold(v, 0, 255, cv.THRESH_OTSU) kernel = np.ones((5, 5), np.uint8) v = cv.morphologyEx(v, cv.MORPH_ERODE, kernel) # cv.imshow('value', v) points = self._get_pos_for_mask(v) xs, ys, dx, dy = cv.boundingRect(points) # img = cv.rectangle(img, (xs, ys), (xs+dx, ys+dy), (255,0,0), 2) roi_mask = mask[ys:ys + dy, xs:xs + dx] kernel = np.ones((3, 3), np.uint8) roi_mask = cv.morphologyEx(roi_mask, cv.MORPH_CLOSE, kernel) # Trimming roi = img[ys:ys + dy, xs:xs + dx] _, contours, _ = cv.findContours(roi_mask, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) # centers = [] boxes = [] for i in range(len(contours)): # roi = cv.drawContours(roi, contours, i, (255,0,0), 1) points = [x[0] for x in contours[i]] area = cv.contourArea(contours[i]) # filter out small areas if area < self._min_area or area > self._max_area: continue xs, ys, dx, dy = cv.boundingRect(np.array(points)) # centers.append(center) boxes.append((xs, ys, dx, dy)) # debug # cv.imshow('mask', mask) # cv.imshow('roi', roi) # cv.imshow('roi_mask', roi_mask) # cv.imshow('hue', h) # cv.imshow('saturation', s) # cv.imshow('intensity', v) return roi, boxes
def generate_saturation_contours(img_s, blur_kernel, mask, edge_mask, min_size, max_size, erode_iter, max_dilate_percentage, blur_type='gaussian'): img_shape = img_s.shape if blur_type == 'gaussian': img_s_blur = cv2.GaussianBlur(img_s, blur_kernel, 0, 0) elif blur_type == 'bilateral': img_s_blur = cv2.bilateralFilter(img_s, blur_kernel[0], blur_kernel[0], blur_kernel[0]) else: raise ValueError("%s isn't a supported blur type" % blur_type) med = np.median(img_s_blur[img_s_blur > 0]) img_s_blur = cv2.bitwise_and(img_s_blur, img_s_blur, mask=mask) mode_s = cv2.inRange(img_s_blur, med, 255) mode_s = cv2.erode(mode_s, block_strel, iterations=erode_iter) good_contours = cv2x.filter_contours_by_size(mode_s, min_size=min_size, max_size=max_size) # update the signal mask by removing the previous color candidates edge_mask = cv2.bitwise_and(edge_mask, edge_mask, mask=mask) contours = cv2x.filter_contours_by_size(edge_mask, min_size=21 * 21) edge_mask = np.zeros(img_shape, dtype=np.uint8) cv2.drawContours(edge_mask, contours, -1, 255, -1) edge_mask = cv2.dilate(edge_mask, (3, 3), iterations=2) good_sat_val_contours = [] for i, c in enumerate(good_contours): filled_c_mask = np.zeros(img_shape, dtype=np.uint8) cv2.drawContours(filled_c_mask, [c], -1, 255, cv2.FILLED) new_mask, signal, orig = cv2x.find_border_by_mask( edge_mask, filled_c_mask, max_dilate_percentage=max_dilate_percentage, dilate_iterations=2) if not orig and signal > 0.7: _, contours, _ = cv2.findContours(new_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) good_sat_val_contours.append(contours[0]) elif signal > 0.7: good_sat_val_contours.append(c) else: pass # ignore contour # The previous contours represent the final contour count, # but not their final shape/size. The final step is to use approxPolyDP, followed # by a few more dilations, but we won't re-draw and extract the contours, as that # would possibly connect some of them together. However, we will draw them for # the mask to exclude these regions from the next group of candidates final_sat_val_contours = [] for c in good_sat_val_contours: peri = cv2.arcLength(c, True) smooth_c = cv2.approxPolyDP(c, 0.0035 * peri, True) single_cnt_mask = np.zeros(img_shape, dtype=np.uint8) cv2.drawContours(single_cnt_mask, [smooth_c], 0, 255, -1) # erode & dilate single_cnt_mask = cv2.dilate(single_cnt_mask, circle_strel, iterations=10) _, tmp_contours, _ = cv2.findContours(single_cnt_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) final_sat_val_contours.append(tmp_contours[0]) return final_sat_val_contours, edge_mask
# while(1): # ret, frame = cap.read() # fgmask = fgbg.apply(frame) # cv2.imshow('frame',fgmask) # k = cv2.waitKey(30) & 0xff # if k == 27: # break # cap.release() # cv2.destroyAllWindows() list_filepaths = [] for filename in os.listdir(r"background"): filepath = os.path.join(r"background", filename) list_filepaths.append(filepath) if not os.path.isdir("background_blur"): os.mkdir("background_blur") list_filepaths_blur = [] for filepath in list_filepaths: img = cv2.imread(filepath) filename = os.path.basename(filepath) blur = cv2.bilateralFilter(img,9,75,75) new_filepath = os.path.join(r"background_blur", filename) cv2.imwrite(new_filepath, blur) list_filepaths_blur.append(new_filepath)
def rescaleFrame(frame, scale=0.75): width = int(frame.shape[1] * scale) height = int(frame.shape[0] * scale) dimensions = (width, height) return cv.resize(frame, dimensions, interpolation=cv.INTER_AREA) init = cv.imread('imagen/wppstalkerlarge.jpg') img = rescaleFrame(init, 0.3) cv.imshow('stalker', img) #Averaging blur average = cv.blur(img, (3, 3)) cv.imshow('averageblur', average) #Gaussian blur gauss = cv.GaussianBlur(img, (3, 3), 0) cv.imshow('gaussianBlur', gauss) #Median Blur (good for Computer Vision) medianBlur = cv.medianBlur(img, 3) cv.imshow('medianBlur', medianBlur) #Bilateral Blur (good as well for Computer Vision) biBlur = cv.bilateralFilter(img, 10, 35, 25) cv.imshow('bilateralBlur', biBlur) cv.waitKey(0)
import cv2.cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread("imgs/sap.png") img = cv.cvtColor(img, cv.COLOR_BGR2RGB) kernel = np.ones((5, 5), np.float32) / 25 dst = cv.filter2D(img, -1, kernel) blur = cv.blur(img, (5, 5)) gauss = cv.GaussianBlur(img, (5, 5), 0) median = cv.medianBlur(img, 5) bilatera = cv.bilateralFilter(img, 9, 75, 75) titles = [ 'original image', '2D convolution', 'blur', 'Gaussian', 'Median blur', 'Bilatera filter' ] image = [img, dst, blur, gauss, median, bilatera] for i in range(len(titles)): plt.subplot(2, 3, i + 1), plt.imshow(image[i], "gray") plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show()
def generate_dataset(self): if self.var_emp_Department.get( ) == "Select Department" or self.var_emp_Name.get( ) == "" or self.va_Emp_Id.get() == "": messagebox.showerror("Error", "All Fields are required", parent=self.root) else: try: conn = mysql.connector.Connect(host="localhost", username="******", password="******", database="face_recognizer") my_cursor = conn.cursor() my_cursor.execute("select * from emp_table") myresult = my_cursor.fetchall() id = 0 for x in myresult: id += 1 my_cursor.execute( "update emp_table set Name=%s,Department=%s,DOB=%s,DOJ=%s,Gender=%s,Proof_type=%s,Proof_number=%s,Address=%s,Email=%s,Contact_no=%s,PhotoSample=%s where emp_id=%s", (self.var_emp_Name.get(), self.var_emp_Department.get(), self.var_emp_Dob.get(), self.var_emp_Doj.get(), self.var_emp_Gender.get(), self.var_emp_Proof_type.get(), self.var_emp_Proof_Number.get(), self.var_emp_Address.get(), self.var_emp_Email.get(), self.var_emp_Contact_No.get(), self.var_radio1.get(), self.va_Emp_Id.get() == id + 1)) conn.commit() self.fetch_data() self.reset_data() conn.close() # Load Predefined data on frontal face from opencv # Initializing the face and eye cascade classifiers from xml files face_cascade = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier( 'haarcascade_eye_tree_eyeglasses.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') recognizer = cv2.face.LBPHFaceRecognizer_create() train_dataset = recognizer.read('trainer/trainer.yml') id = 2 #two persons names = [ '', 'unknownone', 'unknown', 'abc', 'xyz' ] #key in names, start from the second place, leave first empty font = cv2.FONT_HERSHEY_SIMPLEX face_id = input('\n enter user id end press <return> ==> ') # face_id = sys.argv[1] # Variable store execution state first_read = True # Starting the video capture cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) ret, img = cap.read() count = 0 while (ret): ret, img = cap.read() # Coverting the recorded image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Applying filter to remove impurities gray = cv2.bilateralFilter(gray, 5, 1, 1) # Detecting the face for region of image to be fed to eye classifier faces = face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(200, 200)) if (len(faces) > 0): for (x, y, w, h) in faces: img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) id, confidence = recognizer.predict(gray[y:y + h, x:x + w]) # roi_face is face which is input to eye classifier roi_face = gray[y:y + h, x:x + w] roi_face_clr = img[y:y + h, x:x + w] eyes = eye_cascade.detectMultiScale(roi_face, 1.3, 5, minSize=(50, 50)) conn = mysql.connector.Connect( host="localhost", username="******", password="******", database="face_recognizer") my_cursor = conn.cursor() # Examining the length of eyes object for eyes if (len(eyes) >= 2): # Check if program is running for detection if (first_read): cv2.putText(img, "Eye detected press s", (70, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) else: count += 1 cv2.putText(img, "Eyes open!", (70, 70), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 255), 2) # print('click ' + str(count) + ' photo' + ' new face') if (train_dataset ) and train_dataset != False: id, confidence = recognizer.predict( gray[y:y + h, x:x + w]) if (confidence > "30%"): cv2.putText( img, 'Already in the dataset', (x + 50, y + w + 20), font, 1, (255, 255, 0), 2) else: print('click ' + str(count) + ' photo' + confidence + ' new face' + id) print(confidence) cv2.imwrite( "data/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w]) else: cv2.putText(img, 'New face was detected', (x + 50, y + w + 20), font, 1, (255, 255, 0), 1) print( 'click ' + str(count) + ' photo' + ' new face', confidence, id) cv2.imwrite( "data/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w]) else: if (first_read): # To ensure if the eyes are present before starting cv2.putText(img, "No eyes detected", (70, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 255), 2) else: cv2.waitKey(30) first_read = True else: cv2.putText(img, "No face detected", (100, 100), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) # Controlling the algorithm with keys cv2.imshow('img', img) a = cv2.waitKey(1) if (a == ord('q')): break elif (a == ord('s') and first_read): # This will start the detection first_read = False elif count >= 100: # Take 30 face sample and stop video break cap.release() cv2.destroyAllWindows() messagebox.showinfo("Result", "Generating data set complted !!!!") except Exception as es: messagebox.showerror("Error", f"Due to:{str(es)}", parent=self.root)
img_blur_2 = cv2.GaussianBlur(img_invert, (23, 23), 0) # The Dodge blend function divides the bottom layer by the inverted top layer. # This lightens the bottom layer depending on the value of the top layer. # We have the blurred image, which highlights the boldest edges. def dodgeV2(image, mask): # inverting color with 255 - ... return cv2.divide(image, 255 - mask, scale=256) final_img = dodgeV2(img_gray, img_blur) final_img_2 = dodgeV2(img_gray, img_blur_2) # final_img_2 = cv2.blur(final_img_2, (3, 3),0) final_img_2 = cv2.bilateralFilter(final_img_2, 9, 50, 50) # convert to bgr for showing the input and output in the same window # this will convert the output from 2 channel to 3 channel final_img = cv2.cvtColor(final_img, cv2.COLOR_GRAY2BGR) # concatenate both the input and output numpy_vertical_concat = np.concatenate((img, final_img), axis=1) # cv2.imshow('image', final_img) # cv2.imshow('image_2', final_img_2) # displaying the sketch image cv2.imshow('image', numpy_vertical_concat) print("Press 'Esc' button to exit or Press 's' to save the image and exit.") k = cv2.waitKey(0)
help="nearest multiple of 32 for resized height") ap.add_argument("-p", "--padding", type=float, default=0.1, help="amount of padding to add to each border of ROI") args = vars(ap.parse_args()) # load the input image and grab the image dimensions image = cv2.imread(args["image"]) orig = image.copy() image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) (thresh, image) = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) image = cv2.bilateralFilter(image, 9, 75, 75) # Remove horizontal and vertical lines horizontal_img = 255 - image vertical_img = 255 - image kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1)) horizontal_img = cv2.erode(horizontal_img, kernel, iterations=1) horizontal_img = cv2.dilate(horizontal_img, kernel, iterations=1) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 20)) vertical_img = cv2.erode(vertical_img, kernel, iterations=1) vertical_img = cv2.dilate(vertical_img, kernel, iterations=1) mask_img = vertical_img + horizontal_img image = np.bitwise_or(image, mask_img) (origH, origW) = orig.shape[:2]
# In this demo, we add a 50% noise to our original image and use a median filter. img_m = cv2.imread(r'pictures\median.jpg') img_m = cv2.cvtColor(img_m, cv2.COLOR_BGR2RGB) median_filtering = cv2.medianBlur(img_m, 5) # 中值过滤 # As we noted, the filters we presented earlier tend to blur edges. This is not the case for the bilateral filter, cv2.bilateralFilter(), which was defined for, and is highly effective at noise removal while preserving edges. But the operation is slower compared to other filters. We already saw that a Gaussian filter takes the a neighborhood around the pixel and finds its Gaussian weighted average. This Gaussian filter is a function of space alone, that is, nearby pixels are considered while filtering. It does not consider whether pixels have almost the same intensity value and does not consider whether the pixel lies on an edge or not. The resulting effect is that Gaussian filters tend to blur edges, which is undesirable. # The bilateral filter also uses a Gaussian filter in the space domain, but it also uses one more (multiplicative) Gaussian filter component which is a function of pixel intensity differences. The Gaussian function of space makes sure that only pixels are ‘spatial neighbors’ are considered for filtering, while the Gaussian component applied in the intensity domain (a Gaussian function of intensity differences) ensures that only those pixels with intensities similar to that of the central pixel (‘intensity neighbors’) are included to compute the blurred intensity value. As a result, this method preserves edges, since for pixels lying near edges, neighboring pixels placed on the other side of the edge, and therefore exhibiting large intensity variations when compared to the central pixel, will not be included for blurring. img_b = cv2.imread(r'pictures\bilateral.png') img_b = cv2.cvtColor(img_b, cv2.COLOR_BGR2RGB) bilateral_filtering = cv2.bilateralFilter(img_b, 9, 75, 75) # 双边过滤 plt.subplot(241), plt.imshow(img), plt.title('Original') plt.xticks([]), plt.yticks([]) plt.subplot(245), plt.imshow(avg_blur), plt.title('Avg_Blurred') plt.xticks([]), plt.yticks([]) plt.subplot(246), plt.imshow(g_blur), plt.title('Gaussian_Blurred') plt.xticks([]), plt.yticks([]) plt.subplot(243), plt.imshow(img_m), plt.title('Median_Original') plt.xticks([]), plt.yticks([]) plt.subplot(247), plt.imshow(median_filtering), plt.title('Median_Filtering') plt.xticks([]), plt.yticks([]) plt.subplot(244), plt.imshow(img_b), plt.title('Bilateral_Original') plt.xticks([]), plt.yticks([]) plt.subplot(248), plt.imshow(bilateral_filtering), plt.title( 'Bilateral_Filtering')
def bi_demo(image): dst = cv.bilateralFilter(image, 0, 150, 10) #cv.imshow('bilateralFilter',dst) return dst
import os.path as path def getPath(typeFile, file): folder = "Photos" if typeFile == 'img' else "Videos" return path.abspath(path.join(__file__, "../..", "Resources/"+folder, file)) img = cv.imread(getPath("img", "cats.jpg")) cv.imshow("Cats", img) # Averaging average = cv.blur(img, (3, 3)) cv.imshow("Cats_average", average) # Gaussian Blur gauss = cv.GaussianBlur(img, (3, 3), 0) cv.imshow("Gaussian blur", gauss) # Median blur median = cv.medianBlur(img, 3) cv.imshow("Cats_median", median) # Bilateral bilateral = cv.bilateralFilter(img, 10, 35, 35) cv.imshow("Cats_Bilateral", bilateral) cv.waitKey(0)