使用fillPoly函数将目标贴入背景图

目的:将需要的检测的目标融合到背景图片中,用于丰富目标检测中的负样本和均衡各个类别,效果较好;

背景图片:

目标图片(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)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫与橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值