目的:将需要的检测的目标融合到背景图片中,用于丰富目标检测中的负样本和均衡各个类别,效果较好;
背景图片:
目标图片(X光机下的一把美工刀,同时有它的mask坐标标签):
1.扣取出目标图将要放置在背景图中的背景像素区
cut_bg1 = bg_img[p_y:p_y+ro_h, p_x:p_x+ro_w,:] 域
cv2.imwrite('cut_bg1.jpg', cut_bg1)
2.得到准备放置目标的区域
#point_obj类型为np.array, 存储方式为[[],[],[],[],...]
I = np.zeros((ro_h, ro_w, 3), dtype=np.uint8)
I = cv2.fillPoly(I, [point_obj], color=(1, 1, 1))
temp1 = I * cut_bg1
cv2.imwrite('temp1.jpg', temp1)
3.去除放置目标区域的像素
cut_bg = cv2.fillPoly(cut_bg1, [point_obj], color=(0, 0, 0))
cv2.imwrite('cut_bg.jpg', cut_bg)
4.去除目标以外的边缘信息
temp = I * obj_img
cv2.imwrite('temp.jpg', temp)
5.将区域内的目标像素和背景像素加和,进行一个像素的融合
ratio = random.randint(90, 99) * 0.01
obj_img_read = (temp * ratio + temp1 * (1-ratio)).astype('uint8')
cv2.imwrite('obj_img_read.jpg', obj_img_read)
6.将背景区域与目标区域融合
obj_img_read111 =(obj_img_read+cut_bg).astype('uint8')
cv2.imwrite('obj_img_read111.jpg', obj_img_read111)
7.cv格式转换为PIL格式,进行图片区域的融合
pil_img_cut = Image.fromarray(cv2.cvtColor(obj_img_read111, cv2.COLOR_BGR2RGB))
pil_img_bg = Image.fromarray(cv2.cvtColor(bg_img, cv2.COLOR_BGR2RGB))
pil_img_bg.paste(pil_img_cut, (p_x, p_y))
img_path = os.path.join(save_img_dir,'result.jpg')
pil_img_bg.save(img_path)
代码:
import cv2
from Image import PIL
bg_img = cv2.imread(bg) #背景图片
em_h, em_w = bg_img.shape[:2]
obj_img = cv2.imread(obj_img_path)#目标图片
ro_h, ro_w = obj_img.shape[:2]
p_x = random.randint(int(em_w * 0.1), int(0.9 * em_w - ro_w )) #目标图放置的位置
p_y = random.randint(int(em_h * 0.1), int(0.9 * em_h - ro_h))
point_obj = []
for (x_, y_) in zip(x_list, y_list):#point:[(x1,y1),(x2,y2),(x3,y3),...]
point_obj.append((x_, y_))
point_obj = np.array(point_obj, dtype=np.int32) #point_obj是目标图的mask坐标(多坐标点组成)
I = np.zeros((ro_h, ro_w, 3), dtype=np.uint8)
I = cv2.fillPoly(I, [point_obj], color=(1, 1, 1)) #做一个模板
cut_bg1 = bg_img[p_y:p_y+ro_h, p_x:p_x+ro_w,:] #扣取出目标图将要放置在背景图中的背景像素区域
cv2.imwrite('cut_bg1.jpg', cut_bg1)
temp1 = I * cut_bg1 # 得到准备放置目标的区域
cv2.imwrite('temp1.jpg', temp1)
cut_bg = cv2.fillPoly(cut_bg1, [point_obj], color=(0, 0, 0)) #去除放置目标区域的像素
cv2.imwrite('cut_bg.jpg', cut_bg)
temp = I * obj_img #去除目标以外的边缘信息
cv2.imwrite('temp.jpg', temp)
ratio = random.randint(90, 99) * 0.01
obj_img_read = (temp * ratio + temp1 * (1-ratio)).astype('uint8') #将区域内的目标像素和背景像素加和,进行一个像素的融合
cv2.imwrite('obj_img_read.jpg', obj_img_read)
obj_img_read111 =(obj_img_read+cut_bg).astype('uint8')
cv2.imwrite('obj_img_read111.jpg', obj_img_read111)
#cv格式转换为PIL格式
pil_img_cut = Image.fromarray(cv2.cvtColor(obj_img_read111, cv2.COLOR_BGR2RGB))
pil_img_bg = Image.fromarray(cv2.cvtColor(bg_img, cv2.COLOR_BGR2RGB))
pil_img_bg.paste(pil_img_cut, (p_x, p_y))
img_path = os.path.join(save_img_dir,'result.jpg')
pil_img_bg.save(img_path)