OpenCV笔记-操作矩阵和图像的函数之加和阿尔法融合(add)

矩阵A+矩阵B

矩阵A+常数

使用掩码来影响结果

阿尔法融合

示例代码:

#include <cv.h>
#include <cstdio>

int main()
{
    CvMat *mat = cvCreateMat(3, 3, CV_32SC1);
    CvMat *mat2 = cvCreateMat(3, 3, CV_32SC1);
    CvMat *mat3 = cvCreateMat(3, 3, CV_32SC1);
    CvMat *mask = cvCreateMat(3, 3, CV_8UC1);
    /*初始化矩阵mat*/
    for(int i = 0; i < mat->height; ++i)
    {
        int *ptr = (int*)(mat->data.ptr + mat->step * i);
        for(int j = 0; j < mat->width; ++j)
        {
            *(ptr + j) = j;
        }
    }
    /*初始化矩阵mat2*/
    for(int i = 0; i < mat2->height; ++i)
    {
        int *ptr = (int*)(mat2->data.ptr + mat2->step * i);
        for(int j = 0; j < mat2->width; ++j)
        {
            *(ptr + j) = j + 1;
        }
    }
    /*初始化掩模mask*/
    for(int i = 0; i < mask->height; ++i)
    {
        uchar *ptr = mask->data.ptr + mask->step * i;
        for(int j = 0; j < mask->width; ++j)
        {
            if(j % 2 == 0)
                *(ptr + j) = 1;
            else
                *(ptr + j) = 0;
        }
    }
    printf("mat的值:\n");
    for(int i = 0; i < mat->height; ++i)
    {
        int *ptr = (int*)(mat->data.ptr + mat->step * i);
        for(int j = 0; j < mat->width; ++j)
        {
            printf("%d ", *(ptr + j));
        }
        printf("\n");
    }
    printf("mat2的值:\n");
    for(int i = 0; i < mat2->height; ++i)
    {
        int *ptr = (int*)(mat2->data.ptr + mat2->step * i);
        for(int j = 0; j < mat2->width; ++j)
        {
            printf("%d ", *(ptr + j));
        }
        printf("\n");
    }
    /*mat+mat2放到mat3中*/
    cvAdd(mat, mat2, mat3);
    printf("mat加mat2的结果mat3的值:\n");
    for(int i = 0; i < mat3->height; ++i)
    {
        int *ptr = (int*)(mat3->data.ptr + mat3->step * i);
        for(int j = 0; j < mat3->width; ++j)
        {
            printf("%d ", *(ptr + j));
        }
        printf("\n");
    }
    
    /*mat3清空*/
    for(int i = 0; i < mat3->height; ++i)
    {
        int *ptr = (int*)(mat3->data.ptr + mat3->step * i);
        for(int j = 0; j < mat3->width; ++j)
        {
            *(ptr + j) = 0;
        }
    }
    /*mat+mat2,使用掩模mask,使得mat+mat2结果只有相应的mask为1时才影响mat3,即mask值为0,mat3中相应的位置值不变,得到的结果放到mat3中*/
    cvAdd(mat, mat2, mat3, mask);
    printf("使用掩模mask后,mat加mat2的结果mat3的值:\n");
    for(int i = 0; i < mat3->height; ++i)
    {
        int *ptr = (int*)(mat3->data.ptr + mat3->step * i);
        for(int j = 0; j < mat3->width; ++j)
        {
            printf("%d ", *(ptr + j));
        }
        printf("\n");
    }
   
    cvAddS(mat, cvScalar(4), mat3, mask);
    printf("使用掩模mask后,mat加常数4的结果mat3的值:\n");
    for(int i = 0; i < mat3->height; ++i)
    {
        int *ptr = (int*)(mat3->data.ptr + mat3->step * i);
        for(int j = 0; j < mat3->width; ++j)
        {
            printf("%d ", *(ptr + j));
        }
        printf("\n");
    }

    double a, b, y;
    a=0.7;
    b=0.3;
    y=5;

    cvAddWeighted(mat, a, mat2, b, y, mat3);
    printf("a融合:mat*a+mat2*b+y的结果mat3的值:\n");
    for(int i = 0; i < mat3->height; ++i)
    {
        int *ptr = (int*)(mat3->data.ptr + mat3->step * i);
        for(int j = 0; j < mat3->width; ++j)
        {
            printf("%d ", *(ptr + j));
        }
        printf("\n");
    }
    cvReleaseMat(&mat);
    cvReleaseMat(&mat2);
    cvReleaseMat(&mat3);
    cvReleaseMat(&mask);
    return 0;
}

图像与图像的阿尔法融合

示例代码:

#include <cv.h>
#include <highgui.h>

int main()
{
    IplImage *src1, *src2;

    src1 = cvLoadImage("E:\\Res\\Lighthouse.jpg");
    src2 = cvLoadImage("E:\\Res\\20071211105321519_2.jpg");

    double alpha, beta, gamma;
    alpha = 0.5;
    beta = 0.5;
    gamma = 0;
    cvSetImageROI(src1, cvRect(840, 280, 64, 56));
    cvSetImageROI(src2, cvRect(240, 210, 64, 56));

    cvAddWeighted(src1, alpha, src2, beta, gamma, src1);
    cvResetImageROI(src1);
    cvResetImageROI(src2);

    cvNamedWindow("alpha融合示例");
    cvShowImage("alpha融合示例", src1);
    cvWaitKey(0);

    cvReleaseImage(&src1);
    cvReleaseImage(&src2);
    cvDestroyWindow("alpha融合示例");
    return 0;
}

效果如下:

OpenCV笔记-操作矩阵和图像的函数之加和阿尔法融合(add)_第1张图片

OpenCV笔记-操作矩阵和图像的函数之加和阿尔法融合(add)_第2张图片

OpenCV笔记-操作矩阵和图像的函数之加和阿尔法融合(add)_第3张图片

你可能感兴趣的:(opencv)