Ask Your Question

kieranfish's profile - activity

2020-11-05 14:26:40 -0600 received badge  Student (source)
2017-02-21 03:30:03 -0600 asked a question Effective way to automatically set NoiseSigma in BackgroundSubtractorMOG?

Hey there, I'm trying to make a program that detects people in CCTV footage and I've made a lot of progress. Unfortunately, the amount of noise in the videos varies a lot between different cameras and time of day, so with each of the sample videos. This means that the NoiseSigma needed varies from 1-25.

I've used a fastNlMeansDenoisingColored function and that helped a bit, but the NoiseSigma is still an issue.

Would it be effective to maybe loop through the video once, and somehow get an idea for how noisy the video is and make a relationship for noise vs NoiseSigma? Any ideas would be welcome.

2017-02-20 07:12:47 -0600 commented answer OpenCV issue with drawing Farneback optical flow in ROI.

This solved it:

const Point2f& flowatxy=flow.at<Point2f>( pt1.y+y , pt1.x+x );
2017-02-20 07:12:06 -0600 commented answer OpenCV issue with drawing Farneback optical flow in ROI.

@berak I'VE DONE IT!

I just changed the flowatxy declaration to: const Point2f& flowatxy=flow.at<point2f>( pt1.y+y , pt1.x+x );

2017-02-20 05:27:40 -0600 commented answer OpenCV issue with drawing Farneback optical flow in ROI.

@berak Well, this might be better but it still isn't right. Now it draws it up in the top left corner, which makes sense if you look at the first points in the line function. The for loops will start plotting the line at (0,0) and then (1,0) and so on.

But at least now, it seems like it is plotting the correct flow for the ROI, just in the wrong place. If that makes sense.

2017-02-20 04:52:25 -0600 commented answer Grabbing the image skips current (or next) frame.

@Farshadhn If you post your code, it will be easier to help. Is the issue with the video playing to fast when you imshow it or when you save it? For the imshow, make sure that the waitKey at the end is 1000/fps and that there isn't any grab inside the if(frame.empty()) loop.

2017-02-20 04:36:15 -0600 asked a question OpenCV issue with drawing Farneback optical flow in ROI.

Hey hey, I've written a code to create bounding boxes and draw the Farneback optical flow inside. The optical flow is calculated normally before hand and then it is drawn separately for each ROI box.

The problem comes when I draw the flow. The flow comes out looking normal, but shifted down and right. Here's the output, notice the bottom right has the flow of the moving person.

person is moving but flow drawn in wrong place

Here is the frame with the flow drawn everywhere, showing where the flow should be drawn.

Flow drawn everywhere to show where it should be

The code attached is stripped down for simplicity, so excuse me if there are a few undeclared Matrices or something.

#include ...

using namespace cv;
using namespace std;

Mat currentImage, img, printr, gray ,prevgray, flow;

void getRectanglesandROI(Mat &Mask, Mat &imgTmp, Mat &imgOut, vector<Rect> &outBoxes);

void DrawFlowMap(Mat Image, Mat ROI, Rect Box, Point centre);

int main (int argc, char *argv[]) {

    VideoCapture inVid("input.avi");

    if (!inVid.isOpened()) {
        cout << "Failed to open the input video" << endl;
        exit(5);}

    int loop=0, count =0, MaxTargets=0;
bool test=true; 

    namedWindow("Detected");

    int ex = inVid.get(CV_CAP_PROP_FOURCC);
    double fps = inVid.get(CV_CAP_PROP_FPS);
    int wait=1000/fps;
    Size S = Size(  (int) inVid.get(CV_CAP_PROP_FRAME_WIDTH), (int) inVid.get(CV_CAP_PROP_FRAME_HEIGHT));
    int fr =inVid.get(CV_CAP_PROP_FRAME_COUNT);

    VideoWriter output;                                        // Open the output
    output.open("output.avi", ex, fps, S, true);
    if (!output.isOpened())
        {
            cout  << "Could not open the output video for write: " << endl;
            return -1;
        }
//=============4EVR=================
    while(test){

    inVid>>currentImage;
        if (currentImage.empty())
        {
            count++;
            //if (count==1){if (waitKey(0)==27){waitKey(2);}}
            if (count==1){fs.release(); break;}
            cout <<"Max Targets=" <<MaxTargets<< endl<< "End of video, looping" << endl<<endl;
            inVid.set(CV_CAP_PROP_POS_AVI_RATIO, 0);
            loop=0;
        }

        cvtColor(currentImage, gray,CV_RGB2GRAY);
        if (prevgray.empty()){gray.copyTo(prevgray);}

        currentImage.copyTo(img);

        calcOpticalFlowFarneback(prevgray,gray,flow,0.5,3,21,20,5,1.2,0);

        vector<Rect> outputBoxes;
        getRectanglesandROI(fgMaskMOG2, img, currentImage, outputBoxes);
        gray.copyTo(prevgray);

        imshow("Detected", currentImage);
        waitKey(wait);
    }
    return 0;
}
//============END===========================================================

void getRectanglesandROI(Mat &Mask, Mat &imgTmp, Mat &imgOut, vector<Rect> &outBoxes){

    vector<vector<Point> > v; 
vector<int> targets;
int tarArea=1;

    findContours(Mask, v, CV_RETR_EXTERNAL/*CV_RETR_LIST*/, CV_CHAIN_APPROX_SIMPLE);

    for (int j = 0; j < v.size(); j++) {
            if (tarArea < v[j].size()) { // excluding tiny contours
                    targets.push_back(j);
            }
    }
        for (int j = 0; j < targets.size(); j++) {  
            drawContours(imgTmp, v, targets[j], Scalar(255, 0, 255), 1, 8);
            Rect rect = boundingRect(v[targets[j]]);

            roi=currentImage(rect);
            DrawFlowMap(currentImage, roi, rect);
            }
}

void DrawFlowMap(Mat Image, Mat ROI, Rect Box){

Point pt1 = Point(Box.x, Box.y);

for( int y=0; y<roi.rows; y+=5){        //this is the issue area, probably.
    for (int x=0;x<roi.cols;x+=5){
        const Point2f& flowatxy=flow.at<Point2f>(y,x);

            line(Image, Point(cvRound(pt1.x+x),             cvRound(pt1.y+y)),
                        Point(cvRound(pt1.x+x+flowatxy.x),  cvRound(pt1.y+y+flowatxy.y)),   Scalar(0,255,0)); ///FLOW LINES
    }
}
}
2017-02-14 05:16:16 -0600 received badge  Enthusiast
2017-02-13 01:19:29 -0600 commented answer Grabbing the image skips current (or next) frame.

Perfect! The problem was in the "if (!cap.grab())" loop, as that method caused a second grab. If I use the frame.empty instead, it works out great!

Thanks a bunch.

2017-02-13 01:17:25 -0600 received badge  Supporter (source)
2017-02-13 01:17:24 -0600 received badge  Scholar (source)
2017-02-12 10:30:34 -0600 commented question Grabbing the image skips current (or next) frame.

Yeah, sure sure. I had a video writer with CV_CAP_PROP_FPS or something set as the fps, but I took it out as I was trying to strip the code to see where it skipped the frame.

I've never played a video using imshow(currentimage0) though, I always use imshow(currentimage). Do you think it would make a difference if I didn't use cap>>currentimage every loop, and instead used it once before the loop and then called currentimage0 to display?

Edit: Do you think if I remove the cap>>currentImage and make a Mat currentImage= cap after the if loop, it will stop grabbing twice? That seems positive.

2017-02-10 08:30:36 -0600 asked a question Grabbing the image skips current (or next) frame.

Hey there, I've recently realised that all of my output videos are twice as fast as the original, so i made the program print out the current frame and noticed that it was increasing by 2 each time (2,4,6,8...). When I removed the "cap>>currentImage;" it printed 1,2,3,4,...

I just don't understand why this is happening. Is there a mistake in the way I'm printing the frame? Any help would be appreciated.

int main (int argc, char *argv[]) {

    /// CHANGE INPUT FILE HERE
    Mat currentImage;
    VideoCapture cap("testcut.avi");//"herman.avi"

    if (!cap.isOpened()) {
            cout << "Failed to open the input video" << endl;
            exit(5);}

    for(;;){
        cap>>currentImage;
        if (!cap.grab())
        {
            cout << "\n End of video, looping" << endl;
            cap.set(CV_CAP_PROP_POS_AVI_RATIO, 0);
        }
    waitKey(80);
    cout<<"frame number=         "<<cap.get(CV_CAP_PROP_POS_FRAMES)<<endl;

    }
    return 0;
}
2017-02-06 03:28:12 -0600 commented answer Video feed starts white and it's messing with my background subtraction

Some great suggestions up there! I'm thinking of taking a frame from somewhere in the middle and using it as a 'template frame'-type thing. And then getting the value of that image and adjusting the brightness of each frame until it's that value, do you think that will be affective? I like the sound of changing the learning rate as the lighting gets better. Would the learning rate in BackgroundSubtractorMOG be the history parameter?

2017-02-03 04:51:51 -0600 answered a question How to scan line by line in an image array

I'm not sure how to check for binary, but this works for scanning through each pixel.

for( int y=0; y< roi.rows; y+=1){
    for (int x=0;x< roi.cols;x+=1){
    checkforbinary();
    }
}
2017-02-02 02:32:47 -0600 asked a question Video feed starts white and it's messing with my background subtraction

Hey there, I've made an motion tracker in C++ and I'm running into issues with the video feed. The video starts off white and then the brightness fades as the camera adjusts to the brightness. I'm using BackgroundSubtractorMOG and it detects this as a constantly changing background. Is there anyway to auto detect the brightness and keep it at a certain level, or even just detect when an image is too bright to detect and only apply the subtractor when it reaches an acceptable level?