Advertisement
DocNumFo

agh

Feb 5th, 2018
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.54 KB | None | 0 0
  1. header:
  2. #pragma once
  3. #include <opencv2/objdetect/objdetect.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <opencv2/imgproc/imgproc.hpp>
  6. #include <opencv2/core/core.hpp>
  7. #include <opencv2/features2d.hpp>
  8. #include <iostream>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <map>
  12. #include <vector>
  13. #include <string>
  14. #include <math.h>
  15.  
  16. namespace grip {
  17.  
  18. /**
  19. * A representation of the different types of blurs that can be used.
  20. *
  21. */
  22. enum BlurType {
  23.     BOX, GAUSSIAN, MEDIAN, BILATERAL
  24. };
  25. /**
  26. * GellowRect class.
  27. *
  28. * An OpenCV pipeline generated by GRIP.
  29. */
  30. class GellowRect {
  31.     private:
  32.         cv::Mat hsvThresholdOutput;
  33.         cv::Mat maskOutput;
  34.         cv::Mat blurOutput;
  35.         std::vector<std::vector<cv::Point> > findContoursOutput;
  36.         std::vector<std::vector<cv::Point> > filterContoursOutput;
  37.         std::vector<std::vector<cv::Point> > convexHullsOutput;
  38.         void hsvThreshold(cv::Mat &, double [], double [], double [], cv::Mat &);
  39.         void mask(cv::Mat &, cv::Mat &, cv::Mat &);
  40.         void blur(cv::Mat &, BlurType &, double , cv::Mat &);
  41.         void findContours(cv::Mat &, bool , std::vector<std::vector<cv::Point> > &);
  42.         void filterContours(std::vector<std::vector<cv::Point> > &, double , double , double , double , double , double , double [], double , double , double , double , std::vector<std::vector<cv::Point> > &);
  43.         void convexHulls(std::vector<std::vector<cv::Point> > &, std::vector<std::vector<cv::Point> > &);
  44.  
  45.     public:
  46.         GellowRect();
  47.         void Process(cv::Mat& source0);
  48.         cv::Mat* GetHsvThresholdOutput();
  49.         cv::Mat* GetMaskOutput();
  50.         cv::Mat* GetBlurOutput();
  51.         std::vector<std::vector<cv::Point> >* GetFindContoursOutput();
  52.         std::vector<std::vector<cv::Point> >* GetFilterContoursOutput();
  53.         std::vector<std::vector<cv::Point> >* GetConvexHullsOutput();
  54. };
  55.  
  56.  
  57. } // end namespace grip
  58.  
  59.  
  60. cpp file:
  61. #include "GellowRect.h"
  62.  
  63. namespace grip {
  64.  
  65. GellowRect::GellowRect() {
  66. }
  67. /**
  68. * Runs an iteration of the pipeline and updates outputs.
  69. */
  70. void GellowRect::Process(cv::Mat& source0){
  71.     //Step HSV_Threshold0:
  72.     //input
  73.     cv::Mat hsvThresholdInput = source0;
  74.     double hsvThresholdHue[] = {24.280575539568343, 38.703071672354945};
  75.     double hsvThresholdSaturation[] = {87.14028776978417, 255.0};
  76.     double hsvThresholdValue[] = {119.24460431654676, 255.0};
  77.     hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, this->hsvThresholdOutput);
  78.     //Step Mask0:
  79.     //input
  80.     cv::Mat maskInput = source0;
  81.     cv::Mat maskMask = hsvThresholdOutput;
  82.     mask(maskInput, maskMask, this->maskOutput);
  83.     //Step Blur0:
  84.     //input
  85.     cv::Mat blurInput = hsvThresholdOutput;
  86.     BlurType blurType = BlurType::MEDIAN;
  87.     double blurRadius = 8.108108108108109;  // default Double
  88.     blur(blurInput, blurType, blurRadius, this->blurOutput);
  89.     //Step Find_Contours0:
  90.     //input
  91.     cv::Mat findContoursInput = blurOutput;
  92.     bool findContoursExternalOnly = false;  // default Boolean
  93.     findContours(findContoursInput, findContoursExternalOnly, this->findContoursOutput);
  94.     //Step Filter_Contours0:
  95.     //input
  96.     std::vector<std::vector<cv::Point> > filterContoursContours = findContoursOutput;
  97.     double filterContoursMinArea = 1000.0;  // default Double
  98.     double filterContoursMinPerimeter = 0.0;  // default Double
  99.     double filterContoursMinWidth = 0.0;  // default Double
  100.     double filterContoursMaxWidth = 1000.0;  // default Double
  101.     double filterContoursMinHeight = 0.0;  // default Double
  102.     double filterContoursMaxHeight = 1000.0;  // default Double
  103.     double filterContoursSolidity[] = {75.31760435571687, 100};
  104.     double filterContoursMaxVertices = 1000000.0;  // default Double
  105.     double filterContoursMinVertices = 0.0;  // default Double
  106.     double filterContoursMinRatio = 0.0;  // default Double
  107.     double filterContoursMaxRatio = 1000.0;  // default Double
  108.     filterContours(filterContoursContours, filterContoursMinArea, filterContoursMinPerimeter, filterContoursMinWidth, filterContoursMaxWidth, filterContoursMinHeight, filterContoursMaxHeight, filterContoursSolidity, filterContoursMaxVertices, filterContoursMinVertices, filterContoursMinRatio, filterContoursMaxRatio, this->filterContoursOutput);
  109.     //Step Convex_Hulls0:
  110.     //input
  111.     std::vector<std::vector<cv::Point> > convexHullsContours = filterContoursOutput;
  112.     convexHulls(convexHullsContours, this->convexHullsOutput);
  113. }
  114.  
  115. /**
  116.  * This method is a generated getter for the output of a HSV_Threshold.
  117.  * @return Mat output from HSV_Threshold.
  118.  */
  119. cv::Mat* GellowRect::GetHsvThresholdOutput(){
  120.     return &(this->hsvThresholdOutput);
  121. }
  122. /**
  123.  * This method is a generated getter for the output of a Mask.
  124.  * @return Mat output from Mask.
  125.  */
  126. cv::Mat* GellowRect::GetMaskOutput(){
  127.     return &(this->maskOutput);
  128. }
  129. /**
  130.  * This method is a generated getter for the output of a Blur.
  131.  * @return Mat output from Blur.
  132.  */
  133. cv::Mat* GellowRect::GetBlurOutput(){
  134.     return &(this->blurOutput);
  135. }
  136. /**
  137.  * This method is a generated getter for the output of a Find_Contours.
  138.  * @return ContoursReport output from Find_Contours.
  139.  */
  140. std::vector<std::vector<cv::Point> >* GellowRect::GetFindContoursOutput(){
  141.     return &(this->findContoursOutput);
  142. }
  143. /**
  144.  * This method is a generated getter for the output of a Filter_Contours.
  145.  * @return ContoursReport output from Filter_Contours.
  146.  */
  147. std::vector<std::vector<cv::Point> >* GellowRect::GetFilterContoursOutput(){
  148.     return &(this->filterContoursOutput);
  149. }
  150. /**
  151.  * This method is a generated getter for the output of a Convex_Hulls.
  152.  * @return ContoursReport output from Convex_Hulls.
  153.  */
  154. std::vector<std::vector<cv::Point> >* GellowRect::GetConvexHullsOutput(){
  155.     return &(this->convexHullsOutput);
  156. }
  157.     /**
  158.      * Segment an image based on hue, saturation, and value ranges.
  159.      *
  160.      * @param input The image on which to perform the HSL threshold.
  161.      * @param hue The min and max hue.
  162.      * @param sat The min and max saturation.
  163.      * @param val The min and max value.
  164.      * @param output The image in which to store the output.
  165.      */
  166.     void GellowRect::hsvThreshold(cv::Mat &input, double hue[], double sat[], double val[], cv::Mat &out) {
  167.         cv::cvtColor(input, out, cv::COLOR_BGR2HSV);
  168.         cv::inRange(out,cv::Scalar(hue[0], sat[0], val[0]), cv::Scalar(hue[1], sat[1], val[1]), out);
  169.     }
  170.  
  171.         /**
  172.          * Filter out an area of an image using a binary mask.
  173.          *
  174.          * @param input The image on which the mask filters.
  175.          * @param mask The binary image that is used to filter.
  176.          * @param output The image in which to store the output.
  177.          */
  178.         void GellowRect::mask(cv::Mat &input, cv::Mat &mask, cv::Mat &output) {
  179.             mask.convertTo(mask, CV_8UC1);
  180.             cv::bitwise_xor(output, output, output);
  181.             input.copyTo(output, mask);
  182.         }
  183.  
  184.     /**
  185.      * Softens an image using one of several filters.
  186.      *
  187.      * @param input The image on which to perform the blur.
  188.      * @param type The blurType to perform.
  189.      * @param doubleRadius The radius for the blur.
  190.      * @param output The image in which to store the output.
  191.      */
  192.     void GellowRect::blur(cv::Mat &input, BlurType &type, double doubleRadius, cv::Mat &output) {
  193.         int radius = (int)(doubleRadius + 0.5);
  194.         int kernelSize;
  195.         switch(type) {
  196.             case BOX:
  197.                 kernelSize = 2 * radius + 1;
  198.                 cv::blur(input,output,cv::Size(kernelSize, kernelSize));
  199.                 break;
  200.             case GAUSSIAN:
  201.                 kernelSize = 6 * radius + 1;
  202.                 cv::GaussianBlur(input, output, cv::Size(kernelSize, kernelSize), radius);
  203.                 break;
  204.             case MEDIAN:
  205.                 kernelSize = 2 * radius + 1;
  206.                 cv::medianBlur(input, output, kernelSize);
  207.                 break;
  208.             case BILATERAL:
  209.                 cv::bilateralFilter(input, output, -1, radius, radius);
  210.                 break;
  211.         }
  212.     }
  213.     /**
  214.      * Finds contours in an image.
  215.      *
  216.      * @param input The image to find contours in.
  217.      * @param externalOnly if only external contours are to be found.
  218.      * @param contours vector of contours to put contours in.
  219.      */
  220.     void GellowRect::findContours(cv::Mat &input, bool externalOnly, std::vector<std::vector<cv::Point> > &contours) {
  221.         std::vector<cv::Vec4i> hierarchy;
  222.         contours.clear();
  223.         int mode = externalOnly ? cv::RETR_EXTERNAL : cv::RETR_LIST;
  224.         int method = cv::CHAIN_APPROX_SIMPLE;
  225.         cv::findContours(input, contours, hierarchy, mode, method);
  226.     }
  227.  
  228.  
  229.     /**
  230.      * Filters through contours.
  231.      * @param inputContours is the input vector of contours.
  232.      * @param minArea is the minimum area of a contour that will be kept.
  233.      * @param minPerimeter is the minimum perimeter of a contour that will be kept.
  234.      * @param minWidth minimum width of a contour.
  235.      * @param maxWidth maximum width.
  236.      * @param minHeight minimum height.
  237.      * @param maxHeight  maximimum height.
  238.      * @param solidity the minimum and maximum solidity of a contour.
  239.      * @param minVertexCount minimum vertex Count of the contours.
  240.      * @param maxVertexCount maximum vertex Count.
  241.      * @param minRatio minimum ratio of width to height.
  242.      * @param maxRatio maximum ratio of width to height.
  243.      * @param output vector of filtered contours.
  244.      */
  245.     void GellowRect::filterContours(std::vector<std::vector<cv::Point> > &inputContours, double minArea, double minPerimeter, double minWidth, double maxWidth, double minHeight, double maxHeight, double solidity[], double maxVertexCount, double minVertexCount, double minRatio, double maxRatio, std::vector<std::vector<cv::Point> > &output) {
  246.         std::vector<cv::Point> hull;
  247.         output.clear();
  248.         for (std::vector<cv::Point> contour: inputContours) {
  249.             cv::Rect bb = boundingRect(contour);
  250.             if (bb.width < minWidth || bb.width > maxWidth) continue;
  251.             if (bb.height < minHeight || bb.height > maxHeight) continue;
  252.             double area = cv::contourArea(contour);
  253.             if (area < minArea) continue;
  254.             if (arcLength(contour, true) < minPerimeter) continue;
  255.             cv::convexHull(cv::Mat(contour, true), hull);
  256.             double solid = 100 * area / cv::contourArea(hull);
  257.             if (solid < solidity[0] || solid > solidity[1]) continue;
  258.             if (contour.size() < minVertexCount || contour.size() > maxVertexCount) continue;
  259.             double ratio = (double) bb.width / (double) bb.height;
  260.             if (ratio < minRatio || ratio > maxRatio) continue;
  261.             output.push_back(contour);
  262.         }
  263.     }
  264.  
  265.     /**
  266.      * Compute the convex hulls of contours.
  267.      *
  268.      * @param inputContours The contours on which to perform the operation.
  269.      * @param outputContours The contours where the output will be stored.
  270.      */
  271.     void GellowRect::convexHulls(std::vector<std::vector<cv::Point> > &inputContours, std::vector<std::vector<cv::Point> > &outputContours) {
  272.         std::vector<std::vector<cv::Point> > hull (inputContours.size());
  273.         outputContours.clear();
  274.         for (size_t i = 0; i < inputContours.size(); i++ ) {
  275.             cv::convexHull(cv::Mat((inputContours)[i]), hull[i], false);
  276.         }
  277.         outputContours = hull;
  278.     }
  279.  
  280.  
  281.  
  282. } // end grip namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement