コード例 #1
0
static void drawLineOnCairoContext(GraphicsContext* graphicsContext, cairo_t* context, const FloatPoint& point1, const FloatPoint& point2)
{
    StrokeStyle style = graphicsContext->strokeStyle();
    if (style == NoStroke)
        return;

    const Color& strokeColor = graphicsContext->strokeColor();
    int strokeThickness = floorf(graphicsContext->strokeThickness());
    if (graphicsContext->strokeThickness() < 1)
        strokeThickness = 1;

    int patternWidth = 0;
    if (style == DottedStroke)
        patternWidth = strokeThickness;
    else if (style == DashedStroke)
        patternWidth = 3 * strokeThickness;

    bool isVerticalLine = point1.x() == point2.x();
    FloatPoint point1OnPixelBoundaries = point1;
    FloatPoint point2OnPixelBoundaries = point2;
    GraphicsContext::adjustLineToPixelBoundaries(point1OnPixelBoundaries, point2OnPixelBoundaries, strokeThickness, style);

    cairo_set_antialias(context, CAIRO_ANTIALIAS_NONE);
    if (patternWidth) {
        // Do a rect fill of our endpoints.  This ensures we always have the
        // appearance of being a border.  We then draw the actual dotted/dashed line.
        FloatRect firstRect(point1OnPixelBoundaries, FloatSize(strokeThickness, strokeThickness));
        FloatRect secondRect(point2OnPixelBoundaries, FloatSize(strokeThickness, strokeThickness));
        if (isVerticalLine) {
            firstRect.move(-strokeThickness / 2, -strokeThickness);
            secondRect.move(-strokeThickness / 2, 0);
        } else {
            firstRect.move(-strokeThickness, -strokeThickness / 2);
            secondRect.move(0, -strokeThickness / 2);
        }
        fillRectWithColor(context, firstRect, strokeColor);
        fillRectWithColor(context, secondRect, strokeColor);

        int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2 * strokeThickness;
        double patternOffset = calculateStrokePatternOffset(distance, patternWidth);
        double patternWidthAsDouble = patternWidth;
        cairo_set_dash(context, &patternWidthAsDouble, 1, patternOffset);
    }

    setSourceRGBAFromColor(context, strokeColor);
    cairo_set_line_width(context, strokeThickness);
    cairo_move_to(context, point1OnPixelBoundaries.x(), point1OnPixelBoundaries.y());
    cairo_line_to(context, point2OnPixelBoundaries.x(), point2OnPixelBoundaries.y());
    cairo_stroke(context);
}
コード例 #2
0
void HaarDetectorBody::Process(void)
{
    const Mat& frameIn = imageMessageIn_->Normalized();

    outputFrame_ = imageMessageIn_->Rgb().clone();
    prevObjects_ = objects_;

    if(objects_.empty() || imageMessageIn_->GetMetaData().GetFrameNumber() % 30 == 0)
    {
        const vector<Rect>& rectangles = rectangleMessageIn_->GetRectangles();
        Rect firstRect(0, 0, frameIn.cols, frameIn.rows);

        if(rectangles.size() > 0)
            PartitionateFace(rectangles[0], &firstRect);

        Mat frameInRect = frameIn(firstRect);
        Mat normalizedRes(cvRound(frameInRect.rows * param_->imgScaleFactor), cvRound(frameInRect.cols * param_->imgScaleFactor), CV_8UC1);
        resize(frameInRect, normalizedRes, normalizedRes.size());

        objects_.clear();
        cascade_.detectMultiScale(
            normalizedRes,
            objects_,
            param_->scaleFactor,
            param_->minNeighbors,
            param_->flags,
            param_->minSize,
            param_->maxSize
        );

        if(!objects_.empty())
        {
            for(vector<Rect>::iterator r = objects_.begin(); r != objects_.end(); r++)
            {
                r->x = cvRound(r->x * param_->invImgScaleFactor) + firstRect.x;
                r->y = cvRound(r->y * param_->invImgScaleFactor) + firstRect.y;
                r->width = cvRound(r->width * param_->invImgScaleFactor);
                r->height = cvRound(r->height * param_->invImgScaleFactor);

                rectangle(outputFrame_, *r, Scalar(255.0, 0.0, 0.0), 2);
                putText(outputFrame_, GetFullName(), r->tl(), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255.0, 0.0, 0.0));
            }
        }
    }
    else if(!prevObjects_.empty() && prevImageMessageIn_)
    {
        const Mat& prevFrame = prevImageMessageIn_->Normalized();

        param_->ClearLKVectors();

        for(vector<Rect>::const_iterator r = prevObjects_.begin(); r != prevObjects_.end(); r++)
        {
            param_->prevPoints.push_back(Point(cvRound(r->x + r->width * 0.5), cvRound(r->y + r->height * 0.5)));
            param_->objSizes.push_back(r->size());
        }

        calcOpticalFlowPyrLK(
            prevFrame, frameIn,
            param_->prevPoints, param_->nextPoints,
            param_->status, param_->error,
            param_->winSize,
            param_->maxLevel,
            param_->criteria,
            param_->LKflags,
            param_->minEigThreshold
        );

        const vector<Point2f>& actPoints = (param_->prevPoints.size() == param_->nextPoints.size() ? param_->nextPoints : param_->prevPoints);
        for(size_t i = 0; i < actPoints.size(); i++)
        {
            Point tl(cvRound(actPoints[i].x - param_->objSizes[i].width * 0.5), cvRound(actPoints[i].y - param_->objSizes[i].height * 0.5));
            Point br(cvRound(actPoints[i].x + param_->objSizes[i].width * 0.5), cvRound(actPoints[i].y + param_->objSizes[i].height * 0.5));
            objects_[i] = Rect(tl, br);

            rectangle(outputFrame_, objects_[i], Scalar(0.0, 0.0, 255.0), 2);
            putText(outputFrame_, GetFullName(), tl, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0.0, 0.0, 255.0));
        }
    }

    IMSHOW(GetFullName(), outputFrame_);

    output_ = HasSuccessor() ? new RectangleMessage(objects_) : NULL;
}