我正在做一个嵌入式视觉项目,无需使用颜色就可以识别圆,我制作的程序总处理时间为600毫秒,但是我发现只有执行4个二维卷积的程序块才能进行全部处理时间为300毫秒,这意味着只有该程序块需要的时间为总编程时间的50%。
这是在openCV的filter2D命令中使用二维卷积的简单示例,因为我们看到它在主函数中顺序排列,以查看执行四次时需要多少时间。行代码:cout <<“执行时间:” << time << endl;在程序显示执行时间:0.026662sec
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <ctime>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace cv;
using namespace std;
float buf_in[1280] = { 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765, 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765, 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 };
float buf_kernel[441] ={ 0.434, 1.4343, 9.434, 5.453, 3.5656, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 6.45,
1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 2.53,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.454,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 4.56,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 9.3434,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 4.656,
1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 3.343,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 2.4545,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.4334,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 7.5665,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 9.4554,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 3.545,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 7.4545,
1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 9.445,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 3.54545,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 4.546,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 6.4545,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.786,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 6.5656,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 7.4554,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 7.344};
unsigned t0, t1;
int counter = 0;
void processor(void *arg ){
printf(" Dentro de la funcion\n");
float matriz[1280];
float *buf_out = &matriz[0];
// variable local
int var_local =0;
var_local = var_local +10;
// variable global
counter = counter +10;
Mat mat_out = Mat(32,40, CV_32FC1); //output
Mat mat_flip_kernel; // se cargara la matriz rotada 180°
Mat mat_kernel( Size(21, 21), CV_32FC1, buf_kernel ); // kernel
Mat mat_in( Size(40, 32),CV_32FC1, buf_in); // input
flip(mat_kernel, mat_flip_kernel,-1); // rotando la matriz 180° para la convolucion, porque filter2d solo realiza la correlacion
Point anchor( -1,-1); // para que este centrado en el centro del kerneL
filter2D(mat_in, mat_out, -1, mat_flip_kernel, anchor, 0, BORDER_DEFAULT);
for (int i=0; i<32; i++){
float *ptr_out = mat_out.ptr<float>(i); // ptr_out puntero a float, to 1st element in row i
for (int j=0 ;j<40 ;j++){
buf_out[i*40+j] = ptr_out[j];
cout<<buf_out[i*40 + j]<<"; ";
}
}
// variable local
var_local++;
// variable global
counter++;
printf(" variable local: %d\n",var_local);
printf(" variable global : %d\n",counter);
return ;
}
int main(void){
int i = 0;
t0 = clock();
processor(NULL);
processor(NULL);
processor(NULL);
processor(NULL);
t1 = clock();
double time = (double(t1-t0)/CLOCKS_PER_SEC);
cout << "Execution Time: " << time << endl;
}
由于4个卷积是独立处理的,因此选择使用多线程以减少处理时间,但是当我实现它时,我感到惊讶的是执行多线程的progamma块需要600毫秒的处理时间,这导致整个程序需要900毫秒的处理时间,也就是说,情况变得更糟,因为实现多线程可能比使用带有线程的程序要慢。
这是以前的简单程序,它使用openCV的filter2D命令使用二维卷积,但是使用多线程,但是每个内核或线程中的每个二维卷积(即它们都是并行处理的),我想应该并行执行。对于此特定的简单示例程序,显示程序“执行时间:0.029157sec”,比使用单个线程的处理时间长一点,显然是错误的,因为该时间应该是使用一个线程的执行时间的四分之一。单线程,不是那样。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <ctime>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace cv;
using namespace std;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t thread_id[4];
float buf_in[1280] = { for reasons of space limit I do not place the data, but it is the same as the example program above};
float buf_kernel[441] ={for reasons of space limit I do not place the data, but it is the same as the example program above};
unsigned t0, t1;
int counter = 0;
void *thread_processor(void *arg ){
printf(" Dentro de Thread\n");
float matriz[1280];
float *buf_out = &matriz[0];
// variable local
pthread_mutex_lock( &mutex1 );
int var_local =0;
var_local = var_local +10;
pthread_mutex_unlock( &mutex1 );
// variable global
pthread_mutex_lock( &mutex1 );
counter = counter +10;
pthread_mutex_unlock( &mutex1 );
Mat mat_out = Mat(32,40, CV_32FC1); //output
Mat mat_flip_kernel;// = Mat(21,21, CV_32FC1); // se cargara la matriz rotada 180°
Mat mat_kernel( Size(21, 21), CV_32FC1, buf_kernel ); // kernel
Mat mat_in( Size(40, 32),CV_32FC1, buf_in); // input
flip(mat_kernel, mat_flip_kernel,-1); // rotando la matriz 180° para la convolucion, porque filter2d solo realiza la correlacion
Point anchor( -1,-1); // para que este centrado en el centro del kerneL
filter2D(mat_in, mat_out, -1, mat_flip_kernel, anchor, 0, BORDER_DEFAULT);
// pthread_mutex_lock( &mutex1 );
// cout<<"mat_out :"<<mat_out<<endl;
// pthread_mutex_unlock( &mutex1 );
// Pero al usar multithread,por eso cambie a otro tipo de declaracion de puntero que si funciono
pthread_mutex_lock( &mutex1 );
for (int i=0; i<32; i++){
float *ptr_out = mat_out.ptr<float>(i); // ptr_out puntero a float, to 1st element in row 0
for (int j=0 ;j<40 ;j++){
// pthread_mutex_lock( &mutex1 );
buf_out[i*40+j] = ptr_out[j];
// cout<<" Elemento ["<<i<<"] ["<<j<<"] :"<<buf_out[i*40 + j];
cout<<buf_out[i*40 + j]<<"; ";
// pthread_mutex_unlock( &mutex1 );
}
}
pthread_mutex_unlock( &mutex1);
// variable local
pthread_mutex_lock( &mutex1 );
var_local++;
pthread_mutex_unlock( &mutex1 );
// variable global
pthread_mutex_lock( &mutex1 );
counter++;
pthread_mutex_unlock( &mutex1 );
printf(" variable local: %d\n",var_local);
printf(" variable global : %d\n",counter);
printf(" Fuera de Thread\n");
return NULL;
}
int main(void){
int i = 0;
int error;
t0 = clock();
while(i < 4) {
error = pthread_create(&(thread_id[i]), NULL, &thread_processor,NULL);
if (error != 0){
printf("\nthere was a problem creating thread:");
}
else{
printf("\n Thread number %d created.\n", i);
}
i++;
}
for ( i =0 ; i< 4 ; i++){
pthread_join( thread_id[i], NULL);
}
t1 = clock();
double time = (double(t1-t0)/CLOCKS_PER_SEC);
cout << "Execution Time: " << time << endl;
}
有人在运行带有多个线程的解决方案时可能会帮助我找出导致程序运行缓慢的原因吗?谢谢您预先提供的所有帮助和建议!
答案 0 :(得分:0)
您绝对应该阅读一些有关并发的优秀教程或书籍,因为程序代码中存在一些严重问题:
int val_local
是一个局部变量,每个线程都有自己的变量,这里不需要互斥体。除counter
之外,其他所有变量同样如此。 for
循环也不需要互斥体,因为它不使用任何全局变量。互斥锁会导致阻塞和抢占,这会导致性能显着下降,因此仅应在需要时使用它们。我不知道下面的代码部分会发生什么,但是您在这里使用的是全局变量。同时写入它们时,可能会导致争用情况,并且需要通过互斥锁进行同步:
Mat mat_out = Mat(32,40, CV_32FC1); //output
Mat mat_flip_kernel;// = Mat(21,21, CV_32FC1); // se cargara la matriz rotada 180°
Mat mat_kernel( Size(21, 21), CV_32FC1, buf_kernel ); // kernel
Mat mat_in( Size(40, 32),CV_32FC1, buf_in); // input
flip(mat_kernel, mat_flip_kernel,-1); // rotando la matriz 180° para la convolucion, porque filter2d solo realiza la correlacion