开发者

getting alpha by subtracting two images with different backgrounds

开发者 https://www.devze.com 2023-03-20 07:16 出处:网络
I am using a program what can render color images but only without alpha information. I would like to get alpha information from those images by using two and subtracting them. I can set the backgroun

I am using a program what can render color images but only without alpha information. I would like to get alpha information from those images by using two and subtracting them. I can set the background to different colors.

My idea is that if I render an image with black background and an other one with white, then I can subtract those images from each other and get an alpha channel. But it i开发者_如何学运维s just a theory, I don't know how to do it in practise and that if there are any standard methods / algorithms for extracting alpha information out of two images by subtracting them from each other.

I would like to use a command line program (or a very easy to use library in C++) to do this processing. I have used convert.exe from ImageMagick before, but I have never used the other utilities in ImageMagick.

Is there anyone who can recommend me a way how to do it in practise? What I am looking for is some kind of a command line solution or a C++ library with easy to understand example files what can do this.

Update: My backgrounds are computer generated, solid colors. So I can set it to 0,0,0 black. Here is an example.

getting alpha by subtracting two images with different backgrounds


this might be good enough if your object differs enough from the background but this looks like it might be exactly what you asked for

EDIT: the second one ends up with this command line (replace the stuff in <> with your images)

  convert <image1> <image2> -alpha off \
          \( -clone 0,1 -compose difference -composite \
             -separate -evaluate-sequence max -auto-level -negate \) \
          \( -clone 0,2 -fx "v==0?0:u/v-u.p{0,0}/v+u.p{0,0}" \) \
          -delete 0,1 +swap -compose Copy_Opacity -composite \
          <output>

You'll need to use a format that supports alpha on the output, but that's probably what you want anyway. (NOTE: I have not actually tried this for myself so it might not work with the latest imagemagick versions since I don't think that documentation is always up to date)

Or, if you actually just use pure black and white you can do it like this:

  convert <image1> <image2> -alpha off \
          \( -clone 0,1 -compose difference -composite -negate \) \
          \( -clone 0,2 +swap -compose divide -composite \) \
          -delete 0,1 +swap -compose Copy_Opacity -composite \
          <output>
0

精彩评论

暂无评论...
验证码 换一张
取 消