From 3f597fc0b115747fca777e0117a81d1a6de293fd Mon Sep 17 00:00:00 2001 From: ja-San Date: Wed, 1 Feb 2017 15:53:44 -0500 Subject: [PATCH] PLEASE READ COMMIT. Added GPU acceleration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unable to test if GPU parts work because I don’t have access to GPU module library. It needs to be compiled on a computer that has it --- jetson/grip/Pipeline.cpp | 175 ++++++++++++++++++++------------------- jetson/grip/Pipeline.h | 40 +++++---- 2 files changed, 113 insertions(+), 102 deletions(-) diff --git a/jetson/grip/Pipeline.cpp b/jetson/grip/Pipeline.cpp index 07ef941..b3f4f02 100644 --- a/jetson/grip/Pipeline.cpp +++ b/jetson/grip/Pipeline.cpp @@ -11,24 +11,24 @@ Pipeline::Pipeline() { * Sources need to be set before calling this method. * */ -void Pipeline::Process(){ +void Pipeline::Process(bool onGPU){ //Step Resize_Image0: //input - Mat resizeImageInput = source0; + cv::cuda::GpuMat resizeImageInput = source0; double resizeImageWidth = 320.0; double resizeImageHeight = 240.0; int resizeImageInterpolation = INTER_CUBIC; resizeImage(resizeImageInput, resizeImageWidth, resizeImageHeight, resizeImageInterpolation, this->resizeImageOutput); //Step HSV_Threshold0: //input - Mat hsvThresholdInput = resizeImageOutput; + cv::cuda::GpuMat hsvThresholdInput = resizeImageOutput; double hsvThresholdHue[] = {41.62385249309404, 92.23793941146276}; double hsvThresholdSaturation[] = {0.0, 255.0}; double hsvThresholdValue[] = {222.76464527030643, 255.0}; hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, this->hsvThresholdOutput); //Step Find_Contours0: //input - Mat findContoursInput = hsvThresholdOutput; + cv::cuda::GpuMat findContoursInput = hsvThresholdOutput; bool findContoursExternalOnly = false; findContours(findContoursInput, findContoursExternalOnly, this->findContoursOutput); //Step Filter_Contours0: @@ -50,23 +50,23 @@ void Pipeline::Process(){ /** * This method is a generated setter for source0. - * @param source the Mat to set + * @param source the cv::cuda::GpuMat to set */ -void Pipeline::setsource0(Mat &source0){ +void Pipeline::setsource0(cv::cuda::GpuMat &source0){ source0.copyTo(this->source0); } /** * This method is a generated getter for the output of a Resize_Image. - * @return Mat output from Resize_Image. + * @return cv::cuda::GpuMat output from Resize_Image. */ -Mat* Pipeline::getresizeImageOutput(){ +cv::cuda::GpuMat* Pipeline::getresizeImageOutput(){ return &(this->resizeImageOutput); } /** * This method is a generated getter for the output of a HSV_Threshold. - * @return Mat output from HSV_Threshold. + * @return cv::cuda::GpuMat output from HSV_Threshold. */ -Mat* Pipeline::gethsvThresholdOutput(){ +cv::cuda::GpuMat* Pipeline::gethsvThresholdOutput(){ return &(this->hsvThresholdOutput); } /** @@ -83,79 +83,86 @@ vector >* Pipeline::getfindContoursOutput(){ vector >* Pipeline::getfilterContoursOutput(){ return &(this->filterContoursOutput); } - /** - * Scales and image to an exact size. - * - * @param input The image on which to perform the Resize. - * @param width The width of the output in pixels. - * @param height The height of the output in pixels. - * @param interpolation The type of interpolation. - * @param output The image in which to store the output. - */ - void Pipeline::resizeImage(Mat &input, double width, double height, int interpolation, Mat &output) { - resize(input, output, Size(width, height), 0.0, 0.0, interpolation); - } - /** - * Segment an image based on hue, saturation, and value ranges. - * - * @param input The image on which to perform the HSL threshold. - * @param hue The min and max hue. - * @param sat The min and max saturation. - * @param val The min and max value. - * @param output The image in which to store the output. - */ - void Pipeline::hsvThreshold(Mat &input, double hue[], double sat[], double val[], Mat &out) { - cvtColor(input, out, COLOR_BGR2HSV); - inRange(out,Scalar(hue[0], sat[0], val[0]), Scalar(hue[1], sat[1], val[1]), out); - } - /** - * Finds contours in an image. - * - * @param input The image to find contours in. - * @param externalOnly if only external contours are to be found. - * @param contours vector of contours to put contours in. - */ - void Pipeline::findContours(Mat &input, bool externalOnly, vector > &contours) { - vector hierarchy; - contours.clear(); - int mode = externalOnly ? RETR_EXTERNAL : RETR_LIST; - int method = CHAIN_APPROX_SIMPLE; - cv::findContours(input, contours, hierarchy, mode, method); - } +/** + * Scales and image to an exact size. + * + * @param input The image on which to perform the Resize. + * @param width The width of the output in pixels. + * @param height The height of the output in pixels. + * @param interpolation The type of interpolation. + * @param output The image in which to store the output. + */ +//void Pipeline::resizeImage(Mat &input, double width, double height, int interpolation, Mat &output) { +// resize(input, output, Size(width, height), 0.0, 0.0, interpolation); +//} +void Pipeline::resizeImage(const cv::cuda::GpuMat &input, double width, double height, int interpolation, cv::cuda::GpuMat &output) { + gpu::resize(input, output, Size(width, height), 0.0, 0.0, interpolation); +} - /** - * Filters through contours. - * @param inputContours is the input vector of contours. - * @param minArea is the minimum area of a contour that will be kept. - * @param minPerimeter is the minimum perimeter of a contour that will be kept. - * @param minWidth minimum width of a contour. - * @param maxWidth maximum width. - * @param minHeight minimum height. - * @param maxHeight maximimum height. - * @param solidity the minimum and maximum solidity of a contour. - * @param minVertexCount minimum vertex Count of the contours. - * @param maxVertexCount maximum vertex Count. - * @param minRatio minimum ratio of width to height. - * @param maxRatio maximum ratio of width to height. - * @param output vector of filtered contours. - */ - void Pipeline::filterContours(vector > &inputContours, double minArea, double minPerimeter, double minWidth, double maxWidth, double minHeight, double maxHeight, double solidity[], double maxVertexCount, double minVertexCount, double minRatio, double maxRatio, vector > &output) { - vector hull; - output.clear(); - for (vector contour: inputContours) { - Rect bb = boundingRect(contour); - if (bb.width < minWidth || bb.width > maxWidth) continue; - if (bb.height < minHeight || bb.height > maxHeight) continue; - double area = contourArea(contour); - if (area < minArea) continue; - if (arcLength(contour, true) < minPerimeter) continue; - convexHull(Mat(contour, true), hull); - double solid = 100 * area / contourArea(hull); - if (solid < solidity[0] || solid > solidity[1]) continue; - if (contour.size() < minVertexCount || contour.size() > maxVertexCount) continue; - double ratio = bb.width / bb.height; - if (ratio < minRatio || ratio > maxRatio) continue; - output.push_back(contour); - } - } +/** + * Segment an image based on hue, saturation, and value ranges. + * + * @param input The image on which to perform the HSL threshold. + * @param hue The min and max hue. + * @param sat The min and max saturation. + * @param val The min and max value. + * @param output The image in which to store the output. + */ +//void Pipeline::hsvThreshold(Mat &input, double hue[], double sat[], double val[], Mat &out) { +// cvtColor(input, out, COLOR_BGR2HSV); +// inRange(out,Scalar(hue[0], sat[0], val[0]), Scalar(hue[1], sat[1], val[1]), out); +//} +void Pipeline::hsvThreshold(const cv::cuda::GMat &input, double hue[], double sat[], double val[], cv::cuda::Mat &output) { + gpu::cvtColor(input, output, COLOR_BGR2HSV); + inRange(output, Scalar(hue[0], sat[0], val[0]), Scalar(hue[1], sat[1], val[1]), output); +} +/** + * Finds contours in an image. + * + * @param input The image to find contours in. + * @param externalOnly if only external contours are to be found. + * @param contours vector of contours to put contours in. + */ +void Pipeline::findContours(cv::cuda::GpuMat &input, bool externalOnly, vector > &contours) { + vector hierarchy; + contours.clear(); + int mode = externalOnly ? RETR_EXTERNAL : RETR_LIST; + int method = CHAIN_APPROX_SIMPLE; + cv::findContours(input, contours, hierarchy, mode, method); +} +/** + * Filters through contours. + * @param inputContours is the input vector of contours. + * @param minArea is the minimum area of a contour that will be kept. + * @param minPerimeter is the minimum perimeter of a contour that will be kept. + * @param minWidth minimum width of a contour. + * @param maxWidth maximum width. + * @param minHeight minimum height. + * @param maxHeight maximimum height. + * @param solidity the minimum and maximum solidity of a contour. + * @param minVertexCount minimum vertex Count of the contours. + * @param maxVertexCount maximum vertex Count. + * @param minRatio minimum ratio of width to height. + * @param maxRatio maximum ratio of width to height. + * @param output vector of filtered contours. + */ +void Pipeline::filterContours(vector > &inputContours, double minArea, double minPerimeter, double minWidth, double maxWidth, double minHeight, double maxHeight, double solidity[], double maxVertexCount, double minVertexCount, double minRatio, double maxRatio, vector > &output) { + vector hull; + output.clear(); + for (vector contour: inputContours) { + Rect bb = boundingRect(contour); + if (bb.width < minWidth || bb.width > maxWidth) continue; + if (bb.height < minHeight || bb.height > maxHeight) continue; + double area = contourArea(contour); + if (area < minArea) continue; + if (arcLength(contour, true) < minPerimeter) continue; + convexHull(cv::cuda::GpuMat(contour, true), hull); + double solid = 100 * area / contourArea(hull); + if (solid < solidity[0] || solid > solidity[1]) continue; + if (contour.size() < minVertexCount || contour.size() > maxVertexCount) continue; + double ratio = bb.width / bb.height; + if (ratio < minRatio || ratio > maxRatio) continue; + output.push_back(contour); + } +} diff --git a/jetson/grip/Pipeline.h b/jetson/grip/Pipeline.h index 4eb582d..9014ca7 100644 --- a/jetson/grip/Pipeline.h +++ b/jetson/grip/Pipeline.h @@ -5,11 +5,13 @@ #include #include #include +#include #include #include #include #include #include + using namespace cv; using namespace std; @@ -21,24 +23,26 @@ using namespace std; * Make sure to set sources before running process() */ class Pipeline { - private: - Mat source0; - Mat resizeImageOutput; - Mat hsvThresholdOutput; - vector > findContoursOutput; - void resizeImage(Mat &, double , double , int , Mat &); - void hsvThreshold(Mat &, double [], double [], double [], Mat &); - void findContours(Mat &, bool , vector > &); - void filterContours(vector > &, double , double , double , double , double , double , double [], double , double , double , double , vector > &); +private: + cv::cuda::GpuMat source0; + cv::cuda::GpuMat resizeImageOutput; + cv::cuda::GpuMat hsvThresholdOutput; + vector > findContoursOutput; +// void resizeImage(Mat &, double , double , int , Mat &); + void resizeImage(const cv::cuda::GpuMat &, double, double, int, cv::cuda::GpuMat &); +// void hsvThreshold(Mat &, double [], double [], double [], Mat &); + void hsvThreshold(const cv::cuda::GpuMat &, double [], double [], double [], cv::cuda::GpuMat &); + void findContours(cv::cuda::GpuMat &, bool , vector > &); + void filterContours(vector > &, double , double , double , double , double , double , double [], double , double , double , double , vector > &); - public: - vector > filterContoursOutput; - Pipeline(); - void Process(); - void setsource0(Mat &source0); - Mat* getresizeImageOutput(); - Mat* gethsvThresholdOutput(); - vector >* getfindContoursOutput(); - vector >* getfilterContoursOutput(); +public: + vector > filterContoursOutput; + Pipeline(); + void Process(bool); + void setsource0(cv::cuda::GpuMat &source0); + cv::cuda::GpuMat* getresizeImageOutput(); + cv::cuda::GpuMat* gethsvThresholdOutput(); + vector >* getfindContoursOutput(); + vector >* getfilterContoursOutput(); };