opencv 实例演示

1. 腐蚀膨胀

main.cpp

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>

// Erosion腐蚀   Dilation膨胀   滑动条用来控制膨胀和腐蚀的次数
using namespace cv;

/// 全局变量
Mat src, erosion_dst, dilation_dst;

int erosion_elem = 0;
int erosion_size = 0;
int dilation_elem = 0;
int dilation_size = 0;
int const max_elem = 2;
int const max_kernel_size = 5;

/** Function Headers */
void Erosion( int, void* );
void Dilation( int, void* );

/** @function main */
int main()
{
  /// Load 图像
  src = imread("1.png");

  if( !src.data )
  { return -1; }

  /// 创建显示窗口
  namedWindow( "Erosion Demo", CV_WINDOW_AUTOSIZE );    // 创建腐蚀
  namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE );    // 创建膨胀
  cvMoveWindow( "Dilation Demo", src.cols, 0 );

  /// 创建腐蚀 Trackbar
  ///
  /*
  createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo",
                  &erosion_elem, max_elem,
                  Erosion );
  */
  createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo",
                  &erosion_size, max_kernel_size,
                  Erosion );


  /// 创建膨胀 Trackbar
  /*
  createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo",
                  &dilation_elem, max_elem,
                  Dilation );
  */

  createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo",
                  &dilation_size, max_kernel_size,
                  Dilation );

  /// Default start
  Erosion( 0, 0 );
  Dilation( 0, 0 );

  waitKey(0);
  return 0;
}

/**  @function Erosion  */
void Erosion( int, void* )
{
  int erosion_type;
  if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
  else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
  else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }

  Mat element = getStructuringElement( erosion_type,
                                       Size( 2*erosion_size + 1, 2*erosion_size+1 ),
                                       Point( erosion_size, erosion_size ) );

  /// 腐蚀操作
  erode( src, erosion_dst, element );
  imshow( "Erosion Demo", erosion_dst );
}

/** @function Dilation */
void Dilation( int, void* )
{
  int dilation_type;
  if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
  else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
  else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }

  Mat element = getStructuringElement( dilation_type,
                                       Size( 2*dilation_size + 1, 2*dilation_size+1 ),
                                       Point( dilation_size, dilation_size ) );
  ///膨胀操作
  dilate( src, dilation_dst, element );
  imshow( "Dilation Demo", dilation_dst );
}

可通过滑动条来控制腐蚀膨胀的次数,结果如下:

2. 二值化差分图像

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

    imwrite("last.jpg", 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;
        }
    }
}

猜你喜欢

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