Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 91 additions & 84 deletions jetson/grip/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);
}
/**
Expand All @@ -83,79 +83,86 @@ vector<vector<Point> >* Pipeline::getfindContoursOutput(){
vector<vector<Point> >* 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<vector<Point> > &contours) {
vector<Vec4i> 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<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) {
vector<Point> hull;
output.clear();
for (vector<Point> 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<vector<Point> > &contours) {
vector<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) {
vector<Point> hull;
output.clear();
for (vector<Point> 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);
}
}

40 changes: 22 additions & 18 deletions jetson/grip/Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include <opencv2/core/core.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/gpu/gpu.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <math.h>

using namespace cv;
using namespace std;

Expand All @@ -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<vector<Point> > findContoursOutput;
void resizeImage(Mat &, double , double , int , Mat &);
void hsvThreshold(Mat &, double [], double [], double [], Mat &);
void findContours(Mat &, bool , vector<vector<Point> > &);
void filterContours(vector<vector<Point> > &, double , double , double , double , double , double , double [], double , double , double , double , vector<vector<Point> > &);
private:
cv::cuda::GpuMat source0;
cv::cuda::GpuMat resizeImageOutput;
cv::cuda::GpuMat hsvThresholdOutput;
vector<vector<Point> > 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<vector<Point> > &);
void filterContours(vector<vector<Point> > &, double , double , double , double , double , double , double [], double , double , double , double , vector<vector<Point> > &);

public:
vector<vector<Point> > filterContoursOutput;
Pipeline();
void Process();
void setsource0(Mat &source0);
Mat* getresizeImageOutput();
Mat* gethsvThresholdOutput();
vector<vector<Point> >* getfindContoursOutput();
vector<vector<Point> >* getfilterContoursOutput();
public:
vector<vector<Point> > filterContoursOutput;
Pipeline();
void Process(bool);
void setsource0(cv::cuda::GpuMat &source0);
cv::cuda::GpuMat* getresizeImageOutput();
cv::cuda::GpuMat* gethsvThresholdOutput();
vector<vector<Point> >* getfindContoursOutput();
vector<vector<Point> >* getfilterContoursOutput();
};