Beispiel #1
0
    gfx::Rect Label::GetTextBounds() const
    {
        gfx::Rect available_rect(GetAvailableRect());
        gfx::Size text_size(GetTextSize());
        text_size.set_width(std::min(available_rect.width(), text_size.width()));

        gfx::Insets insets = GetInsets();
        gfx::Point text_origin(insets.left(), insets.top());
        switch(horiz_alignment_)
        {
        case ALIGN_LEFT:
            break;
        case ALIGN_CENTER:
            // We put any extra margin pixel on the left rather than the right.  We
            // used to do this because measurement on Windows used
            // GetTextExtentPoint32(), which could report a value one too large on the
            // right; we now use DrawText(), and who knows if it can also do this.
            text_origin.Offset((available_rect.width()+1-text_size.width())/2, 0);
            break;
        case ALIGN_RIGHT:
            text_origin.set_x(available_rect.right()-text_size.width());
            break;
        default:
            NOTREACHED();
            break;
        }
        text_origin.Offset(0,
            std::max(0, (available_rect.height()-text_size.height()))/2);
        return gfx::Rect(text_origin, text_size);
    }
void HSVDetector::siftBlobs() {

    cv::Mat thesh_cpy = threshold_image.clone();
    std::vector< std::vector < cv::Point > > contours;
    std::vector< cv::Vec4i > hierarchy;

    // This function will modify the threshold_img data.
    cv::findContours(thesh_cpy, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);

    object_area = 0;
    object_position.position_valid = false;

    if (hierarchy.size() > 0) {

        for (int index = 0; index >= 0; index = hierarchy[index][0]) {

            cv::Moments moment = cv::moments((cv::Mat)contours[index]);
            double area = moment.m00;

            // Isolate the largest contour within the min/max range.
            if (area > min_object_area && area < max_object_area && area > object_area) {
                object_position.position.x = moment.m10 / area;
                object_position.position.y = moment.m01 / area;
                object_position.position_valid = true;
                object_area = area;
            }
        }
    }

    if (tuning_on) {

        std::string msg;
        int baseline = 0;
        cv::Size textSize = cv::getTextSize(msg, 1, 1, 1, &baseline);
        cv::Point text_origin(
                threshold_image.cols - 2 * textSize.width - 10,
                threshold_image.rows - 2 * baseline - 10);

        // Plot a circle representing found object
        if (object_position.position_valid) {
            auto radius = std::sqrt(object_area / PI);
            cv::Point center;
            center.x = object_position.position.x;
            center.y = object_position.position.y;
            cv::circle(threshold_image, center, radius, cv::Scalar(0, 0, 255), 2);
  
            // Tell object position
            if (object_position.world_coords_valid) {
                shmem::Position3D covert_pos = object_position.convertPositionToWorldCoords(object_position.position);
                msg = cv::format("(%d, %d) world units", (int) covert_pos.x, (int) covert_pos.y);
                cv::putText(threshold_image, msg, text_origin, 1, 1, cv::Scalar(0, 255, 0));
            } else {
                msg = cv::format("(%d, %d) pixels", (int) object_position.position.x, (int) object_position.position.y);
                cv::putText(threshold_image, msg, text_origin, 1, 1, cv::Scalar(0, 255, 0));
            }
        } else {
            msg = "Object not found";
            cv::putText(threshold_image, msg, text_origin, 1, 1, cv::Scalar(0, 255, 0));
        }
    }
}