admin 管理员组文章数量: 1184232
2023年12月18日发(作者:access下载后的样子)
# Load the foreground input image foreground = (source) print('foreground1') print(foreground) # Change the color of foreground image to RGB # and resize image to match shape of R-band in RGB output map foreground = or(foreground, _BGR2RGB) foreground = (foreground, ([1], [0])) print('foreground2') # Create a background array to hold white pixels # with the same size as RGB output map background = 255 * _like(rgb).astype(8) # Convert uint8 to float foreground = (float) background = (float) # Create a binary mask of the RGB output map using the threshold value 0 th, alpha = old((rgb), 0, 255, _BINARY) # Apply a slight blur to the mask to soften edges print('alpha1') print(alpha) alpha = anBlur(alpha, (7, 7), 0) print('alpha2') print(alpha) # Normalize the alpha mask to keep intensity between 0 and 1 alpha = (float) / 255 print('alpha3') print(alpha) # Multiply the foreground with the alpha matte foreground = ly(alpha, foreground) # Multiply the background with ( 1 - alpha ) background = ly(1.0 - alpha, background) # Add the masked foreground and background outImage = (foreground, background) # Return a normalized output image for display # return (int) return outImage / 255def segment(net, path, show_orig=True, dev='cuda'): img = (path) if show_orig: (img) ('off') () # Comment the Resize and CenterCrop for better inference results trf = e([(450), # Crop(224), or(), ize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]) inp = trf(img).unsqueeze(0).to(dev) out = (dev)(inp)['out'] om = (e(), dim=0).detach().cpu().numpy() rgb = decode_segmap(om, path) (rgb) ('off') ()dlab = bv3_resnet101(pretrained=1).eval()segment(dlab, '', show_orig=False)
g = _like(image).astype(8) b = _like(image).astype(8) for l in range(0, nc): idx = image == l r[idx] = label_colors[l, 0] g[idx] = label_colors[l, 1] b[idx] = label_colors[l, 2] rgb = ([r, g, b], axis=2) # Load the foreground input image foreground = (source) # Load the background input image background = (bgimg) # Change the color of foreground image to RGB # and resize images to match shape of R-band in RGB output map foreground = or(foreground, _BGR2RGB) background = or(background, _BGR2RGB) foreground = (foreground, ([1], [0])) background = (background, ([1], [0])) # Convert uint8 to float foreground = (float) background = (float) # Create a binary mask of the RGB output map using the threshold value 0 th, alpha = old((rgb), 0, 255, _BINARY) # Apply a slight blur to the mask to soften edges alpha = anBlur(alpha, (7, 7), 0) # Normalize the alpha mask to keep intensity between 0 and 1 alpha = (float) / 255 # Multiply the foreground with the alpha matte foreground = ly(alpha, foreground) # Multiply the background with ( 1 - alpha ) background = ly(1.0 - alpha, background) # Add the masked foreground and background outImage = (foreground, background) # Return a normalized output image for display return outImage / 255def segment(net, path, bgimagepath, show_orig=True, dev='cuda'): img = (path) if show_orig: (img); ('off'); () # Comment the Resize and CenterCrop for better inference results trf = e([(400), # Crop(224), or(), ize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]) inp = trf(img).unsqueeze(0).to(dev) out = (dev)(inp)['out'] om = (e(), dim=0).detach().cpu().numpy() rgb = decode_segmap(om, path, bgimagepath)
(rgb); ('off'); ()dlab = bv3_resnet101(pretrained=1).eval()segment(dlab, '','', show_orig=False)结果:原图
背景图3、模糊背景先上代码,注意:重复代码未包括。def decode_segmap(image, source, nc=21):
label_colors = ([(0, 0, 0), # 0=background # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), # 6=bus, 7=car, 8=cat, 9=chair, 10=cow (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), # 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128), # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)]) r = _like(image).astype(8) g = _like(image).astype(8) b = _like(image).astype(8)
for l in range(0, nc): idx = image == l r[idx] = label_colors[l, 0] g[idx] = label_colors[l, 1] b[idx] = label_colors[l, 2]
rgb = ([r, g, b], axis=2)
# Load the foreground input image
foreground = (source) # Change the color of foreground image to RGB
# and resize image to match shape of R-band in RGB output map
foreground = or(foreground, _BGR2RGB) foreground = (foreground,([1],[0])) # Create a Gaussian blur of kernel size 7 for the background image blurredImage = anBlur(foreground, (7,7), 0) # Convert uint8 to float foreground = (float) blurredImage = (float) # Create a binary mask of the RGB output map using the threshold value 0 th, alpha = old((rgb),0,255, _BINARY) # Apply a slight blur to the mask to soften edges alpha = anBlur(alpha, (7,7),0) # Normalize the alpha mask to keep intensity between 0 and 1 alpha = (float)/255 # Multiply the foreground with the alpha matte foreground = ly(alpha, foreground)
# Multiply the background with ( 1 - alpha ) background = ly(1.0 - alpha, blurredImage)
# Add the masked foreground and background outImage = (foreground, background) # Return a normalized output image for display return outImage/255def segment(net, path, show_orig=True, dev='cuda'): img = (path)
if show_orig: (img); ('off'); () # Comment the Resize and CenterCrop for better inference results trf = e([(450),
#Crop(224),
or(),
ize(mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])]) inp = trf(img).unsqueeze(0).to(dev) out = (dev)(inp)['out'] om = (e(), dim=0).detach().cpu().numpy()
rgb = decode_segmap(om, path)
(rgb); ('off'); ()segment(dlab, '', show_orig=False)结果:
4、背景灰度化上代码:def decode_segmap(image, source, nc=21):
label_colors = ([(0, 0, 0), # 0=background # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128), # 6=bus, 7=car, 8=cat, 9=chair, 10=cow (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0), # 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128), # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)]) r = _like(image).astype(8) g = _like(image).astype(8) b = _like(image).astype(8)
for l in range(0, nc): idx = image == l r[idx] = label_colors[l, 0] g[idx] = label_colors[l, 1] b[idx] = label_colors[l, 2]
rgb = ([r, g, b], axis=2) # Load the foreground input image
foreground = (source) # Change the color of foreground image to RGB
# and resize image to match shape of R-band in RGB output map
foreground = or(foreground, _BGR2RGB) foreground = (foreground,([1],[0]))
# Create a background image by copying foreground and converting into grayscale background = or(foreground, _BGR2GRAY)
# convert single channel grayscale image to 3-channel grayscale image background = or(background, _GRAY2RGB)
# Convert uint8 to float foreground = (float) background = (float) # Create a binary mask of the RGB output map using the threshold value 0 th, alpha = old((rgb),0,255, _BINARY) # Apply a slight blur to the mask to soften edges alpha = anBlur(alpha, (7,7),0) # Normalize the alpha mask to keep intensity between 0 and 1 alpha = (float)/255
版权声明:本文标题:使用语义分割进行图像前景后景分离处理 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1702863624a433719.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论