【问题标题】:Microsoft Visual Studio C++, OpenCV animationMicrosoft Visual Studio C++、OpenCV 动画
【发布时间】:2015-11-23 23:33:40
【问题描述】:

我有一个问题,我不太确定如何解决它。我想创建一个动画来(流畅地)改变我提取的轮廓的颜色。我假设我所要做的就是使用 while 循环(用于动画),并更改 r、g、b 变量之一的值(使用 for 循环) ,但我不确定如何正确地做到这一点。

提前致谢!

using namespace cv;
using namespace std;
int main()
{
Mat OurImage, img, bin, anim, gray;
string Destination = "rot.jpg";
OurImage = imread(Destination, CV_LOAD_IMAGE_COLOR);

if(! OurImage.data)
{
    printf("No image!");
    getchar();
    return -1;
}
int r = 0, g = 255, b = 255;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
cvtColor(OurImage,gray,CV_RGB2GRAY);
Canny( gray, img, 100, 200,3);
findContours(img,contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
Mat drawing = Mat::zeros(img.size(),CV_8UC3);
for(int i = 0; i < contours.size(); i++)
{
    Scalar color = Scalar(r, g, b);
    drawContours(drawing, contours,i,color,2,8,hierarchy, 0,Point());
}

namedWindow("WINDOW", CV_WINDOW_AUTOSIZE);
while(true)
{

不知道这里的 for 循环应该是什么样子

                printf(".");
                anim = drawing.clone();
                r = r+5;
                imshow("WINDOW",anim);

        if(waitKey(20) == 27) 
        break;
}

waitKey(0);
}

编辑

我已经设法让它从黄色变为白色(从 0 到 255),但现在我不知道如何让它返回并强制程序这样做,直到我点击。我是否使用标志/如果?另一个?

while(true)
{ 
for(int i = 0; i < contours.size(); i++)
        {
        r=r+5;
        printf(".");
        Scalar color = Scalar(r, g, b);     
        drawing = drawing.clone();
        drawContours(drawing, contours,i,color,2,8,hierarchy, 0,Point());
        }
        imshow("WINDOW",drawing);

        if(waitKey(20) == 27) 
        break;
}

【问题讨论】:

  • 请定义:“改变颜色(流畅地)”
  • 从黄色到白色,使用所有黄色阴影(将 r 的值更改 5,从 0 到 255,然后再从 255 到 0)

标签: c++ visual-studio-2010 opencv animation


【解决方案1】:

这段代码会不断地改变边缘的颜色,从白色到黄色,然后再变回来。

您可以使用delay 更改动画速度。

请注意,您可以使用带有边缘掩码的setTo 更改边缘点。如果您需要绘制较粗的边缘,您可以调用 drawContours 和索引 -1 来绘制所有轮廓,无需 for 循环。

请记住,OpenCV Highgui 非常适合调试。除了像这样的程序之外的任何事情都应该使用适当的 GUI 库(如 Qt 等)来完成。

#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;

int main()
{
    // Yellow image
    Mat3b img = imread("path_to_image");

    int step = 5;   // Color step
    int delay = 30; // Animation speed

    bool forward = true;
    Scalar color(0,255,255);

    Mat1b gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);

    Mat1b edges;
    Canny(gray, edges, 400, 200);

    vector<vector<Point>> contours;
    findContours(edges, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);

    Mat3b canvas(img.rows, img.cols, Vec3b(0,0,0));

    while (true)
    {
        imshow("Draw", canvas);
        if (waitKey(delay) == 27 /*ESC*/) break;

        // Update color 
        color[0] = color[0] + ((forward) ? step : -step);

        // Deal with direction
        if (forward && color[0] > 255) {
            forward = false;
            color[0] = 255;
        }
        if (!forward && color[0] < 0) {
            forward = true;
            color[0] = 0;
        }

        // Update only edge points 
        //canvas.setTo(color, edges);

        // Draw a thick contour
        drawContours(canvas, contours, -1, color, 2);
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2012-07-16
    • 2013-01-22
    相关资源
    最近更新 更多