OpenCV笔记-操作矩阵和图像的函数之与操作(and)

示例代码:

#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中*/

    cvAnd(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全部初始化为1*/

    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) = 1;

        }

    }

    /*mat与mat2,使用掩模mask,使得mat与mat2结果只有相应的mask为1时才影响mat3,即mask值为0,mat3中相应的位置值不变,得到的结果放到mat3中*/

    cvAnd(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");

    }

   

    cvAndS(mat, cvScalar(1), 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");

    }



    cvReleaseMat(&mat);

    cvReleaseMat(&mat2);

    cvReleaseMat(&mat3);

    cvReleaseMat(&mask);

    return 0;

}

 

你可能感兴趣的:(opencv)