检测闪烁灯的顺序

2024-10-05 10:13:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找一个例子或只是一个起点来实现以下目标:

使用PythonopenCV我想检测闪烁灯光的序列。i、 e.on off on off=匹配

这有可能吗?有人可以先给我举个简单的例子吗。我希望能从中学到东西。我通过示例学习得更好,但找不到任何实现排序功能的方法。在


Tags: 方法示例目标on序列例子起点灯光
1条回答
网友
1楼 · 发布于 2024-10-05 10:13:03

如果光源在图像中非常突出,可以使用图像的平均强度来检测变化。在

这是一个非常简单的例子。我使用this视频进行测试。 您可能需要调整视频的阈值。在

如果你的视频不像我用来测试的那样简单,你可能需要做一些调整。例如,如果图像的其他部分有太多分散注意力的情况,可以先尝试分割光源。或者如果连续图像之间的强度变化不够大,您可能需要查看多个图像的变化。在

编辑:

我刚才看到的问题是用Python标记的,但是我的源代码是C++。但我暂时不谈了。也许这有助于您了解基本概念,以便您自己将其移植到python。在

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;

int main(int argc, char** argv)
{
    VideoCapture capture(argv[1]);
    Mat frame;

    if( !capture.isOpened() )
        throw "Error when reading video";

    double lastNorm = 0.0;
    int lastCounter = 0;
    int counter = 0;
    int currentState = 0;

    namedWindow( "w", 1);
    for( ; ; )
    {
        capture >> frame;
        imshow("w", frame);


        double currentNorm = norm(frame);
        double diffNorm = currentNorm - lastNorm;

        if (diffNorm > 20000 && currentState == 0)
        {
            currentState = 1;
            std::cout << "on  - was off for " << counter - lastCounter << " frames" << std::endl;
            lastCounter = counter;
        }
        if (diffNorm < -20000 && currentState == 1)
        {
            currentState = 0;
            std::cout << "off - was on  for " << counter - lastCounter << " frames" << std::endl;
            lastCounter = counter;
        }

        waitKey(20); // waits to display frame

        lastNorm = currentNorm;
        counter++;
    }
    waitKey(0); // key press to close window
    // releases and window destroy are automatic in C++ interface
}

相关问题 更多 >

    热门问题