美文网首页
四色地图填充程序(Python opencv)

四色地图填充程序(Python opencv)

作者: Wslei | 来源:发表于2018-12-26 08:38 被阅读0次

代码使用了floodfill填充函数:
cv.floodFill(copyImage, mask, (x,y), (0, 255, 255), (0, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
具体使用方法网上有很多大体就是选一个种子点然后跟周围像素点进行比较第五和第六设置像素点周围的大于小于的值
第三个值是设置要上色的颜色。

# coding: utf-8
import cv2 as cv
import numpy as np

# floodfill填充,x,y代表涂色种子的位置,id代表涂得颜色,采用四色填充方法
def fill_image(image,x,y,id,copyImage):

    h, w = image.shape[:2]  # 读取图像的宽和高
    mask = np.zeros([h + 2, w + 2], np.uint8)  # 新建图像矩阵  +2是floodfill函数要求
    if(id==1):
        cv.floodFill(copyImage, mask, (x,y), (0, 255, 255), (0, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
    if(id==2):
        cv.floodFill(copyImage, mask, (x, y), (255, 255, 0), (0, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
    if (id == 3):
        cv.floodFill(copyImage, mask, (x, y), (0, 255, 0), (0, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)
    if (id == 4):
        cv.floodFill(copyImage, mask, (x, y), (100, 0, 255), (0, 100, 100), (50, 50, 50), cv.FLOODFILL_FIXED_RANGE)

#加载图片跟坐标和涂色信息
src = cv.imread("map.jpg")
copyImage = src.copy()  # 复制原图像
cv.imshow("before", src)
a = np.loadtxt('zuobiao.txt',skiprows=0, dtype=int)
m,n=a.shape
b = np.loadtxt('tuse.txt',skiprows=0, dtype=int)
#对地图进行涂色
for i in range(m):
    fill_image(src, a[i][0], a[i][1], b[i][1], copyImage)
#展示涂色地图
cv.imshow("after", copyImage)
cv.imwrite("new.bmp",copyImage)
cv.waitKey(0)
cv.destroyAllWindows()

zuobiao文件

151,171
282,220
424,223
660,91
645,153
620,198
524,268
536,231
486,274
439,329
407,276
264,290
49,249
128,315
222,408
349,370
423,403
482,384
502,331
558,292
554,364
588,339
599,404
540,434
489,426
413,448
319,483
439,503
506,508
577,458
461,573
608,501

tuse文件

1  1
2  2
3  1
4  2
5  3
6  2
7  3
8  1
9  2
10  3
11  4
12  3
13  2
14  4
15  1
16  1
17  2
18  1
19  4
20  1
21  2
22  3
23  1
24  3
25  4
26  3
27  2
28  1
29  2
30  4
31  1
32  1

图片


map.jpg

相关文章

网友评论

      本文标题:四色地图填充程序(Python opencv)

      本文链接:https://www.haomeiwen.com/subject/hluclqtx.html