opencv图像分割多线程执行
- 1、模板多线程函数
- 2、vector数组多线程函数
- 3、多线程类
1、模板多线程函数
#include
#include
#include
#include
#include
#include
#include
#include
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
using std::thread;
using std::vector;
using std::accumulate;
using std::cout;
using std::endl;
template <typename Iterator, typename T> class Accum
{
public:
void operator() (Iterator first, Iterator last, T& sum)
{
sum = 0;
for (; first != last; ++first)
{
auto k = std::sqrtf(*first);
int i = 0;
for (i = 2; i <= k; i++)
{
if ((*first) % i == 0)
{
break;
}
}
if (i > k)
{
++sum;
}
}
}
};
template <typename Iterator, typename T>
T ParallelAccum(Iterator first, Iterator last, T& sum)
{
const unsigned int len = std::distance(first, last);
if (!len)
{
return sum;
}
const unsigned int num_thread = thread::hardware_concurrency();
cout << "总线程数:" << num_thread << endl;
const unsigned int block_size = len / num_thread;
vector<T> results(num_thread);
vector<thread> threads(num_thread - 1);
Iterator block_begin = first;
for (unsigned int i = 0; i < (num_thread - 1); ++i)
{
Iterator block_end = block_begin;
std::advance(block_end, block_size);
threads[i] = thread(Accum<Iterator, T>(), block_begin, block_end, std::ref(results[i]));
block_begin = block_end;
}
Accum<Iterator, T>()(block_begin, last, results[num_thread - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&thread::join));
return accumulate(results.begin(), results.end(), sum);
}
LARGE_INTEGER t1, t2, tc;
void time_begin()
{
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
}
float time_end()
{
QueryPerformanceCounter(&t2);
return ((t2.QuadPart - t1.QuadPart) * 1.0 / tc.QuadPart) * 1000;
}
vector<Mat> splitImg(Mat image, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
vector<Mat> ceil_img;
Mat image_cut, roi_img;
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect rect(i * sub_width, j * sub_height, sub_width, sub_height);
image_cut = Mat(image, rect);
roi_img = image_cut.clone();
ceil_img.push_back(roi_img);
}
}
return ceil_img;
}
Mat MergeImg(Mat image, vector<Mat> ceil_img, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
int t = 0;
Mat MergeImage(image.size(), image.type());
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect ROI(i * sub_width, j * sub_height, sub_width, sub_height);
ceil_img[t].copyTo(MergeImage(ROI));
t++;
}
}
return MergeImage;
}
template <typename Iterator, typename T> class Accume
{
public:
void operator() (Iterator first, Iterator last, T& sum)
{
sum = 1;
for (; first != last; ++first)
{
auto ceil_img = *first;
Rect rect = Rect(0, 0, ceil_img.cols, ceil_img.rows);
Scalar color = Scalar(0, 255, 0);
rectangle(ceil_img, rect, color, 0.1, LINE_8);
}
}
};
template <typename Iterator, typename T>
T ParallelAccume(Iterator first, Iterator last, T& sum)
{
const unsigned int len = std::distance(first, last);
if (!len)
{
return sum;
}
const unsigned int num_thread = thread::hardware_concurrency();
cout << "总线程数:" << num_thread << endl;
const unsigned int block_size = len / num_thread;
vector<T> results(num_thread);
vector<thread> threads(num_thread - 1);
Iterator block_begin = first;
for (unsigned int i = 0; i < (num_thread - 1); ++i)
{
Iterator block_end = block_begin;
std::advance(block_end, block_size);
threads[i] = thread(Accume<Iterator, T>(), block_begin, block_end, std::ref(results[i]));
block_begin = block_end;
}
Accume<Iterator, T>()(block_begin, last, results[num_thread - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&thread::join));
return accumulate(results.begin(), results.end(), sum);
}
template <typename VecoterSrc, typename VecoterTemp, typename First, typename Fast, typename T> class Accume2
{
public:
void operator() (VecoterSrc Srcv, VecoterSrc Tempv, First first, First last, T& sum)
{
cout << "处理个数: " << last - first << " 两个值:" << first << "&" << last << endl;
cout << "" << endl;
for (; first != last; ++first)
{
sum = 1;
Rect rect = Rect(0, 0, Srcv[first].cols, Srcv[first].rows);
Scalar color = Scalar(0, 255, 0);
rectangle(Srcv[first], rect, color, 0.1, LINE_8);
circle(Srcv[first], Point(Srcv[first].cols / 2, Srcv[first].rows / 2), 10, Scalar(0, 255, 0), -1);
Rect rect2 = Rect(0, 0, Tempv[first].cols, Tempv[first].rows);
Scalar color2 = Scalar(255, 0, 0);
rectangle(Tempv[first], rect, color2, 0.1, LINE_8);
circle(Tempv[first], Point(Tempv[first].cols / 2, Tempv[first].rows / 2), 10, Scalar(0, 255, 0), -1);
}
}
};
template <typename VoctorImg, typename T>
T ParallelAccumed(VoctorImg Srcv, VoctorImg Tempv, T first, T last, T& sum)
{
const unsigned int len = last - first;
if (!len)
{
return sum;
}
const unsigned int num_thread = thread::hardware_concurrency();
cout << "总线程数:" << num_thread << endl;
const unsigned int block_size = len / (num_thread - 1);
vector<T> results(num_thread);
vector<thread> threads(num_thread - 1);
unsigned int block_begin = first;
for (unsigned int i = 0; i < (num_thread - 1); ++i)
{
unsigned int block_end = block_begin + block_size;
threads[i] = thread(Accume2<VoctorImg, VoctorImg, T, T, T>(), Srcv, Tempv, block_begin, block_end, std::ref(results[i]));
block_begin = block_end;
}
Accume2<VoctorImg, VoctorImg, T, T, T>()(Srcv, Tempv, block_begin, last, results[num_thread - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&thread::join));
return accumulate(results.begin(), results.end(), sum);
}
int main()
{
int sum = 0;
Mat src;
src = imread("../Image.bmp");
if (!src.data) { printf("erro"); return false; }
vector<Mat> ceil_img = splitImg(src, 7, 10);
Mat templ;
templ = imread("../banjing.jpeg");
if (!templ.data) { printf("erro"); return false; }
vector<Mat> ceil_img2 = splitImg(templ, 7, 10);
time_begin();
sum = ParallelAccumed<vector<Mat>, int>(ceil_img, ceil_img2, 0, ceil_img.size(), sum);
cout << "time thread: " << time_end() << ", sum = " << sum << endl;
Mat MergeImage = MergeImg(src, ceil_img, 7, 10);
namedWindow("merge", WINDOW_NORMAL);
imshow("merge", MergeImage);
Mat MergeImage2 = MergeImg(templ, ceil_img2, 7, 10);
namedWindow("merge2", WINDOW_NORMAL);
imshow("merge2", MergeImage2);
waitKey(0);
return 0;
}
2、vector数组多线程函数
#include
#include
#include
#include
#include
#include
#include
#include
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
LARGE_INTEGER t1, t2, tc;
void time_begin()
{
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
}
float time_end()
{
QueryPerformanceCounter(&t2);
return ((t2.QuadPart - t1.QuadPart) * 1.0 / tc.QuadPart) * 1000;
}
vector<Mat> splitImg(Mat image, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
vector<Mat> ceil_img;
Mat image_cut, roi_img;
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect rect(i * sub_width, j * sub_height, sub_width, sub_height);
image_cut = Mat(image, rect);
roi_img = image_cut.clone();
ceil_img.push_back(roi_img);
}
}
return ceil_img;
}
Mat MergeImg(Mat image, vector<Mat> ceil_img, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
int t = 0;
Mat MergeImage(image.size(), image.type());
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect ROI(i * sub_width, j * sub_height, sub_width, sub_height);
ceil_img[t].copyTo(MergeImage(ROI));
t++;
}
}
return MergeImage;
}
int CheckFace(Mat srcImg, Mat tempImg, int& num, int first)
{
Mat imgSrcRef = srcImg;
Mat imgSrcTest = tempImg;
Mat imgGrayRef;
cvtColor(imgSrcRef, imgGrayRef, COLOR_BGR2GRAY);
Mat imgGrayTest;
cvtColor(imgSrcTest, imgGrayTest, COLOR_BGR2GRAY);
Mat diffimg = imgGrayRef - imgGrayTest;
diffimg = 255 - diffimg;
Mat diffimgEro;
Mat diffimgDil;
GaussianBlur(diffimg, diffimg, Size(5, 5), 4.5);
adaptiveThreshold(diffimg, diffimg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 3);
Mat element = getStructuringElement(MORPH_RECT, Size(2, 4));
morphologyEx(diffimg, diffimgEro, MORPH_DILATE, element);
element = getStructuringElement(MORPH_RECT, Size(4, 4));
morphologyEx(diffimgEro, diffimgDil, MORPH_ERODE, element);
long ts3 = cv::getTickCount();
SimpleBlobDetector::Params params;
params.filterByArea = true;
params.minArea = 30;
params.maxArea = 2000;
params.filterByCircularity = false;
params.filterByColor = false;
params.filterByConvexity = false;
params.filterByInertia = false;
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
vector<KeyPoint> keypoints;
detector->detect(diffimgDil, keypoints);
cout << keypoints.size() << endl;
drawKeypoints(imgGrayTest, keypoints, tempImg, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
Rect rect2 = Rect(0, 0, tempImg.cols, tempImg.rows);
Scalar color2 = Scalar(255, 0, 0);
rectangle(tempImg, rect2, color2, 0.1, LINE_8);
circle(tempImg, Point(tempImg.cols / 2, tempImg.rows / 2), 10, Scalar(0, 255, 0), -1);
putText(tempImg, to_string(first), Point(tempImg.cols / 2, tempImg.rows / 2), FONT_HERSHEY_COMPLEX, 3, Scalar(250, 225, 100));
Rect rect = Rect(0, 0, srcImg.cols, srcImg.rows);
Scalar color = Scalar(0, 255, 0);
rectangle(srcImg, rect, color, 0.1, LINE_8);
circle(srcImg, Point(srcImg.cols / 2, srcImg.rows / 2), 10, Scalar(0, 255, 0), -1);
putText(srcImg, to_string(first), Point(srcImg.cols / 2, srcImg.rows / 2), FONT_HERSHEY_COMPLEX, 3, Scalar(250, 225, 100));
long te3 = cv::getTickCount();
cout << "Time3 毫秒: " << (te3 - ts3) / cv::getTickFrequency() * 1000 << " " << "ms" << std::endl;
cout << "Time3 秒: " << (te3 - ts3) / cv::getTickFrequency() << " " << "s" << std::endl;
num += keypoints.size();
bool isNG = false;
if (keypoints.size() > 0)
{
isNG = true;
}
cout << isNG << endl;
return 1;
}
void Accume2(vector<Mat> Srcv, vector<Mat> Tempv, int first, int last, int& sum)
{
cout << "处理个数: " << last - first << " 两个值:" << first << "&" << last << endl;
cout << "" << endl;
sum = 0;
for (; first != last; ++first)
{
CheckFace(Srcv[first], Tempv[first], sum, first);
}
};
int ParallelAccumed(vector<Mat> Srcv, vector<Mat> Tempv, int first, int last, int& sum)
{
const unsigned int len = last - first;
if (!len)
{
return sum;
}
const unsigned int num_thread = thread::hardware_concurrency();
cout << "总线程数:" << num_thread << endl;
const unsigned int block_size = len / (num_thread - 1);
vector<int> results(num_thread);
vector<thread> threads(num_thread - 1);
unsigned int block_begin = first;
for (unsigned int i = 0; i < (num_thread - 1); ++i)
{
unsigned int block_end = block_begin + block_size;
threads[i] = thread(Accume2, Srcv, Tempv, block_begin, block_end, std::ref(results[i]));
block_begin = block_end;
}
Accume2(Srcv, Tempv, block_begin, last, results[num_thread - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&thread::join));
return accumulate(results.begin(), results.end(), sum);
}
class MyThread {
public:
void myThread_in(int a,int c)
{
cout << "thread start "<<a <<";;;" << c << endl;
}
};
int main()
{
MyThread Thread;
std::thread thread_test(&MyThread::myThread_in, &Thread,1000,9999);
thread_test.join();
int sum = 0;
int Row = 5, Clo = 5;
Mat src;
src = imread("../pp/40/ok/Image_27.bmp");
if (!src.data) { printf("erro"); return false; }
vector<Mat> ceil_img = splitImg(src, Row, Clo);
Mat templ;
templ = imread("../pp/40/ng/Image_62.bmp");
if (!templ.data) { printf("erro"); return false; }
vector<Mat> ceil_img2 = splitImg(templ, Row, Clo);
int num = 0;
Mat tempg;
templ.copyTo(tempg);
CheckFace(src, tempg, num, 1);
namedWindow("tempg", WINDOW_NORMAL);
imshow("tempg", tempg);
cout << "总数" << num << endl;
time_begin();
sum = ParallelAccumed(ceil_img, ceil_img2, 0, ceil_img.size(), sum);
cout << "time thread all: " << time_end() << ", sum = " << sum << endl;
Mat MergeImage = MergeImg(src, ceil_img, Row, Clo);
namedWindow("mergeSrc", WINDOW_NORMAL);
imshow("mergeSrc", MergeImage);
Mat MergeImage2 = MergeImg(templ, ceil_img2, Row, Clo);
namedWindow("merge2Temp", WINDOW_NORMAL);
imshow("merge2Temp", MergeImage2);
waitKey(0);
return 0;
}
3、多线程类
#include
#include
#include
#include
#include
#include
#include
#include
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "Face.h"
using namespace std;
using namespace cv;
LARGE_INTEGER t1, t2, tc;
void time_begin()
{
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
}
float time_end()
{
QueryPerformanceCounter(&t2);
return ((t2.QuadPart - t1.QuadPart) * 1.0 / tc.QuadPart) * 1000;
}
vector<Mat> splitImg(Mat image, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
vector<Mat> ceil_img;
Mat image_cut, roi_img;
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect rect(i * sub_width, j * sub_height, sub_width, sub_height);
image_cut = Mat(image, rect);
roi_img = image_cut.clone();
ceil_img.push_back(roi_img);
}
}
return ceil_img;
}
Mat MergeImg(Mat image, vector<Mat> ceil_img, int row, int col)
{
int width = image.cols;
int height = image.rows;
int sub_width = width / col;
int sub_height = height / row;
int t = 0;
Mat MergeImage(image.size(), ceil_img[0].type());
for (int j = 0; j < row; j++)
{
for (int i = 0; i < col; i++)
{
Rect ROI(i * sub_width, j * sub_height, sub_width, sub_height);
ceil_img[t].copyTo(MergeImage(ROI));
t++;
}
}
return MergeImage;
}
class MyThread {
public:
void myThread_in(int a, int c)
{
cout << "thread start " << a << ";;;" << c << endl;
}
void FaceCheckPart(Mat imgSrcRef, Mat imgSrcTest, int& num)
{
Mat imgGrayRef;
cvtColor(imgSrcRef, imgGrayRef, COLOR_BGR2GRAY);
Mat imgGrayTest;
cvtColor(imgSrcTest, imgGrayTest, COLOR_BGR2GRAY);
Mat diffimg = imgGrayRef - imgGrayTest;
diffimg = 255 - diffimg;
Mat diffimgEro;
Mat diffimgDil;
GaussianBlur(diffimg, diffimg, Size(5, 5), 4.5);
adaptiveThreshold(diffimg, diffimg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 3);
Mat element = getStructuringElement(MORPH_RECT, Size(2, 4));
morphologyEx(diffimg, diffimgEro, MORPH_DILATE, element);
element = getStructuringElement(MORPH_RECT, Size(4, 4));
morphologyEx(diffimgEro, diffimgDil, MORPH_ERODE, element);
SimpleBlobDetector::Params params;
params.filterByArea = true;
params.minArea = 30;
params.maxArea = 2000;
params.filterByCircularity = false;
params.filterByColor = false;
params.filterByConvexity = false;
params.filterByInertia = false;
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
vector<KeyPoint> keypoints;
detector->detect(diffimgDil, keypoints);
drawKeypoints(imgGrayTest, keypoints, imgSrcTest, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
num += keypoints.size();
}
void CheckFace22(Mat srcImg, Mat tempImg, int& num, int first)
{
Mat imgSrcRef = srcImg;
Mat imgSrcTest = tempImg;
Mat imgGrayRef;
cvtColor(imgSrcRef, imgGrayRef, COLOR_BGR2GRAY);
Mat imgGrayTest;
cvtColor(imgSrcTest, imgGrayTest, COLOR_BGR2GRAY);
Mat diffimg = imgGrayRef - imgGrayTest;
diffimg = 255 - diffimg;
Mat diffimgEro;
Mat diffimgDil;
GaussianBlur(diffimg, diffimg, Size(5, 5), 4.5);
adaptiveThreshold(diffimg, diffimg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 3);
Mat element = getStructuringElement(MORPH_RECT, Size(2, 4));
morphologyEx(diffimg, diffimgEro, MORPH_DILATE, element);
element = getStructuringElement(MORPH_RECT, Size(4, 4));
morphologyEx(diffimgEro, diffimgDil, MORPH_ERODE, element);
SimpleBlobDetector::Params params;
params.filterByArea = true;
params.minArea = 30;
params.maxArea = 2000;
params.filterByCircularity = false;
params.filterByColor = false;
params.filterByConvexity = false;
params.filterByInertia = false;
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
vector<KeyPoint> keypoints;
detector->detect(diffimgDil, keypoints);
drawKeypoints(imgGrayTest, keypoints, tempImg, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
Rect rect2 = Rect(0, 0, tempImg.cols, tempImg.rows);
Scalar color2 = Scalar(255, 0, 0);
rectangle(tempImg, rect2, color2, 0.1, LINE_8);
circle(tempImg, Point(tempImg.cols / 2, tempImg.rows / 2), 10, Scalar(0, 255, 0), -1);
putText(tempImg, to_string(first), Point(tempImg.cols / 2, tempImg.rows / 2), FONT_HERSHEY_COMPLEX, 3, Scalar(250, 225, 100));
Rect rect = Rect(0, 0, srcImg.cols, srcImg.rows);
Scalar color = Scalar(0, 255, 0);
rectangle(srcImg, rect, color, 0.1, LINE_8);
circle(srcImg, Point(srcImg.cols / 2, srcImg.rows / 2), 10, Scalar(0, 255, 0), -1);
putText(srcImg, to_string(first), Point(srcImg.cols / 2, srcImg.rows / 2), FONT_HERSHEY_COMPLEX, 3, Scalar(250, 225, 100));
num += keypoints.size();
bool isNG = false;
if (keypoints.size() > 0)
{
isNG = true;
}
}
void Accume22(vector<Mat> Srcv, vector<Mat> Tempv, int first, int last, int& sum)
{
sum = 0;
for (; first != last; ++first)
{
CheckFace22(Srcv[first], Tempv[first], sum,first);
}
}
int ParallelAccumed22(vector<Mat> Srcv, vector<Mat> Tempv, int first, int last, int& sum)
{
const unsigned int len = last - first;
if (!len)
{
return sum;
}
const unsigned int num_thread = thread::hardware_concurrency();
const unsigned int block_size = len / (num_thread - 1);
vector<int> results(num_thread);
vector<thread> threads(num_thread - 1);
unsigned int block_begin = first;
for (unsigned int i = 0; i < (num_thread - 1); ++i)
{
unsigned int block_end = block_begin + block_size;
threads[i] = thread(&MyThread::Accume22, this, Srcv, Tempv, block_begin, block_end, std::ref(results[i]));
block_begin = block_end;
}
MyThread::Accume22(Srcv, Tempv, block_begin, last, results[num_thread - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&thread::join));
return accumulate(results.begin(), results.end(), sum);
}
};
int main()
{
MyThread Thread;
std::thread thread_test(&MyThread::myThread_in, &Thread, 1000, 9999);
thread_test.join();
int sum = 0;
int Row = 5, Clo = 5;
Mat src;
src = imread("../pp/40/ok/Image_27.bmp");
if (!src.data) { printf("erro"); return false; }
vector<Mat> ceil_img = splitImg(src, Row, Clo);
Mat templ;
templ = imread("../pp/40/ng/Image_62.bmp");
if (!templ.data) { printf("erro"); return false; }
vector<Mat> ceil_img2 = splitImg(templ, Row, Clo);
int num = 0;
IMFaceChk check;
time_begin();
sum = Thread.ParallelAccumed22(ceil_img, ceil_img2, 0, ceil_img.size(), sum);
cout << "time thread all: " << time_end() << ", sum = " << sum << endl;
Mat MergeImage = MergeImg(src, ceil_img, Row, Clo);
namedWindow("mergeSrc", WINDOW_NORMAL);
imshow("mergeSrc", MergeImage);
Mat MergeImage2 = MergeImg(templ, ceil_img2, Row, Clo);
namedWindow("merge2Temp", WINDOW_NORMAL);
imshow("merge2Temp", MergeImage2);
waitKey(0);
return 0;
}