OpenCV笔记-操作矩阵和图像的函数之取绝对值(abs)

示例代码:

#include <cv.h>

#include <cstdio>



int main()

{

    CvMat *mat = cvCreateMat(3, 3, CV_32SC1);

    CvMat *mat2 = cvCreateMat(3, 3, CV_32SC1);

    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;

        }

    }

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

    }

    /*mat的绝对值放到mat2中*/

    cvAbs(mat, mat2);

    printf("取绝对值后mat的值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");

    }

    

    CvMat *mat3 = cvCreateMat(3, 3, CV_32SC1);

    /*mat-mat2的绝对值放到mat3中*/

    cvAbsDiff(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与常数CvScalar的差的绝对值放到mat3中*/

    cvAbsDiffS(mat3, mat3, cvScalarAll(5));

    printf("mat3-5后的绝对值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);

    return 0;

}

 

你可能感兴趣的:(opencv)