基于OpenCV的视频图像组态 (6): 形状动画效果

形状效果

形状效果:显示目标区域位置不变,显示内容(原始阵不变,屏蔽阵变化 -> 显示内容变化)

 

enum CbwShapeDirection { // 形状方向

    csdZoomIn = 0, // 放大

        csdZoomOut = 1 // 缩小

    };

 

enum CbwShapeType { // 形状类型

    cstCircle = 0, // 圆

        cstRect = 1, // 方框

        cstDiamond = 2, // 菱形

        cstPlus = 3 // 加号

    };

 

bool __fastcall TCbwAnimationEffect_Shape::BuildMaskMat(cv::Mat& destMat,

    cv::Mat& srcMat, TRect displayRect) {

    int zoomType = MyOptionType.Items[1].CurrentValue; // 放大、缩小

    int shapeType = MyOptionType.Items[2].CurrentValue; // 类型

    TRect wholeRect(0, 0, displayRect.right - displayRect.left,

        displayRect.bottom - displayRect.top);

    double cx = wholeRect.right / 2.0, cy = wholeRect.bottom / 2.0;

    double deltaX = double(FCurrentIndex + 1) / FTotalFramesInOnePeriod * cx;

    double deltaY = double(FCurrentIndex + 1) / FTotalFramesInOnePeriod * cy;

    double startX = deltaX, startY = deltaY;

 

    if (zoomType == csdZoomOut) {

        startX = cx - deltaX;

        startY = cy - deltaY;

    }

    BYTE * pSrc = srcMat.data;

    BYTE * pDst = destMat.data;

    for (int row = 0; row < destMat.rows; ++row)

        for (int col = 0; col < destMat.cols; ++col) {

            bool hasValueFlag = (*pSrc++ != 0);

            if (!hasValueFlag)

                * pDst = 0;

            bool inFlag = false;

            double a = (cx - startX) * 1.5, b = (cy - startY) * 1.5;

            if (shapeType == cstCircle) { // 圆

                if (a > 0 && b > 0) {

                    double v = (row - cy) * (row - cy) / (b * b) +

                        (col - cx) * (col - cx) / (a * a);

                    inFlag = (v <= 1);

                }

            }

            if (shapeType == cstRect) { // 方框

                inFlag =

                    (fabs(cx - startX) >= fabs(cx - col) && fabs(cy - startY) >=

                    fabs(cy - row));

            }

            if (shapeType == cstDiamond) { // 菱形

                if (a > 0 && b > 0) {

                    if (zoomType == csdZoomOut) {

                        a *= 2;

                        b *= 2;

                    }

                    bool lr1 = (col < (((-a) * (1 - (row - cy) / (b))) + cx));

                    bool lr2 = (col < (((-a) * (1 - (row - cy) / (-b))) + cx));

                    bool lr3 = (col < (((a) * (1 - (row - cy) / (-b))) + cx));

                    bool lr4 = (col < (((a) * (1 - (row - cy) / (b))) + cx));

                    inFlag = (!lr1 && !lr2 && lr3 && lr4);

                }

            }

            if (shapeType == cstPlus) { // 加号

                inFlag =

                    (fabs(cx - startX) > fabs(cx - col) || fabs(cy - startY) >

                    fabs(cy - row));

            }

            *pDst++ = (inFlag != (zoomType == csdZoomOut) ? 0 : 255);

        }

    return true;

}


 

演示效果

你可能感兴趣的:(OpenCV)