Created
December 8, 2016 22:04
-
-
Save phurley/aaba72df124f8b3e3b2fd87e40fd2c92 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Pipeline.h" | |
/** | |
* Initializes a Pipeline. | |
*/ | |
namespace grip { | |
Pipeline::Pipeline() { | |
} | |
/** | |
* Runs an iteration of the Pipeline and updates outputs. | |
* | |
* Sources need to be set before calling this method. | |
* | |
*/ | |
void Pipeline::Process(){ | |
//Step Blur0: | |
//input | |
cv::Mat blurInput = source0; | |
BlurType blurType = BlurType::GAUSSIAN; | |
double blurRadius = 1.8018018018018018; // default Double | |
blur(blurInput, blurType, blurRadius, this->blurOutput); | |
//Step HSL_Threshold0: | |
//input | |
cv::Mat hslThresholdInput = blurOutput; | |
double hslThresholdHue[] = {51.798561151079134, 99.01528013582343}; | |
double hslThresholdSaturation[] = {68.79496402877697, 255.0}; | |
double hslThresholdLuminance[] = {36.690647482014384, 166.2478777589134}; | |
hslThreshold(hslThresholdInput, hslThresholdHue, hslThresholdSaturation, hslThresholdLuminance, this->hslThresholdOutput); | |
//Step Find_Contours0: | |
//input | |
cv::Mat findContoursInput = hslThresholdOutput; | |
bool findContoursExternalOnly = false; // default Boolean | |
findContours(findContoursInput, findContoursExternalOnly, this->findContoursOutput); | |
//Step Filter_Contours0: | |
//input | |
vector<vector<Point> > filterContoursContours = findContoursOutput; | |
double filterContoursMinArea = 600.0; // default Double | |
double filterContoursMinPerimeter = 0.0; // default Double | |
double filterContoursMinWidth = 0.0; // default Double | |
double filterContoursMaxWidth = 1000.0; // default Double | |
double filterContoursMinHeight = 0.0; // default Double | |
double filterContoursMaxHeight = 1000.0; // default Double | |
double filterContoursSolidity[] = {0, 100}; | |
double filterContoursMaxVertices = 1000000.0; // default Double | |
double filterContoursMinVertices = 0.0; // default Double | |
double filterContoursMinRatio = 0.0; // default Double | |
double filterContoursMaxRatio = 1000.0; // default Double | |
filterContours(filterContoursContours, filterContoursMinArea, filterContoursMinPerimeter, filterContoursMinWidth, filterContoursMaxWidth, filterContoursMinHeight, filterContoursMaxHeight, filterContoursSolidity, filterContoursMaxVertices, filterContoursMinVertices, filterContoursMinRatio, filterContoursMaxRatio, this->filterContoursOutput); | |
//Step Convex_Hulls0: | |
//input | |
vector<vector<Point> > convexHullsContours = filterContoursOutput; | |
convexHulls(convexHullsContours, this->convexHullsOutput); | |
} | |
/** | |
* This method is a generated setter for source0. | |
* @param source the Mat to set | |
*/ | |
void Pipeline::setsource0(cv::Mat &source0){ | |
source0.copyTo(this->source0); | |
} | |
/** | |
* This method is a generated getter for the output of a Blur. | |
* @return Mat output from Blur. | |
*/ | |
cv::Mat* Pipeline::getblurOutput(){ | |
return &(this->blurOutput); | |
} | |
/** | |
* This method is a generated getter for the output of a HSL_Threshold. | |
* @return Mat output from HSL_Threshold. | |
*/ | |
cv::Mat* Pipeline::gethslThresholdOutput(){ | |
return &(this->hslThresholdOutput); | |
} | |
/** | |
* This method is a generated getter for the output of a Find_Contours. | |
* @return ContoursReport output from Find_Contours. | |
*/ | |
vector<vector<Point> >* Pipeline::getfindContoursOutput(){ | |
return &(this->findContoursOutput); | |
} | |
/** | |
* This method is a generated getter for the output of a Filter_Contours. | |
* @return ContoursReport output from Filter_Contours. | |
*/ | |
vector<vector<Point> >* Pipeline::getfilterContoursOutput(){ | |
return &(this->filterContoursOutput); | |
} | |
/** | |
* This method is a generated getter for the output of a Convex_Hulls. | |
* @return ContoursReport output from Convex_Hulls. | |
*/ | |
vector<vector<Point> >* Pipeline::getconvexHullsOutput(){ | |
return &(this->convexHullsOutput); | |
} | |
/** | |
* Softens an image using one of several filters. | |
* | |
* @param input The image on which to perform the blur. | |
* @param type The blurType to perform. | |
* @param doubleRadius The radius for the blur. | |
* @param output The image in which to store the output. | |
*/ | |
void Pipeline::blur(cv::Mat &input, BlurType &type, double doubleRadius, cv::Mat &output) { | |
int radius = (int)(doubleRadius + 0.5); | |
int kernelSize; | |
switch(type) { | |
case BOX: | |
kernelSize = 2 * radius + 1; | |
cv::blur(input,output,cv::Size(kernelSize, kernelSize)); | |
break; | |
case GAUSSIAN: | |
kernelSize = 6 * radius + 1; | |
cv::GaussianBlur(input, output, cv::Size(kernelSize, kernelSize), radius); | |
break; | |
case MEDIAN: | |
kernelSize = 2 * radius + 1; | |
cv::medianBlur(input, output, kernelSize); | |
break; | |
case BILATERAL: | |
cv::bilateralFilter(input, output, -1, radius, radius); | |
break; | |
} | |
} | |
/** | |
* Segment an image based on hue, saturation, and luminance 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 lum The min and max luminance. | |
* @param output The image in which to store the output. | |
*/ | |
//void hslThreshold(Mat *input, double hue[], double sat[], double lum[], Mat *out) { | |
void Pipeline::hslThreshold(cv::Mat &input, double hue[], double sat[], double lum[], cv::Mat &out) { | |
cv::cvtColor(input, out, cv::COLOR_BGR2HLS); | |
cv::inRange(out, cv::Scalar(hue[0], lum[0], sat[0]), cv::Scalar(hue[1], lum[1], sat[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(cv::Mat &input, bool externalOnly, vector<vector<Point> > &contours) { | |
std::vector<cv::Vec4i> 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<vector<Point> > &inputContours, double minArea, double minPerimeter, double minWidth, double maxWidth, double minHeight, double maxHeight, double solidity[], double maxVertexCount, double minVertexCount, double minRatio, double maxRatio, vector<vector<Point> > &output) { | |
std::vector<cv::Point> hull; | |
output.clear(); | |
for (std::vector<Point> contour: inputContours) { | |
cv::Rect bb = boundingRect(contour); | |
if (bb.width < minWidth || bb.width > maxWidth) continue; | |
if (bb.height < minHeight || bb.height > maxHeight) continue; | |
double area = cv::contourArea(contour); | |
if (area < minArea) continue; | |
if (arcLength(contour, true) < minPerimeter) continue; | |
cv::convexHull(Mat(contour, true), hull); | |
double solid = 100 * area / cv::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); | |
} | |
} | |
/** | |
* Compute the convex hulls of contours. | |
* | |
* @param inputContours The contours on which to perform the operation. | |
* @param outputContours The contours where the output will be stored. | |
*/ | |
void Pipeline::convexHulls(vector<vector<Point> > &inputContours, vector<vector<Point> > &outputContours) { | |
std::vector<vector<cv::Point> > hull (inputContours.size()); | |
outputContours.clear(); | |
for (int i = 0; i < inputContours.size(); i++ ) { | |
cv::convexHull(cv::Mat((inputContours)[i]), hull[i], false); | |
} | |
outputContours = hull; | |
} | |
} // end grip namespace | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment