#在图像处理中就是给定一个种子点作为起始点,向附近相邻的像素点扩散,
# 把颜色相同或者相近的所有点都找出来,并填充上新的颜色
# 这些点形成一个连通的区域。 漫水填充算法可以用来标记或者分离图像的一部分
#漫水填充算法实现最常见有四邻域像素填充法,
# 八邻域像素填充法,基于扫描线的填充方法
#在图像处理中就是给定一个种子点作为起始点,向附近相邻的像素点扩散,
# 把颜色相同或者相近的所有点都找出来,并填充上新的颜色
# 这些点形成一个连通的区域。 漫水填充算法可以用来标记或者分离图像的一部分
#漫水填充算法实现最常见有四邻域像素填充法,
# 八邻域像素填充法,基于扫描线的填充方法
import cv2
from matplotlib import image
import numpy as np
import os.path as osp
import os
import numpy as np
from skimage import io, color
from tqdm import tqdm
from skimage import morphology
import matplotlib.pyplot as plt
from skimage import segmentation
from PIL import Image
def get_ori_list(ori_folder):
img_list = os.listdir(ori_folder)
ori_list = []
for img_name in img_list:
flag = 0
for sample in check_list:
if sample in img_name:
flag=1
break
if flag==0:
ori_list.append(osp.join(ori_folder,img_name))
if len(ori_list)>20:
break
return ori_list
check_list = ['copper','bg','check','dust']
def fill_color_demo(img_path,end_path):
image = cv2.imread(img_path)
copyImg = image.copy()
h, w = image.shape[:2]
mask = np.zeros([h+2, w+2],np.uint8) #mask必须行和列都加2,且必须为uint8单通道阵列
#为什么要加2可以这么理解:当从0行0列开始泛洪填充扫描时,mask多出来的2可以保证扫描的边界上的像素都会被处理
cv2.floodFill(copyImg, mask, (220, 250), (0, 255, 0), (100, 100, 100), (50, 50 ,50), cv2.FLOODFILL_FIXED_RANGE)
cv2.imwrite(end_path,copyImg)
img1 = Image.open(img_path)
img2 = Image.open(end_path)
image = Image.blend(img1,img2,0.3)
blend_end_path = '/cloud_disk/users/huh/pcb/script/flood_fill_result/'+'blend_'+osp.basename(end_path)
image.save(blend_end_path)
def get_mask(bg_mask_path):
mask = np.zeros([512+2, 512+2],np.uint8) #mask必须行和列都加2,且必须为uint8单通道阵列
bg_mask = cv2.imread(bg_mask_path)
bg_mask = bg_mask[:,:,0]
for i in range(512):
for j in range(512):
if bg_mask[i][j]==0:
mask[i+1][j+1] = 1
return mask
if __name__ == '__main__':
ori_folder = '/cloud_disk/users/huh/dataset/PCB/ddrnet_23_slim/pre_process_img'
end_folder = '/cloud_disk/users/huh/pcb/script/flood_fill_result'
ori_list = get_ori_list(ori_folder)
for img_path in tqdm(ori_list):
end_path = osp.join(end_folder,osp.basename(img_path))
bg_mask_path = osp.join(ori_folder,osp.basename(img_path[:-4])+'_bg_mask.jpg')
# fore_mask_path = osp.join(ori_folder,osp.basename(img_path[:-4])+'_copper_mask.jpg')
# mask = get_mask(bg_mask_path)
fill_color_demo(img_path,end_path)
|