opencv之图像差分(4)

1.图像差分后取灰度值

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <opencv2/opencv.hpp>

using namespace cv;
void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos);

int main()
{
    Mat srcimage1 = imread("1.png");
    Mat srcimage2 = imread("2.png");
    Mat dstimage;
    Mat dstimage1;
    dstimage.create(srcimage1.rows, srcimage1.cols, srcimage1.type());
    dstimage1.create(srcimage1.rows, srcimage1.cols, srcimage1.type());
    onTrackerSlid(srcimage1, srcimage2, dstimage, 100);   //将图1与图2的差分结果保存在dstimage中
    namedWindow("srcimage1", CV_WINDOW_AUTOSIZE);
    namedWindow("srcimage2", CV_WINDOW_AUTOSIZE);
    namedWindow("dstimage", CV_WINDOW_AUTOSIZE);
    imshow("srcimage1", srcimage1);
    imshow("srcimage2", srcimage2);
    imshow("dstimage", dstimage);
    cvtColor(dstimage, dstimage, COLOR_RGB2GRAY);
    threshold(dstimage, dstimage1, 150, 255, THRESH_BINARY);   //记得加-lopencv_imgproc320
    namedWindow("dstimage1", CV_WINDOW_AUTOSIZE);
    imshow("dstimage1", dstimage1);

    waitKey(0);
    return 0;
}

void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos)
{
    uchar *data1 = NULL;
    uchar *data2 = NULL;
    uchar *data3 = NULL;
    //uchar *data = NULL;
    int i, j;

    outputimage = inputimage1.clone();
    int rowNumber = outputimage.rows;
    int colNumber = outputimage.cols*outputimage.channels();
    int step = outputimage.step/sizeof(uchar);
    data1 = (uchar*)inputimage1.data;
    data2 = (uchar*)inputimage2.data;
    data3 = (uchar*)outputimage.data;

    for(i = 0; i < rowNumber; i++)
    {
        //data = (uchar*)outputimage.ptr<uchar>(i);   //获取第i行的首地址
        for(j = 0; j < colNumber; j++)
        {
            if(fabs(data2[i*step + j] - data1[i*step + j]) > pos)
                data3[i*step + j] = 255;
            else
                data3[i*step + j] = 0;
        }
    }
}

结果

2. 图像差分后按权重处理

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos);

int main()
{
    double alphaValue = 1;
    double betaValue = 1;

    Mat image1 = imread("1.png");
    Mat image2 = imread("2.png");
    Mat image3 = imread("3.png");
    Mat image4 = imread("4.png");

    //cvtColor(image1, image1, COLOR_RGB2GRAY);
    //cvtColor(image2, image2, COLOR_RGB2GRAY);

    Mat dstimage1;
    Mat dstimage2;
    Mat dstimage3;
    Mat dst1;
    Mat dst2;
    dstimage1.create(image1.rows, image1.cols, image1.type());
    dstimage2.create(image1.rows, image1.cols, image1.type());
    dstimage3.create(image1.rows, image1.cols, image1.type());
    dst1.create(image1.rows, image1.cols, image1.type());
    dst2.create(image1.rows, image1.cols, image1.type());

    onTrackerSlid(image1, image2, dstimage1, 94);   //将图1与图2的差分结果保存在dstimage1中
    onTrackerSlid(image1, image3, dstimage2, 94);   //图1与图3
    onTrackerSlid(image1, image4, dstimage3, 94);    //图1与图4

    //betaValue = (1.0 - alphaValue);
    addWeighted(dstimage1, alphaValue, dstimage2, betaValue, 0.0, dst1);  //线性混合操作
    addWeighted(dst1, alphaValue, dstimage3, betaValue, 0.0, dst2);

    //bitwise_and(dstimage1, dstimage2, dst1);   //逻辑交运算,这里不适用
    //bitwise_and(dst1, dstimage3, dst2);


    namedWindow("dstimage1", CV_WINDOW_AUTOSIZE);
    namedWindow("dstimage2", CV_WINDOW_AUTOSIZE);
    namedWindow("dstimage3", CV_WINDOW_AUTOSIZE);
    namedWindow("dst1", CV_WINDOW_AUTOSIZE);
    namedWindow("dst2", CV_WINDOW_AUTOSIZE);

    //cvtColor(dst2, dst2, COLOR_RGB2GRAY);

    imwrite("last.jpg", dst2);      //将dst2输出到文件last.jpg中

    imshow("dstimage1", dstimage1);
    imshow("dstimage2", dstimage2);
    imshow("dstimage3", dstimage3);
    imshow("dst1", dst1);
    imshow("dst2", dst2);

    waitKey(0);
    return 0;
}

void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos)
{
    uchar *data1 = NULL;
    uchar *data2 = NULL;
    uchar *data3 = NULL;
    int i, j;

    outputimage = inputimage1.clone();
    int rowNumber = outputimage.rows;
    int colNumber = outputimage.cols*outputimage.channels();
    int step = outputimage.step/sizeof(uchar);
    data1 = (uchar*)inputimage1.data;
    data2 = (uchar*)inputimage2.data;
    data3 = (uchar*)outputimage.data;

    for(i = 0; i < rowNumber; i++)
    {
        for(j = 0; j < colNumber; j++)
        {
            if(fabs(data2[i*step + j] - data1[i*step + j]) > pos)
                data3[i*step + j] = 255;
            else
                data3[i*step + j] = 0;
        }
    }
}

结果

猜你喜欢

转载自blog.csdn.net/juliarjuliar/article/details/79812683