Ask Your Question
1

blinking window

asked 2015-12-02 03:02:08 -0600

LBerger gravatar image

updated 2015-12-02 03:04:57 -0600

Hi,

I want to measure pulse frequency using opencv and a webcam. I have got one thread for videocapture. In main program I plot pixel value as function of time. Video is nice but plot graph are blinking as you can see in this video. Is it possible to have no blinking for these plots?

Thanks for your help

PS If I use another thread for plot problem is still visible. VS 2013 Windows 10 opencv 3.0-dev

My code to plot a graph is

void TimeSignal()
{
    Mat red=Mat::zeros(300,2*MAXTIME,CV_8UC3);
    Mat green=Mat::zeros(300,2*MAXTIME,CV_8UC3);
    Mat blue=Mat::zeros(300,2*MAXTIME,CV_8UC3);
    imshow("Red componnent",red);
    imshow("Green componnent",green);
    imshow("Blue componnent",blue);
    waitKey(10);
    double fAcq=20;
    std::this_thread::sleep_for (std::chrono::seconds(1));
    int indFramePre=-1;
    while (true)
    {
        mtxFrame.lock();
        if (mode == 27)
        {
            mtxFrame.unlock();
            return;
        }
        if (mode == 's')
        {

            mtxFrame.unlock();
            waitKey(30);

        }
        else
        {

            if (indFrame != indFramePre)
            {
                indFramePre = indFrame;
                mtxProbe.lock();
                map<int, list<Vec4f> >::iterator ite = probeValue.begin();
                vector<Scalar> color={Scalar(255,0,0),Scalar(0,255,0),Scalar(0,0,255),Scalar(255,255,0),Scalar(255,255,255)};
                for (int c=0; ite != probeValue.end(); ite++,c++)
                {

                    list<Vec4f>::iterator iteList = ite->second.begin();
                    list<Vec4f>::iterator iteListPre = ite->second.begin();
                    float tpsIni = (*iteListPre)[0];
                    int minColor[4]={0,0,0,0};
                    int maxColor[4]={255,255,255,255};
                    int offsetRows=20;
                    for (int i = 0; i < ite->second.size(); i++,iteList++)
                    {
                        for (int j = 1; j < 4; j++)
                        {
                            if ((*iteListPre)[j]<minColor[j])
                                minColor[j]=(*iteListPre)[j];
                            if ((*iteListPre)[j]>maxColor[j])
                                maxColor[j]=(*iteListPre)[j];
                       }
                    }
                    iteList = ite->second.begin();
                    iteListPre = ite->second.begin();
                    iteList++;
                    for (int i = 1; i < ite->second.size(); i++,iteListPre++,iteList++)
                    {
                        line(green,Point(((*iteListPre)[0]-tpsIni)*fAcq, ((*iteListPre)[2]-minColor[2])/(maxColor[2]-minColor[2]+1)*255+offsetRows),Point(((*iteList)[0]-tpsIni)*fAcq, ((*iteList)[2]-minColor[2])/(maxColor[2]-minColor[2]+1)*255+offsetRows),color[c],1);
                        line(red,Point(((*iteListPre)[0]-tpsIni)*fAcq, ((*iteListPre)[1]-minColor[1])/(maxColor[1]-minColor[1]+1)*255+offsetRows),Point(((*iteList)[0]-tpsIni)*fAcq, ((*iteList)[1]-minColor[1])/(maxColor[1]-minColor[1]+1)*255+offsetRows),color[c],1);
                        line(blue,Point(((*iteListPre)[0]-tpsIni)*fAcq, ((*iteListPre)[3]-minColor[3])/(maxColor[3]-minColor[3]+1)*255+offsetRows),Point(((*iteList)[0]-tpsIni)*fAcq, ((*iteList)[3]-minColor[3])/(maxColor[3]-minColor[3]+1)*255+offsetRows),color[c],1);
                    }


                }
                mtxProbe.unlock();
            }
            mtxFrame.unlock();
            imshow("Red componnent",red);
            imshow("Green componnent",green);
            imshow("Blue componnent",blue);
            waitKey(30);
            red=Mat::zeros(300,2*MAXTIME,CV_8UC3);
            green=Mat::zeros(300,2*MAXTIME,CV_8UC3);
            blue=Mat::zeros(300,2*MAXTIME,CV_8UC3);
        }
    }
}

and all code is here

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-12-02 04:27:00 -0600

pklab gravatar image

updated 2015-12-02 04:44:45 -0600

isn't blink... charts looks blinking because you rewrite red,green and blue Mats always but draw lines only if (indFrame != indFramePre). Time to time the if is false and you will show a black image ... final result looks blinking

You could reset the Mats before to draw lines than show images only if needed:

bool redraw = false;
if (indFrame != indFramePre)
{
    ...
    red = Mat::zeros(300, 2 * MAXTIME, CV_8UC3);
    green = Mat::zeros(300, 2 * MAXTIME, CV_8UC3);
    blue = Mat::zeros(300, 2 * MAXTIME, CV_8UC3);
    circle(red, cv::Point(100, 100), 50, 255, 2); //draw this to check blinking
    redraw = true;
    for (int i = 1; i < ite->second.size(); i++, iteListPre++, iteList++)
    {
        line(green, Point(((*iteListPre)[0] - tpsIni)*fAcq,...
        ...
    }
}
mtxFrame.unlock();
if (redraw)
{
    imshow("Red componnent", red);
    ...
edit flag offensive delete link more

Comments

1

thanks it's ok now

LBerger gravatar imageLBerger ( 2015-12-02 04:40:54 -0600 )edit
1

btw your application is very nice !

pklab gravatar imagepklab ( 2015-12-02 04:46:43 -0600 )edit

Thanks again next step check if frequency sampling is constant (I think no) and resampling signal before compute and display spectrum..

LBerger gravatar imageLBerger ( 2015-12-02 04:52:49 -0600 )edit
1

sure freq isn't constant because of shared vars and locks. In addition, because of some lock you can lost some signal at all... try it moving the live window... you will lost the signal in TimeSignal. I think locking should be optimized or removed where is possible.

pklab gravatar imagepklab ( 2015-12-02 05:13:11 -0600 )edit

Now I have updated code to include brightness... and improve scale for curve. mean value for frequency sampling is 28.6Hz with a maxmum of 33.3hz and a minimum of 13Hz for only one curve

LBerger gravatar imageLBerger ( 2015-12-02 07:19:49 -0600 )edit

...in your code, probes get value from a single pixel. Why don't you use the average of a small roi around the point ?

pklab gravatar imagepklab ( 2015-12-02 09:30:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-02 03:02:08 -0600

Seen: 1,718 times

Last updated: Dec 02 '15