Ask Your Question

Fragender's profile - activity

2020-04-08 10:55:22 -0500 received badge  Popular Question (source)
2019-01-22 20:29:34 -0500 received badge  Teacher (source)
2019-01-22 20:28:53 -0500 received badge  Nice Question (source)
2016-02-19 07:43:44 -0500 asked a question VideoWriter and imwrite stop working after a lot of pictures


I have a really strange problem. I am using OpenCV 3.0.0 for writing videos with the VideoWriter class. There I am using a lot of .jpg pictures (round about 14000).

Sadly now my software just stops creating the video before all pictures are written. It also never stops at the same time. So sometimes a couple more or less frames are included in the video. Sometimes the last frame is also just half included. This means that the picture is partly grey.

I also tried just to write the .jpg pictures out again. Here it happens the same! I also checked whether the correct files are loaded. In the console all file names appear correct, even if the corresponding file is not written. I also get no error while running the software. Imwrite just stops writing pictures.

All problems just happen with a huge amount of pictures! If I use less everything works fine. My Task-manager says that I am not running out of memory. There is still a lot of free memory left when running the software.

Do you have any idea, what could be the problem?

int debug=0;
cv::Mat image;

void loadPicturesAndWriteThemAgain()
for (int i=posBeg; i<=posEnd;i=i+frameIncrement)
    QFileInfo file= files[i];

        image = imread(pictureName, CV_LOAD_IMAGE_COLOR);   // Read the file

        // Check for invalid input
        if(! )
            cerr <<  "Could not open or find the image" << std::endl ;
            //  return -1;

        QString nr = outputfoldername+QString::number(debug)+".jpg";
        std::cout << "Written: " << pictureName << std::endl;

Using the VideoWriter class with the following function I got sometimes the error: "OpenCV Error: Assertion failed (wsz == wsz0) in writeBlock, file C:\opencv\sources\modules\videoio\src\cap_mjpg_encoder.cpp, line 132"

bool VCset=false;
cv::VideoWriter *ptrVideoWriter;

void Timelapse::writeVideo(int Width, int Height, const cv::Mat &Mat)

   if (VCset==false)
    frame_width=Width; frame_height=Height;
    ptrVideoWriter= new VideoWriter(outputfoldername.toStdString()+filename.toStdString()+".avi",CV_FOURCC('M','J','P','G'),15, Size(frame_width,frame_height),true);
if (VCset==true) ptrVideoWriter->write(Mat);
   catch (...) {
     std::cerr << "Exception in VideoWriterClass occurred" << std::endl;

Thank you very much :-)

2015-09-18 11:15:33 -0500 received badge  Editor (source)
2015-09-15 09:25:17 -0500 asked a question Background substraction MOG2 apply gets stuck


I am using a background substraction with the following code in a class:

void process(const cv::Mat &inputFrame)
// class members: 
//cv::Ptr<cv::BackgroundSubtractor> pMOG2;
//cv::Mat foregroundMaskMOG2;
//bool backgroundSubstractorCreated=false;

//creates one background subtractor object per instance
    if( backgroundSubstractorCreated==false)
       pMOG2 = cv::createBackgroundSubtractorMOG2(10,250,false); 

    //update the background model
    std::cout <<"here" <<std::endl;
    pMOG2->apply(inputFrame, foregroundMaskMOG2,0); //Background not updated = 0 
    std::cout <<"it hangs" <<std::endl;

The code sometimes runs the whole video without problems, but quite often the video just stops. The positions are always different, but I can say that I have always the "here" (see above) on my console whereas I never get the "it hangs."

Also the program does not crash. If I use "try and catch" nothing is catched. The debugger finds nothing. So it looks like the apply function hangs in an endless loop so that no further code is executed.

The OpenCv Version is 3.0 and I write on Windows using Qt with a 64 bit MinGW compiler.

Does anybody have an idea what could be the reason or how to solve this?

Thank you very much :-)

2015-08-11 03:18:21 -0500 commented answer How to speed up seamlessClone? (incredibly slow)

Thank you very much for the perfect help :-)

2015-08-07 07:08:52 -0500 received badge  Self-Learner (source)
2015-08-07 06:15:23 -0500 received badge  Supporter (source)
2015-08-07 06:15:20 -0500 received badge  Scholar (source)
2015-08-07 06:14:48 -0500 answered a question How to speed up seamlessClone? (incredibly slow)

Hi sturkmen,

thank you very much for the nice code!

No just the background of the normal seamlessClone is missing.

This can be achieved by increasing the brightness and then blurring.

I just added this code before the logo.copyTo:

    double alpha=1.0; //alpha value [1.0-3.0]
    int beta=70; //beta value [0-100]

    /// Increasing brightness:
    /// Do the operation image(i,j) = alpha*image(i,j) + beta
     for( int y2 = y; y2 < mask.rows+y; y2++ )
         for( int x2 = x; x2 < mask.cols+x; x2++ )
                for( int c = 0; c < 3; c++ )
          <Vec3b>(y2,x2)[c] =
                    saturate_cast<uchar>( alpha*(<Vec3b>(y2,x2)[c] ) + beta );
    cv::Rect region (x,y,logo.cols, logo.rows);

    GaussianBlur(frame(region), frame(region), Size(0, 0), 6);


There is just one thing I do not know how to manage. As you can see I used manual values in the first two for loops (mask.rows+100). I do not know where to get the real dimensions of the logo automatically as it is copied using a mask.

Still... the solution works now quite nice :-) Thanks a lot!

2015-08-05 06:40:14 -0500 commented question How to speed up seamlessClone? (incredibly slow)

Hi, thank you very much. Due to rights I cannot provide you the original logo, but the following has the same characteristics and I just tested the cloning with it. It is comparable slow like with the logo I use. Image

Thanks a lot :-)

2015-08-05 05:52:31 -0500 commented question How to filter a long and thin rectangle in a grayscale image?

Hi, thank you very much :-) I will try this and I think it should work.

2015-08-05 03:27:17 -0500 asked a question How to filter a long and thin rectangle in a grayscale image?


I have grayscale images in which I need to filter a long and thin rectangle. My images are looking like this: image

Actually I would like to remove everything besides the long rectangle I marked with red.

The direction of the rectangle can change! So the rectangle can lay in every angle in the image. Also the disturbance pixels are different all the time. Therefore I do not really have an idea at the moment.

If you have any ideas, please share them with me :-)

Thank you very much.

2015-07-25 08:11:51 -0500 received badge  Enthusiast
2015-07-22 10:32:23 -0500 received badge  Student (source)
2015-07-22 10:08:30 -0500 asked a question How to speed up seamlessClone? (incredibly slow)


I am using seamlessClone to integrate a logo into a video and it is incredibly slow. Actually creating a video with 1000 frames with a resolution of 1280 x 800 takes 30 seconds. While using seamlessClone it takes round about 15 seconds to write one single frame!

I am not sure whether I use this function in a wrong way or whether seamlessClone is really that heavy.

This is basically what I do:

int main() {
cv::Mat logo= imread("logo.png", CV_LOAD_IMAGE_COLOR);

Size size(100,120);
Mat LogoResized;
Point center(300,300);

Mat mask_forCloning = 255 * Mat::ones(LogoResized.rows, LogoResized.cols, LogoResized.depth());
Mat normal_clone;
Mat image;

for (int i=0; i<1000; i++)
    image = imread(pictureName, CV_LOAD_IMAGE_COLOR);  //Of course the name is changing with every frame ;-)

seamlessClone(LogoResized, image, mask_forCloning, center, normal_clone, NORMAL_CLONE);



Is there a possibility to speed this up?

Thank you very much :-)

2015-07-18 12:14:23 -0500 asked a question Best approach to integrate MXPEG to OpenCV?


I just got a Mobotix camera and this camera offers next to a normal MJPEG stream also streams in MXPEG. To me it looks like the MXPEG codec brings advantages for me in comparison to MJPEG. Therefore I would like to read videos with MXPEG in OpenCV.

I looked around and found three ways which should be possible: 1. Use C++ library provided by Mobotix 2. Use FFMPEG library 3. Use libVLC

Has anyone made experience with on of these librarys?

I would like to know which might be the easiest way to integrate MXPEG in OpenCV.

Thank you very much :-)