Skip to content

Problem for Simple Alpha Blending #16538

@UnaNancyOwen

Description

@UnaNancyOwen
System information (version)
  • OpenCV => 4.2.0
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2019
Detailed description

If running following the code for alpha blending, it output following results.

#include <opencv2/opencv.hpp>

int main( int argc, char* argv[] )
{
    // Read Image
    cv::Mat src = cv::imread( "lena.jpg" );

    // Alpha Blend with Blue, Green, and Red
    double alpha = 0.6;
    double beta  = 1.0 - alpha;
    cv::Mat blend_b = alpha * src + beta * cv::Scalar( 255, 0, 0 ); // Blue (issue case!)
    cv::Mat blend_g = alpha * src + beta * cv::Scalar( 0, 255, 0 ); // Green
    cv::Mat blend_r = alpha * src + beta * cv::Scalar( 0, 0, 255 ); // Red

    // Show Image
    cv::imshow( "src"  , src     );
    cv::imshow( "blue" , blend_b );
    cv::imshow( "green", blend_g );
    cv::imshow( "red"  , blend_r );
    cv::waitKey( 0 );

    return 0;
}

The output of alpha blending with green and red is correct result.
These turned greenish color and reddish color.
But, the output of alpha blending with blue has become whitish color.
It is not expected output. It should turn to bluish color.

output1

If changes blend color to the close to blue such as cv::Scalar(255, 1, 0) (just add one to green), it turn correctly to bluish color.

cv::Mat blend_b = alpha * src + beta * cv::Scalar( 255, 1, 0 ); // Close to Blue

output2

Related issues

This seems to be related to this issue #14738.
I think this is due to the difference between calculation results of cv::Mat and cv::MatExpr.
Therefore, I know that this issue can be avoided by changing to following code.

- cv::Mat blend_b = alpha * src + beta * cv::Scalar( 255, 0, 0 ); // Blue
+ cv::Mat blend_b = cv::Mat( alpha * src ) + beta * cv::Scalar( 255, 0, 0 ); // Blue

But, I think this is a bug of corner case. It should be fixed.
What do you think? Thanks,

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions