ラベル


```c++:hogehogerennsyuu.cpp
cv::Mat blob_convex_draw(
        cv::Mat binary_img,
        std::vector <std::vector <cv::Point> > contours,
        const Scalar& color,
        int thickness){
    cv::Mat dst_img = cv::Mat::zeros(binary_img.rows, binary_img.cols, CV_8UC3);
    int temp_blob_num = contours.size();
    for (int i=0; i<temp_blob_num; i++){
        cv::Mat temp_pointsf;
        cv::Mat(contours[i]).convertTo(temp_pointsf, CV_32F);
        std::vector<cv::Point2f> temp_hull;
        cv::convexHull(temp_pointsf.reshape(2), temp_hull, false);
        int temp_hnum = temp_hull.size();
        for(int i=0; i<temp_hnum; ++i)
            cv::line(dst_img, temp_hull[i], temp_hull[i+1<temp_hnum?i+1:0], color, thickness, CV_AA);
    }
    return dst_img;
}


cv::Mat simple_convex_draw(
        cv::Mat binary_img,
        std::vector <cv::Point> contours,
        const Scalar& color,
        int thickness){
    cv::Mat dst_img = cv::Mat::zeros(binary_img.rows, binary_img.cols, CV_8UC3);
    cv::Mat temp_pointsf;
    cv::Mat(contours).convertTo(temp_pointsf, CV_32F);
    std::vector<cv::Point2f> temp_hull;
    cv::convexHull(temp_pointsf.reshape(2), temp_hull, false);
    int temp_hnum = temp_hull.size();
    for(int i=0; i<temp_hnum; ++i)
        cv::line(dst_img, temp_hull[i], temp_hull[i+1<temp_hnum?i+1:0], color, thickness, CV_AA);
    return dst_img;
}

cv::Mat simple_circle_draw(
        cv::Mat binary_img,
        std::vector <cv::Point> contours,
        const Scalar& color,
        int thickness){
    cv::Mat dst_img = cv::Mat::zeros(binary_img.rows, binary_img.cols, CV_8UC3);
    cv::Mat temp_pointsf;
    cv::Mat(contours).convertTo(temp_pointsf, CV_32F);//点集合に変換
    cv::Point2f center;
    float radius;
    cv::minEnclosingCircle(temp_pointsf.reshape(2), center, radius);
    cv::circle(dst_img, center, radius, color, thickness, CV_AA);

    return dst_img;
}

class blob
{
public:
    blob();
       std::vector <std::vector <cv::Point> > contours;
       int total_contour_number;
       int found_blob_number;
       std::vector <cv::Point> blob_pts;
       std::vector <double> blob_area;
       std::vector <std::vector <cv::Point> > blob_contours;
       std::vector <cv::Point>  group_contour;
       cv::Point group_contour_pts;
       double group_contour_area;
       void simplelabel(cv::Mat binary_img, int minblob, int maxblob);
};


blob::blob()
{
    contours.clear();
    total_contour_number = 0;
    found_blob_number = 0;
    blob_pts.clear();
    blob_area.clear();
    blob_contours.clear();
    group_contour.clear();
    group_contour_pts = cv::Point (0,0);
    group_contour_area = 0;
}

void blob::simplelabel(cv::Mat binary_img, int min_blob_area, int max_blob_area)
{
    contours.clear();
    blob_contours.clear();
    blob_pts.clear();
    blob_area.clear();
    group_contour.clear();

    cv::Moments contour_moment;
    cv::Point contour_pts;
    double contour_area;
    cv::Mat img = binary_img.clone();

    int temp_blob_number = 0;
    found_blob_number = 0;

    cv::Moments img_m=moments(binary_img, true);
    if ((int)img_m.m00 > 0){
        cv::findContours(img, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
        total_contour_number = contours.size();//見つかった輪郭の数
        if ((int)contours.size() > 0){
            for(int i = 0; i < (int)contours.size(); i++){
                contour_moment = cv::moments(contours[i], true);//輪郭の重心点
                contour_pts =  cv::Point((int)(contour_moment.m10/contour_moment.m00), (int)(contour_moment.m01/contour_moment.m00));
                contour_area = cv::contourArea(contours[i]);
                //size条件を満たすときだけpushback
                if((contour_area > min_blob_area) && (contour_area < max_blob_area)){
                    blob_contours.push_back(contours[i]);
                    blob_pts.push_back(contour_pts);
                    blob_area.push_back(contour_area);
                    temp_blob_number ++ ;
                }
            }

            found_blob_number = temp_blob_number;

//複数のblobが見つかった場合、blob全体の重心点などを求める
            if (found_blob_number > 2){
                std::vector<cv::Point> temp_group_contour;
                temp_group_contour.clear();
                for (int i=0; i < found_blob_number; i++){
                    temp_group_contour.push_back(blob_pts[i]);
                }
                group_contour = temp_group_contour;
                cv::Moments group_contour_moment;
                group_contour_moment = cv::moments(temp_group_contour, true);
                group_contour_pts =  cv::Point(
                            (int)(group_contour_moment.m10/group_contour_moment.m00),
                            (int)(group_contour_moment.m01/group_contour_moment.m00)
                            );
                group_contour_area = cv::contourArea(temp_group_contour);
            }
        }
    }
}