闭合曲线内涂色code(辅助制作Ground truth) Posted on 2018-05-30 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import cv2import numpy as npfrom matplotlib import pyplot as pltpath = 'C:\\Users\\Ashraf\\Desktop\\ground\\'def red_bw2d(image): img = image r, c, _ = img.shape for i in range(r): for j in range(c): if img[i, j][2] > img[i, j][0] and img[i, j][2] > img[i, j][1]: img[i, j][0] = 255 img[i,j][1] = 255 img[i,j][2] = 255 else: img[i, j][0] = 0 img[i, j][1] = 0 img[i, j][2] = 0 img = img[:, :, 1] _, bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) return bindef green_bw2d(image): img = image r, c, _ = img.shape for i in range(r): for j in range(c): if img[i, j][1] > img[i, j][0] and img[i, j][1] > img[i, j][2]: img[i, j][0] = 255 img[i,j][1] = 255 img[i,j][2] = 255 else: img[i, j][0] = 0 img[i, j][1] = 0 img[i, j][2] = 0 img = img[:, :, 1] _, bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) return bindef blue_bw2d(image): img = image r, c, _ = img.shape for i in range(r): for j in range(c): if img[i, j][0] > img[i, j][1] and img[i, j][0] > img[i, j][2]: img[i, j][0] = 255 img[i,j][1] = 255 img[i,j][2] = 255 else: img[i, j][0] = 0 img[i, j][1] = 0 img[i, j][2] = 0 img = img[:, :, 1] _, bin = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) return bindef denoise(image): img = image img = cv2.medianBlur(img, 3) return imgdef fill(image, name): point = (0,0) src = image.copy() src = np.invert(src) connectivity = 4 flags = connectivity flags |= cv2.FLOODFILL_FIXED_RANGE cv2.floodFill(src, None, point, (0, 0, 0), (100,) * 3, (100,) * 3, flags) cv2.imwrite(name, src)def function(img, name): img = denoise(img) img = red_bw2d(img) img = denoise(img) fill(img, name)# Mainimg = cv2.imread('./3.png', cv2.IMREAD_COLOR)name = './test.jpg'function(img, name) 坚持原创分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏