mercredi 17 mai 2017

RedCV: how to blend images

Sometimes it's interesting to superpose and mix two images to create a new image. Once again,  the variation of intensity of image can help us to solve this problem. The idea is to apply a factor (between 0. 0 and 1.0) on the image to modify intensity.  This is done for the first image and the second image. The complement of the factor (1.0 - factor) is applied to the second image.


rcvSetIntensity function


RedCV includes  a  function to do the job.

rcvSetIntensity: function [src [image!] dst [image!] alpha [float!]]


The function applies the float factor to the src image and update the result in the destination image.
Then it's very simple to combine a first call of the function on the first image with a factor and a second call on the second image with the complement of the factor:

rcvSetIntensity src1 dest1 alpha
rcvSetIntensity src2 dest2 1.0 - alpha
Then you just need to add both images to create the blended image with the rcvAdd function:

rcvAdd dest1 dest2 dst

Code sample

Red [
    Title:   "Blend Operator "
    Author:  "Francois Jouen"
    File:    %blend2.red
    Needs:   'View
]

;this version uses rcvSetIntensity and not rcvBlend

#include %../../libs/redcv.red ; for redCV functions
margins: 10x10
img1: rcvLoadImage %../../images/lena.jpg
img2: rcvLoadImage %../../images/test.jpg
dst: rcvCreateImage img1/size
tmp1: rcvCreateImage img1/size
tmp2: rcvCreateImage img2/size
alpha: 0.5

blending: function [] [
    rcvSetIntensity img1 tmp1 alpha 
    rcvSetIntensity img2 tmp2  1.0 - alpha 
    rcvAdd tmp1 tmp2 dst
]

; ***************** Test Program ****************************
view win: layout [
        title "Blend Operator Test"
        text 60 "Image 1" 
        f1: field 50 "0.5"
        sl: slider 170 [alpha: face/data * 1.0
                    f1/text: form alpha
                    f2/text: form (1 - alpha)
                    blending
                    ]
        text 60 "Image 2" 
        f2: field 50  "0.5"
        button 60 "Quit" [  rcvReleaseImage img1 
                            rcvReleaseImage img2
                            rcvReleaseImage tmp1
                            rcvReleaseImage tmp2 
                            rcvReleaseImage dst 
                            Quit]
        return
        canvas: base 512x512 dst
        do [sl/data: alpha blending]
]

And the result for a factor = 0.5



image 1


image 2


mixed image



Aucun commentaire:

Enregistrer un commentaire