图像二值化处理,可计算运行时间

来源:互联网 发布:手机钢琴编曲软件 编辑:程序博客网 时间:2024/06/05 15:42
//对图像进行灰度化及二值化
#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "winbase.h"


int main(int argc, char* argv[])
{
    //初始化图像
    IplImage * pIplSource=cvLoadImage("C:\\Users\\Administrator\\Desktop\\I.jpg");  //读取图像
    IplImage * pIplGrayscale=cvCreateImage(cvSize(pIplSource->width,pIplSource->height),IPL_DEPTH_8U,1);//执行灰度化
    IplImage * pIplThreshold=cvCreateImage(cvSize(pIplSource->width,pIplSource->height),IPL_DEPTH_8U,1);//执行二值化
    //执行灰度化和二值化,并输出所用时间
    LARGE_INTEGER frequency,count1,count2,count3;
    double time1,time2;
    QueryPerformanceFrequency(&frequency);
    for(int i=0;i<10;i++)
    {
        QueryPerformanceCounter(&count1);
        cvCvtColor(pIplSource,pIplGrayscale,CV_BGR2GRAY);
        QueryPerformanceCounter(&count2);
        cvThreshold(pIplGrayscale,pIplThreshold,128,255,CV_THRESH_BINARY);
        QueryPerformanceCounter(&count3);
        time1=(double)1000.0*(count2.QuadPart-count1.QuadPart)/frequency.QuadPart;
        time2=(double)1000.0*(count3.QuadPart-count2.QuadPart)/frequency.QuadPart;
        printf("灰度:%g毫秒,二值化:%g毫秒\r\n",time1,time2);
    }
    //显示图像
    cvNamedWindow("grayscale",0);
    cvNamedWindow("threshold",0);
    cvResizeWindow("grayscale",600,480);
    cvResizeWindow("threshold",600,480);
    cvShowImage("grayscale",pIplGrayscale);
    cvShowImage("threshold",pIplThreshold);
    cvSaveImage("C:\\Users\\Administrator\\I.jpg",pIplThreshold);
    cvWaitKey(0);
    //销毁对象
    cvDestroyAllWindows();
    cvReleaseImage(&pIplThreshold);
    cvReleaseImage(&pIplGrayscale);
    cvReleaseImage(&pIplSource);
    return 0;
}
0 0