def blur(): image = cv2.imread(IMG_PATH + "/yumi-cells.jpg") kernel_sizes = [(3, 3), (5, 5), (7, 7), (7, 1), (1, 7)] filter_imgs = {"original": image} blur_imgs = {"original": image} for ksize in kernel_sizes: title = f"ksize: {ksize}" kernel = np.ones(ksize) kernel /= kernel.size filter_imgs[title] = cv2.filter2D(image, -1, kernel) blur_imgs[title] = cv2.blur(image, ksize) resimg = si.show_imgs(filter_imgs, "cv2.filter2D", 3) resimg = si.show_imgs(blur_imgs, "cv2.blur", 3)
def watershed(): srcimg = cv2.imread(IMG_PATH + "/wildgoose.jpg", cv2.IMREAD_COLOR) images = {"original": srcimg} # find apprent background(sky) and wild goose region gray = cv2.cvtColor(srcimg, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (3, 3), 0) ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) images["threshold"] = binary app_sky = cv2.erode(binary, kernel, iterations=3) images["apparent sky"] = app_sky app_goose = cv2.dilate(binary, kernel, iterations=1) images["apparent wildgoose"] = app_goose images["undetermined"] = cv2.subtract(app_goose, app_sky) # create markers and watershed markers = np.zeros(gray.shape, dtype=np.int32) markers[app_sky > 0] = SKY_LABEL markers[app_goose == 0] = GOOSE_LABEL markers = cv2.watershed(srcimg, markers) # show contours of wild gooses labelimg = np.zeros_like(srcimg) labelimg[markers == -1, :] = (0, 0, 255) # draw contours labelimg[markers == SKY_LABEL, :] = (200, 150, 100) labelimg[markers == GOOSE_LABEL, :] = srcimg[markers == GOOSE_LABEL, :] images["label"] = labelimg result_img = si.show_imgs(images, "watershed", 3) cv2.imwrite(IMG_PATH + "/result/watershed_wildgoose.jpg", result_img)
def count_puzzle(img): ih, iw, ch = img.shape mask = np.zeros((ih+2, iw+2), dtype=np.uint8) images = {"original": img.copy()} cv2.imshow("image", img) cv2.waitKey() count = 0 for y in range(img.shape[0]): for x in range(img.shape[1]): if mask[y+1, x+1] == 0: # flood fill로 채울 랜덤 색상 생성 color = np.random.randint(50, 256, 3).tolist() color[0] = 255 ret, img, mask, rect = cv2.floodFill(img, mask, (x,y), color, (10,10,10), (10,10,10), flags=8) mask_show = mask*255 print(f"area={ret}, rect={rect}, mask value={mask[y+1, x+1]}") if ret > 500: # 영역이 넓은 것만 퍼즐 조각으로 인정 cv2.imshow("image", img) cv2.imshow("mask", mask_show) cv2.waitKey(200) count += 1 print("total puzzle count:", count) cv2.destroyAllWindows() images["filled"] = img result_img = si.show_imgs(images, "floodfill", 2) cv2.imwrite(IMG_PATH + "/result/floodfill_puzzle.jpg", result_img)
def canny(): image = cv2.imread(IMG_PATH + "/arya.jpg", cv2.IMREAD_GRAYSCALE) canny_imgs = {"original": image} canny_imgs["Laplacian"] = cv2.Laplacian(image, -1, scale=2) canny_imgs["Canny (100, 200)"] = cv2.Canny(image, 100, 200) canny_imgs["Canny (200, 255)"] = cv2.Canny(image, 150, 255) result_img = si.show_imgs(canny_imgs, "Canny", 2)
def fill_lake(): srcimg = cv2.imread(IMG_PATH + "/sinjeong-lake.jpg", cv2.IMREAD_COLOR) images = {"original": srcimg} seed = (int(srcimg.shape[1] / 2), int(srcimg.shape[0] / 2)) # 아무 처리하지 않고 바로 호수에 flood fill 적용 fill_direct = srcimg.copy() mask = np.zeros((srcimg.shape[0] + 2, srcimg.shape[1] + 2), dtype=np.uint8) retval, fill_direct, mask, rect = cv2.floodFill(fill_direct, mask, seed, (0, 255, 255), (2, 2, 2), (2, 2, 2), flags=8) print(f"pixel area of lake WITHOUT preprocess={retval}, rect={rect}") fill_direct = cv2.circle(fill_direct, seed, 1, (0, 0, 255), 2) images["direct_floodfill"] = fill_direct # flood fill이 잘 퍼지도록 블러링 후 적용 fill_blur = srcimg.copy() fill_blur = cv2.GaussianBlur(fill_blur, (3, 3), 0) fill_blur = cv2.medianBlur(fill_blur, 3) mask = np.zeros((srcimg.shape[0] + 2, srcimg.shape[1] + 2), dtype=np.uint8) retval, fill_blur, mask, rect = cv2.floodFill(fill_blur, mask, seed, (0, 255, 255), (2, 2, 2), (2, 2, 2), flags=8 | (255 << 8)) print(f"pixel area of lake WITH preprocess= {retval}, rect={rect}") fill_blur = cv2.circle(fill_blur, seed, 1, (0, 0, 255), 2) images["blur_n_floodfill"] = fill_blur # 결과 출력 images["final mask"] = cv2.cvtColor(mask[1:-1, 1:-1], cv2.COLOR_GRAY2BGR) result_img = si.show_imgs(images, "fill the lake", 2) cv2.imwrite(IMG_PATH + "/result/fill_lake.jpg", result_img)
def gaussian(): image = cv2.imread(IMG_PATH + "/yumi-cells.jpg") kernel_size = (5, 5) blur_imgs = {} blur_imgs["original"] = image blur_imgs["blur"] = cv2.blur(image, kernel_size) blur_imgs["GaussianBlur"] = cv2.GaussianBlur(image, kernel_size, 0) result_img = si.show_imgs(blur_imgs, "GaussianBlur", 3, 1000)
def laplacian(): image = cv2.imread(IMG_PATH + "/yumi-cells.jpg", cv2.IMREAD_GRAYSCALE) lapla_imgs = {"original": image} sobel_dx = cv2.Sobel(image, ddepth=-1, dx=1, dy=0, ksize=3) sobel_dy = cv2.Sobel(image, ddepth=-1, dx=0, dy=1, ksize=3) lapla_imgs["Sobel dx+dy"] = cv2.add(sobel_dx, sobel_dy) lapla_imgs["Laplacian"] = cv2.Laplacian(image, -1) result_img = si.show_imgs(lapla_imgs, "Laplacian", 3)
def erode_and_dilate(): srcimg = cv2.imread(IMG_PATH + "/marvel.jpg", cv2.IMREAD_GRAYSCALE) srcimg = salt_and_pepper_noise(srcimg) # erode and dilate, and then show them images = {"original": srcimg} kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) images["erode"] = cv2.erode(srcimg, kernel) images["dilate"] = cv2.dilate(srcimg, kernel) result_img = si.show_imgs(images, "Erode and Dilate", 1)
def median(): image = cv2.imread(IMG_PATH + "/saltnpepper.jpg") kernel_size = (5, 5) median_imgs = dict() median_imgs["original"] = image median_imgs[f"gaussian {kernel_size}"] = cv2.GaussianBlur( image, kernel_size, 0) median_imgs[f"median {kernel_size[0]}"] = cv2.medianBlur( image, kernel_size[0]) result_img = si.show_imgs(median_imgs, "Median Filter", 3) # cv2.imwrite(IMG_PATH + "/result/snp-median.jpg", result_img) image = cv2.imread(IMG_PATH + "/ann.jpg") median_imgs = dict() median_imgs["original"] = image median_imgs["median (3)"] = cv2.medianBlur(image, 3) median_imgs["median (5)"] = cv2.medianBlur(image, 5) result_img = si.show_imgs(median_imgs, "Median Filter", 3)
def sobel(): image = cv2.imread(IMG_PATH + "/yumi-cells.jpg", cv2.IMREAD_GRAYSCALE) sobel_imgs = {"original": image} sobel_imgs["Sobel dx"] = cv2.Sobel(image, ddepth=-1, dx=1, dy=0, ksize=3) sobel_imgs["Sobel dy"] = cv2.Sobel(image, ddepth=-1, dx=0, dy=1, ksize=3) sobel_imgs["Sobel dx+dy"] = cv2.add(sobel_imgs["Sobel dx"], sobel_imgs["Sobel dy"]) sobel_imgs["Scharr dx"] = cv2.Scharr(image, ddepth=-1, dx=1, dy=0) sobel_imgs["Scharr dy"] = cv2.Scharr(image, ddepth=-1, dx=0, dy=1) result_img = si.show_imgs(sobel_imgs, "Sobel & Scharr", 3)
def count_balls(): srcimg = cv2.imread(IMG_PATH + "/ballpool.jpg", cv2.IMREAD_COLOR) images, mask = prepare_mask(srcimg) result_img = si.show_imgs(images, "floodfill", 3) label = LABEL_BEGIN ih, iw, ch = srcimg.shape hueimg = images["hue"].copy() for v in range(0, ih, 5): for u in range(0, iw, 5): if mask[v+1, u+1] == 0: hueimg, mask, area = fill_image(hueimg, mask, (u, v), label) cv2.imshow("floodfill mask", mask) if area > AREA_THR: label += 1 cv2.waitKey(100) cv2.waitKey() labeled = colorize_regions(mask, label) images["labeled balls"] = labeled result_img = si.show_imgs(images, "floodfill", 3) cv2.imwrite(IMG_PATH + "/result/floodfill_ball.jpg", result_img)
def bilateral(): image = cv2.imread(IMG_PATH + "/road.jpg") kernel_size = (5, 5) blur_imgs = {} blur_imgs["original"] = image blur_imgs["gaussian"] = cv2.GaussianBlur(image, kernel_size, 0) blur_imgs["bilateral (5,50,50)"] = cv2.bilateralFilter(image, 5, 50, 50) blur_imgs["bilateral (5,150,150)"] = cv2.bilateralFilter( image, 5, 150, 150) result_img = si.show_imgs(blur_imgs, "Bilateral Filter", 2) cv2.imwrite(IMG_PATH + "/result/road-bilateral.jpg", result_img)
def morphologies(): srcimg = cv2.imread(IMG_PATH + "/marvel.jpg", cv2.IMREAD_GRAYSCALE) srcimg = salt_and_pepper_noise(srcimg) # erode and dilate, and then show them images = {"original": srcimg} kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) images["opening"] = cv2.morphologyEx(srcimg, cv2.MORPH_OPEN, kernel) images["closing"] = cv2.morphologyEx(srcimg, cv2.MORPH_CLOSE, kernel) images["gradient"] = cv2.morphologyEx(srcimg, cv2.MORPH_GRADIENT, kernel) images["tophat"] = cv2.morphologyEx(srcimg, cv2.MORPH_TOPHAT, kernel) images["blackhat"] = cv2.morphologyEx(srcimg, cv2.MORPH_BLACKHAT, kernel) result_img = si.show_imgs(images, "Morphology Ops", 2)