def gray_world(img: np.ndarray): b, g, r = cv2.split(img)[:3] b_mean, g_mean, r_mean = np.mean(b), np.mean(g), np.mean(r) k = (b_mean + g_mean + r_mean) / 3 kb, kg, kr = k / b_mean, k / g_mean, k / r_mean return cv2.merge( (cv2.addWeighted(b, kb, 0, 0, 0), cv2.addWeighted(g, kg, 0, 0, 0), cv2.addWeighted(r, kr, 0, 0, 0)))
def superSample1080(imgin4k): m = imgin4k l1 = m[0::2] l2 = m[1::2] m = cv2.addWeighted(l1, 0.5, l2, 0.5, 1) l1 = m[:, 0::2] l2 = m[:, 1::2] m = cv2.addWeighted(l1, 0.5, l2, 0.5, 1) return m
def draw_landmarks(image: np.array, landmarks: Landmarks, points=False, draw_bg=False, color=None) -> None: """Draws facial landmarks on the specified image.""" lm = landmarks alpha = Config.Landmarks.ALPHA if alpha <= 0.0: return if color is None: color = Config.Landmarks.BASE_COLOR mouth_color = Config.Landmarks.MOUTH_COLOR eye_color = Config.Landmarks.EYE_COLOR else: mouth_color = color eye_color = color if draw_bg: bg_alpha = 0.2 overlay = image.copy() cv2.fillPoly(overlay, Point.to_numpy(lm.outer_shape), color) cv2.addWeighted(overlay, bg_alpha, image, 1.0 - bg_alpha, 0, image) thickness = Config.Landmarks.THICKNESS line_type = Config.Landmarks.LINE_TYPE overlay = image.copy() if alpha < 1.0 else image for pts in [lm.chin, lm.left_eyebrow, lm.right_eyebrow, lm.nose_bridge, lm.nose_tip]: if points: for point in pts: cv2.circle(overlay, point, thickness, color, thickness, line_type) else: cv2.polylines(overlay, Point.to_numpy(pts), False, color, thickness, line_type) for pts in [lm.left_eye, lm.right_eye]: if points: for point in pts: cv2.circle(overlay, point, thickness, eye_color, thickness, line_type) else: cv2.polylines(overlay, Point.to_numpy(pts), True, eye_color, thickness, line_type) for pts in [lm.top_lip, lm.bottom_lip]: if points: for point in pts: cv2.circle(overlay, point, thickness, mouth_color, thickness, line_type) else: cv2.polylines(overlay, Point.to_numpy(pts), True, mouth_color, thickness, line_type) if alpha < 1.0: cv2.addWeighted(overlay, alpha, image, 1.0 - alpha, 0.0, image)
def stack_loop(nums=5, delay=0.2, auto_process=False): """ 拍摄堆栈图像 :param nums: 堆栈次数 :param delay: 每次拍摄的延时 """ global ACTIVE # 计数器和图片列表 current = 0 images = [] keyboard_response() cam = cv.VideoCapture(0, cv.CAP_DSHOW) while RUNNING: _, original = cam.read() # 展示窗口 cv.imshow('result', original) # 退出程序判断 if cv.waitKey(1) & 0xFF == ord('q'): break # 激活程序判断 if ACTIVE: # 连拍控制 current += 1 # print(f'progress: {current} / {nums}') if current >= nums: ACTIVE = False current = 0 # 载入列表 images.append(original) print(f'{len(images)} images loaded') # 等待 sleep(delay) # 完成连拍 if len(images) == nums: print('finished sampling') # 叠加堆栈 # 第一张图像 stacked = images[0] for image in images[1:]: cv.addWeighted(stacked, 0.5, image, 0.5, 0, stacked) images = [] cv.imwrite('./output/stacked.png', stacked) print('stack operation finished') # 自动处理 if auto_process: sleep(0.1) single_image('./output/stacked.png') cv.destroyAllWindows()
def sobel_extract(img): x = cv2.Sobel(img, cv2.CV_16S, 1, 0) # 1,0代表只计算x方向计算边缘 y = cv2.Sobel(img, cv2.CV_16S, 0, 1) # 0,1代表只在y方向计算边缘 absX = cv2.convertScaleAbs(x) absY = cv2.convertScaleAbs(y) dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) return dst
def Sobel_Filter(img): sobel_x = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=5) sobel_y = cv2.Sobel(img, cv2.CV_16S, 0, 1, ksize=5) sobel_x = cv2.convertScaleAbs(sobel_x) sobel_y = cv2.convertScaleAbs(sobel_y) sobel_xy = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0) return sobel_xy
def img_fusion(img1, img2): print(img1.shape, img2.shape) # img1 = cv.resize(img1, (640, 480)) # img2 = cv.resize(img2, (640, 480)) # cv.imshow("img1", img1) print(img1.shape) # 分离YUV数据 yuv1 = cv.cvtColor(img1, cv.COLOR_BGR2YCrCb) y1, u1, v1 = cv.split(yuv1) yuv2 = cv.cvtColor(img2, cv.COLOR_BGR2YCrCb) y2, u2, v2 = cv.split(yuv2) # cv.imshow("y1", y1) # cv.imshow("y2", y2) # Y值均衡化 clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) y2 = clahe.apply(y2) # Y像素范围压缩 img_pixels_range(y1, 128) img_pixels_range(y2, 128) cv.imshow("y1", y1) cv.imshow("y2", y2) # y3 = cv.add(y1, y2) y3 = cv.addWeighted(y1, 0.6, y2, 0.4, 0) cv.imshow("y3", y3) img3 = cv.merge([y3, u1, v1]) img3 = cv.cvtColor(img3, cv.COLOR_YCrCb2BGR) cv.imshow("img3", img3) cv.imwrite(r".04-picture-fusion_images/opencv.jpg", img3)
def USM(src): val = 3 blur = cv2.GaussianBlur(src, (7, 7), 3) val = 4 blur = cv2.GaussianBlur(src, (5, 5), 0) res = cv2.addWeighted(src, val, blur, 1.0 - val, 0) return res
def red_filtering(self, frame): # Adapted from # https://stackoverflow.com/questions/42840526/opencv-python-red-ball-detection-and-tracking # # convert the input stream into HSV color space hsv_conv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # then the image is blurred hsv_blurred_img = cv2.medianBlur(hsv_conv_img, 9, 3) # because hue wraps up and to extract as many "red objects" as possible, # we define lower and upper boundaries for brighter and for darker red shades bright_red_mask = cv2.inRange(hsv_blurred_img, self.bright_red_lower_bounds, self.bright_red_upper_bounds) dark_red_mask = cv2.inRange(hsv_blurred_img, self.dark_red_lower_bounds, self.dark_red_upper_bounds) # after masking the red shades out, I add the two images weighted_mask = cv2.addWeighted(bright_red_mask, 1.0, dark_red_mask, 1.0, 0.0) # then the result is blurred blurred_mask = cv2.GaussianBlur(weighted_mask, (9, 9), 3, 3) # some morphological operations (closing) to remove small blobs erode_element = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) dilate_element = cv2.getStructuringElement(cv2.MORPH_RECT, (8, 8)) eroded_mask = cv2.erode(blurred_mask, erode_element) dilated_mask = cv2.dilate(eroded_mask, dilate_element) return dilated_mask
def apply_sobel(img, *params): ddepth = cv2.CV_16S scale = 1 delta = 0 kernel_size = 3 img = cv2.GaussianBlur(img, (5, 5), cv2.BORDER_DEFAULT) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) grad_x = cv2.Sobel(img, ddepth, 1, 0, ksize=kernel_size, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT) grad_y = cv2.Sobel(img, ddepth, 0, 1, ksize=kernel_size, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT) abs_grad_x = cv2.convertScaleAbs(grad_x) abs_grad_y = cv2.convertScaleAbs(grad_y) new_image = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0) #new_image = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=3) return new_image
def draw_area(undist, binary_warped, Minv, left_fit, right_fit): """ Parameter: undist: binary_wraped: Minv: left_fit: right_fit: Return: result: """ # Generate x and y values for plotting ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0]) left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2] right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2] # Create an image to draw the lines on warp_zero = np.zeros_like(binary_warped).astype(np.uint8) color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) # Recast the x and y points into usable format for cv2.fillPoly() pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))]) pts_right = np.array( [np.flipud(np.transpose(np.vstack([right_fitx, ploty])))]) pts = np.hstack((pts_left, pts_right)) # Draw the lane onto the warped blank image cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0)) # Warp the blank back to original image space using inverse perspective matrix (Minv) newwarp = cv2.warpPerspective(color_warp, Minv, (undist.shape[1], undist.shape[0])) # Combine the result with the original image result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0) return result
def whiteBalance(img): r, g, b = cv2.split(img) r_avg = cv2.mean(r)[0] g_avg = cv2.mean(g)[0] b_avg = cv2.mean(b)[0] k = (r_avg + g_avg + b_avg) / 3 kr = k / r_avg kg = k / g_avg kb = k / b_avg r = cv2.addWeighted(src1=r, alpha=kr, src2=0, beta=0, gamma=0) g = cv2.addWeighted(src1=g, alpha=kg, src2=0, beta=0, gamma=0) b = cv2.addWeighted(src1=b, alpha=kb, src2=0, beta=0, gamma=0) balanceImg = cv2.merge([b, g, r]) return balanceImg
def _overlayBanner(self, pixmap, y, height, color, alpha): """ place a banner at y of the image, width = Imagewidth :param pixmap: an QPixmap Image :param y: position of the banner inside image :param height: height of the banner :param color: background color of the banner, e.g. (0, 0, 255) :param alpha: alpha of backgroundcolor :return: Resultant QPixmap """ Qimg = pixmap.toImage() img = self.QImage2MAT(Qimg) overlay = img.copy() output = img.copy() cv2.rectangle(overlay, (0, y), (pixmap.width(), y + height), color, -1) cv2.addWeighted(overlay, alpha, output, 1 - alpha, 1.0, output) return self.MAT2QPixmap(output)
def improve_photo(img): img_ = img.copy() img_ = cv2.detailEnhance(img_, sigma_s=20, sigma_r=0.15) img_ = cv2.edgePreservingFilter(img_, flags=1, sigma_s=60, sigma_r=0.15) img_ = cv2.stylization(img_, sigma_s=95, sigma_r=0.95) img = cv2.addWeighted(img, .8, img_, .7, .5) return img
def draw_mask_on_image(image: np.ndarray, mask: np.ndarray) -> np.ndarray: mask = (cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB) * (0, 255, 0)).astype( np.uint8) image_mask = cv2.addWeighted(image, 1, mask, 0.5, 0) image_mask = np.uint8(image_mask) return image_mask
def extract(img, left, right): dim_y = len(img) dim_x = len(img[0]) # cv2.imshow('', img) # cv2.waitKey(0) final_mask = np.zeros((dim_y, dim_x, 3), np.uint8) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # cv2.imshow('gray', gray) # cv2.waitKey(0) _, thresh = cv2.threshold(gray, 75, 255, cv2.THRESH_BINARY_INV) # cv2.imshow('thresh', thresh) # cv2.waitKey(0) l, r = 0, 0 t = 0 max_cnt = [] print("_____________") while r - l < right - left: t += 1 cnt = copy.deepcopy(thresh) # cv2.imshow('cnt', cnt) # cv2.waitKey(0) contours, hierarchy = cv2.findContours(cnt, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cv2.drawContours(cnt, contours, -1, 255, t) # cv2.imshow('cnt', cnt) # cv2.waitKey(0) contours, hierarchy = cv2.findContours(cnt, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) max_cnt = max(contours, key=cv2.contourArea) # print(max_cnt) # for tmp in max_cnt: # print(tmp) l = min(max_cnt[:, :, 0]) r = max(max_cnt[:, :, 0]) # print(l, r) cv2.drawContours(final_mask, [max_cnt], 0, (255, 255, 255), -1) # cv2.imshow('final_mask', final_mask) # cv2.waitKey(0) bit_and = cv2.bitwise_and(img, final_mask) bit_not = cv2.bitwise_not(final_mask) final = cv2.bitwise_or(bit_and, bit_not) # cv2.imshow('final', final) # cv2.waitKey(0) contrast = cv2.addWeighted(final, 1.5, final, 0, 0) # cv2.imshow('contrast', contrast) # cv2.waitKey(0) return contrast
def sobel_gradient(image): grad_x = cv.Sobel(image, cv.CV_32F, 1, 0) # 对x求一阶导 grad_y = cv.Sobel(image, cv.CV_32F, 0, 1) # 对y求一阶导 gradx = cv.convertScaleAbs(grad_x) # 用convertScaleAbs()函数将其转回原来的uint8形式 grady = cv.convertScaleAbs(grad_y) # cv.imshow("sobel_x", gradx) # x方向上的梯度 # cv.imshow("sobel_y", grady) # y方向上的梯度 gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0) # 图片融合 cv.imshow("sobel", gradxy)
def __render_bars(self, frame: np.array) -> None: alpha = config.Renderer.Label.BAR_ALPHA if alpha <= 0.0: return bar_height = 2 * config.Renderer.Label.PADDING + config.Renderer.Label.FONT_HEIGHT size = Size.of_image(frame) overlay = frame.copy() if alpha < 1.0 else frame cv2.rectangle(overlay, (0, 0), (size.width, bar_height), color.BLACK, cv2.FILLED) cv2.rectangle(overlay, (0, size.height - bar_height), size, color.BLACK, cv2.FILLED) if alpha < 1.0: cv2.addWeighted(overlay, alpha, frame, 1.0 - alpha, 0, frame)
def bright_contrast1(img, contrast=1, brightness=100): #亮度与对比度contrast:对比度 brightness:亮度 pic_turn = cv.addWeighted(img, contrast, img, 0, brightness) #cv.addWeighted(对象,对比度,对象,对比度) '''cv.addWeighted()实现的是图像透明度的改变与图像的叠加''' # cv.imshow('turn', pic_turn) #显示图片 # cv.waitKey(0) # cv.destroyAllWindows() return pic_turn
def draw_line(img, lines): img = np.copy(img) blank_image = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) for line in lines: for x1, y1, x2, y2 in line: cv2.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 3) img = cv2.addWeighted(img, 0.8, blank_image, 1, 0.0) return img
def display_lines(frame, lines, line_color=(0, 255, 0), line_width=6): # line color (B,G,R) line_image = np.zeros_like(frame) if lines is not None: for line in lines: for x1, y1, x2, y2 in line: cv.line(line_image, (x1, y1), (x2, y2), line_color, line_width) line_image = cv.addWeighted(frame, 0.8, line_image, 1, 1) return line_image
def overlying(oripath, imgpath, savepath): for filename in tqdm(os.listdir(oripath)): ori = cv2.imread(oripath + '/' + filename) img = cv2.imread(imgpath + '/' + filename) res = cv2.addWeighted(ori, 0.4, img, 0.6, 0) if not os.path.exists(savepath): os.makedirs(savepath) cv2.imwrite(savepath + '/' + filename, res) else: cv2.imwrite(savepath + '/' + filename, res)
def effect(dir="vertical", pixelLength=480): cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) _, new_img = cap.read() x, y = 0, 0 w = len(new_img[0]) - 1 #Cam width h = len(new_img) - 1 #Cam height line_thickness = 2 if dir == "vertical": pixelLength = h x_max = w else: pixelLength = w y_max = h for i in range(1, pixelLength, 1): _, img = cap.read() if dir == "vertical": new_img[i, :] = img[i, :] img[:i, :] = new_img[:i, :] y = i y_max = i else: new_img[:, i] = img[:, i] img[:, :i] = new_img[:, :i] x = i x_max = i #"Whity" image effect sub_img = img[y:y + h, x:x + w] white_rect = np.ones(sub_img.shape, dtype=np.uint8) * 255 res = cv2.addWeighted(sub_img, 0.5, white_rect, 0.5, 1.0) img[y:y + h, x:x + w] = res #Separation line cv2.line(img, (x, y), (x_max, y_max), (0, 0, 255), line_thickness) # Display cv2.imshow('img', img) # Stop if escape key is pressed k = cv2.waitKey(30) & 0xff #ESC if k == 27: break # Release the VideoCapture object cap.release() #Creates a jpg file img_name = input("Put a name to your creation:") if (img_name != ""): img = img[:-line_thickness - 1, :-line_thickness - 1] cv2.imwrite("images/" + img_name + ".jpg", img) print("Image created succesfully!") else: print("No image created!")
def test_image(photo): image = cv2.cvtColor((photo), cv2.COLOR_BGR2RGB) canny_image = do_canny(cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)) crop_img = region_interest(canny_image, np.array([vertices], np.int32)) lines = cv2.HoughLinesP(crop_img, 3, np.pi / 180, 100, np.array([]), minLineLength=70, maxLineGap=50) line_image = drawline(image, average_slope_intercept(photo, lines)) angle = compute_steering_angle(average_slope_intercept(photo, lines)) image_with_lines = cv2.addWeighted(photo, 0.8, line_image, 1, 1) image_with_direction_line = cv2.addWeighted( image_with_lines, 0.8, display_heading_line(image, angle), 1, 1) cv2.putText(image_with_direction_line, "steering angle: " + str(angle), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2, cv2.LINE_AA) return cv2.imshow("combine", image_with_direction_line)
def __lifting_scheme(self, img, row_col): """ :row_col: 1: split rowwise; 0: split colwise """ img_even, img_odd = self.__split(img, row_col) # split img_update = self.__update(img_odd, img_even) # update img_predict = self.__predict(img_update) # predict # calculate the diffence between predict and odd img_difference = cv2.addWeighted(img_predict, 1, img_odd, -1, 0) return img_update, img_difference
def sobel_demo(image): #当边缘不明显时可用Scharr算子,但该算子受噪声影响较大 grad_x = cv.Sobel(image, cv.CV_32F, 1, 0) grad_y = cv.Sobel(image, cv.CV_32F, 0, 1) gradx = cv.convertScaleAbs(grad_x) grady = cv.convertScaleAbs(grad_y) cv.imshow("x_dir", gradx) cv.imshow("y_dir", grady) gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0) cv.imshow("xy_dir", gradxy)
def __contrastAndBrightness(self, alpha, beta, image): """ 调整亮度和对比度 :param alpha:影响对比度 =1原图 >1对比度增强更加清晰 <1对比度减弱更加模糊 :param beta:影响亮度 (>0) 增加或减少 灰度值增加或降低 变亮或变暗 :param image:待处理图像 :return: """ blank = np.zeros(image.shape, image.dtype) changed = addWeighted(image, alpha, blank, 1 - alpha, beta) # 融合两张图片 return changed
def overlay_heatmap(self, heatmap, image, alpha=0.5, colormap=cv2.COLORMAP_VIRIDIS): # apply the supplied color map to the heatmap and then # overlay the heatmap on the input image heatmap = cv2.applyColorMap(heatmap, colormap) # if the input image is grey-scale, convert it to 3-dim if len(image.shape) == 2 or image.shape[-1] != 3: image = cv2.cvtColor(src=image, code=cv2.COLOR_GRAY2RGB) # if image px values are in [0, 1], upscale to [0, 255] if np.max(image) <= 1.0: image = image * 255.0 output = cv2.addWeighted(image.astype('uint8'), alpha, heatmap, 1 - alpha, 0) return output
def __cv_addweighted(src1, alpha, src2, beta, gamma): """Computes the weighted addition of two Mats. Args: src1: A numpy.ndarray. alpha: The weight for the first Mat. src2: A numpy.ndarray. beta: The weight for the second Mat. gamma: Constant to add to each sum. Returns: A numpy.ndarray. """ return cv2.addWeighted(src1, alpha, src2, beta, gamma)
def pre_dispose(self):#返回一个面上的颜色平均值HSVRGB img = cv2.GaussianBlur(self.frame,(7,7),0) b,g,r = cv2.split(img) avgb = cv2.mean(b)[0] avgg = cv2.mean(g)[0] avgr = cv2.mean(r)[0] k = (avgb+avgg+avgr)/3 kb = k/avgb kg = k/avgg kr = k/avgr b = cv2.addWeighted(src1=b, alpha=kb, src2=0, beta=0, gamma=0) g = cv2.addWeighted(src1=g, alpha=kg, src2=0, beta=0, gamma=0) r = cv2.addWeighted(src1=r, alpha=kr, src2=0, beta=0, gamma=0) img = cv2.merge([b,g,r]) img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) h,s,v = cv2.split(img_hsv) v = cv2.equalizeHist(v) img_hsv = cv2.merge([h,s,v]) self.img_hsv = img_hsv img = cv2.cvtColor(img_hsv,cv2.COLOR_HSV2BGR) self.img_bgr = img self.frame = img