OpenCV读取图像时Alpha通道的处理

近日研究OpenCV的templateMatch,发现读取的template如果带有Alpha通道,那么templateMatch无法得到正确的结果,因此,尝试在遇到这种情况时,通过算法合并Alpha通道到BGR通道上,由于目标图像背景为纯黑,所以,这里没做其它的计算。
代码如下:

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
    Mat img =   imread( argv[1], -1 );

    if( img.empty() ) return -1;

    cout << "Image Depth:\t" << depth_string[ img.depth() ] << endl;
    cout << "Image Channels:\t" << img.channels() << endl;
    cout << "Image Dims:\t" << img.dims << endl;
    cout << "Image Size:\t" << img.size() << endl;
    cout << "Image Total:\t" << img.total() << endl;
    cout << "Image is Submatrix:\t" << img.isSubmatrix() << endl;
    cout << "Image Rows:\t" << img.rows << endl;
    cout << "Image Columns:\t" << img.cols << endl;
    cout << "Image is Continuous:\t" << ( img.isContinuous() ? "true" : "false" ) << endl;

    if( img.channels() == 4 )                     /* Should be checked if it is BGRA format */
    {
        Mat _dst;
        Mat _chs[4];

        cout << "Image is BGRA format, convert it to BGR format." << endl;

        split( img, _chs );

        for( int i = 0; i < 4; i ++ )
        {
            multiply( _chs[i], _chs[3], _chs[i] );
        }

        merge( _chs, 3, _dst );

        img =   _dst;
    }

    namedWindow( "Window", WINDOW_AUTOSIZE );
    imshow( "Window", img );

    waitKey( 0 );

    destroyWindow( "Show Image" );

    return 0;
}               /* ----------  end of function main  ---------- */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值