OpenCV:使用Trimap图像 [英] OpenCV : Using a Trimap image
问题描述
我找到了这个猫和猫的图像数据集: The Oxford-IIIT宠物数据集.每个图像都有一个像素级的前景背景分割(trimap)图像.
I found this dog and cat image dataset: The Oxford-IIIT Pet Dataset. Each image has a pixel level foreground-background segmentation (trimap) image.
在Internet上搜索时,我看到trimap是具有三种颜色的图像(一种用于背景,一种用于前景,一种用于未分类的区域),但是这里的图像全是黑色的.
Searching the internet, I saw that trimap is an image with three colors (one for the background, one for the foreground and one for the not-classified region), but here the image is all black.
是错误还是正确?但最重要的是,我想知道是否以及如何使用它在给定正常图像的情况下获得黑色背景上的猫或狗的新图像.
Is it a mistake or is it correct? But above all I want to know if and how you can use it to get, given a normal image, a new image with the cat or dog on a black background.
谢谢.
推荐答案
trimap看起来是黑色的,因为它们只包含0-255范围内的0-2像素范围内的像素值,其中:
The trimaps look black because they only contain pixels values ranging from 0-2 on a scale of 0-255, where:
- 1代表宠物"
- 2表示背景"
- 3表示边界"
查看文本形式的像素:
identify -verbose Abyssinian_1trimap.png | more
输出
Histogram:
22938: ( 1, 1, 1) #010101 gray(1)
198766: ( 2, 2, 2) #020202 gray(2)
18296: ( 3, 3, 3) #030303 gray(3)
如果对比拉伸三幅图,则可以看到更好的图像.我在这里使用Abyssinian1
图片:
If you contrast stretch the trimaps, you can see better. I am using the Abyssinian1
image here:
convert Abyssinian_1trimap.png -auto-level trimap.jpg
如果将Trimap中的所有1
像素设置为白色,将所有2
像素设置为黑色,将所有3
像素设置为白色,并使用darken
混合将其与实际照片混合,则将得到你想要的:
If you make all the 1
pixels in the trimap white and all the 2
pixels black and all the 3
pixels white and blend that with the actual photo using a darken
blend, you will get what you want:
convert Abyssinian_1.jpg \( Abyssinian_1trimap.png -fill white -opaque "rgb(1,1,1)" -opaque "rgb(3,3,3)" -fill black -opaque "rgb(2,2,2)" \) -compose darken -composite pet.png
如果您想要边境以及宠物,请执行以下操作:
If you want the border as well as the pet, do this:
convert Abyssinian_1.jpg \( Abyssinian_1trimap.png -fill white -opaque "rgb(1,1,1)" -opaque "rgb(3,3,3)" -fill black -opaque "rgb(2,2,2)" \) -compose darken -composite pet.png
您还可以尝试通过模糊蒙版来柔化边缘:
You can also experiment with blurring the mask to soften the edges:
convert Abyssinian_1.jpg \( Abyssinian_1trimap.png -fill white -opaque "rgb(1,1,1)" -fill black -opaque "rgb(3,3,3)" -opaque "rgb(2,2,2)" -blur 0x8 \) -compose darken -composite pet.png
对不起,我使用 ImageMagick 做到了这一点,因为我发现它更容易安装在大多数Linux发行版中,并且可用于macOS和Windows.这些原理与OpenCV相同.
Sorry, I did it with ImageMagick because I find that easier and it is installed on most Linux distros and available for macOS and Windows. The principles are the same for OpenCV.
这篇关于OpenCV:使用Trimap图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!