生活随笔
收集整理的這篇文章主要介紹了
【OpenCV学习】OpenMP并行化实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>void EdgeOpenMP(IplImage *src,IplImage *dst,int thresh)
{int height = src->height;int width = src->width;int step = src->widthStep;uchar *data1 = (uchar *)src->imageData;uchar *data2 = (uchar *)dst->imageData;int i=step;#pragma omp parallel forfor(i=step+1;i<height*width;i++){if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh)data2[i]=255;/* 對于單通道,前后兩幀差分大于門限或者對于多通道前后兩幀的一個指標差分大于門限,則視為邊緣*/elsedata2[i]=0;}
}void Edge(IplImage *src,IplImage *dst,int thresh)
{int height = src->height;int width = src->width;int step = src->widthStep;uchar *data1 = (uchar *)src->imageData;uchar *data2 = (uchar *)dst->imageData;int i=step;for(i=step+1;i<height*width;i++){if(abs(data1[i]-data1[i-1])>thresh || abs(data1[i]-data1[i-step])>thresh)data2[i]=255;elsedata2[i]=0;}
}int main()
{char filename[512];IplImage *src,*edge1,*edge2;puts("File name:");gets(filename);src = cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE );edge1=cvCloneImage(src);edge2=cvCloneImage(src);cvNamedWindow("src", CV_WINDOW_AUTOSIZE);cvMoveWindow("src", 100, 100);cvShowImage( "src", src);cvNamedWindow("Edge", CV_WINDOW_AUTOSIZE);cvMoveWindow("Edge", 200, 100);cvNamedWindow("EdgeOpenMP", CV_WINDOW_AUTOSIZE);cvMoveWindow("EdgeOpenMP", 300, 100);/* 以上都是準備一些窗口和圖形基本數據 */int tekrar=100;//運行次數int thresh=30;double start, end,t1, t2;/* 計算沒有使用OpenMP優化的時間 */start= (double)cvGetTickCount();//記下開始的時鐘計數,以便計算函數或用戶代碼執行時間for(int i=0;i<tekrar;i++)Edge(src,edge1,thresh);end= (double)cvGetTickCount();//記下結束的時鐘計數t1= (end-start)/((double)cvGetTickFrequency()*1000.);//計算運行時間,以毫秒為單位printf( "Run time without OpenMP = %g ms/n", t1 );/* 計算使用了OpenMP優化的時間 */start= (double)cvGetTickCount();for(int i=0;i<tekrar;i++)EdgeOpenMP(src,edge2,thresh);end= (double)cvGetTickCount();t2= (end-start)/((double)cvGetTickFrequency()*1000.);printf( "Run time with OpenMP = %g ms/n", t2 );printf( "Performance ratio (%%) = %% %.1f /n", 100*(t1/t2-1) );cvShowImage( "Edge", edge1);cvShowImage( "EdgeOpenMP", edge2);cvWaitKey();cvDestroyWindow("Edge");cvDestroyWindow("EdgeOpenMP");cvReleaseImage(&src);cvReleaseImage(&edge1);cvReleaseImage(&edge2);
}這是我的結果:
File name:
dog.jpg
Run time without OpenMP = 647.627 ms
Run time with OpenMP = 453.001 ms
Performance ratio (%) = % 43.0
?
作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/
總結
以上是生活随笔為你收集整理的【OpenCV学习】OpenMP并行化实例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。