ochensati
ochensati

Reputation: 359

Slanted video in openCV

I am trying to make a movie with openCV. I can put all the images into the avi and it works great, except that the image is slanted at a big angle. It looks like it has been smeared across the image.

The frames are perfectly square, so it cannot be a matter of getting the height and width confused. The images are 24 bit. The other thing that is weird is that it does not seem to be consistent when it happens. sometimes it looks great, sometimes it is slanted.

Here is an image from the movie: http://dl.dropbox.com/u/63600049/MIP.avi

Here is the code:

PINVOKE Method

                IntPtr _ptr = CvInvoke.cvCreateVideoWriter(AVIFilename, CvInvoke.CV_FOURCC('M', 'J', 'P', 'G'), 15, size, 1);

                int count = 0;
                for (int n = 1; n < Frames.Length; n++)
                {
                    if (Frames[n].Trim().Length > 0)
                    {
                        try
                        {
                            FileInfo file = new FileInfo(Frames[n]);
                            IntPtr ptr = CvInvoke.cvLoadImage(file.FullName, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR | Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYDEPTH);
                            if (ptr == IntPtr.Zero)
                                throw new NullReferenceException(String.Format("Unable to load image from file \"{0}\".", file.FullName));

                            CvInvoke.cvWriteFrame(_ptr, ptr);
                            //VW.WriteFrame<Bgr, byte>(frame);
                            count++;
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.Print(ex.Message);
                        }
                    }
                }
                CvInvoke.cvReleaseVideoWriter(ref _ptr);

EMGU Version

 public static void CreateAVIVideoEMGU(string AVIFilename, string[] Frames)
    {
        Bitmap bitmap = new Bitmap(Frames[0]);

        VideoWriter VW = null;

        VW = new VideoWriter(AVIFilename, CvInvoke.CV_FOURCC('M', 'J', 'P', 'G'), 15, bitmap.Height, bitmap.Width, true);

        int count = 0;
        for (int n = 1; n < Frames.Length; n++)
        {
            if (Frames[n].Trim().Length > 0)
            {
                var frame = new Emgu.CV.Image<Bgr, byte>(ConvertBitmapTo24(new Bitmap ( Frames[n])));
                VW.WriteFrame<Bgr, byte>(frame);
                count++;
            }
        }

        VW.Dispose();
    }

The images are all 24 bit jpg of the same dimensions

Upvotes: 0

Views: 1678

Answers (2)

dvhamme
dvhamme

Reputation: 1450

Certain codecs will only accept width (and sometimes height) values that are a multiple of 2, 4, 8 or more. I am unaware of this restriction in the MJPG codec, but the issue you describe is similar to what happens when you use said codecs on images with widths that are not a multiple of the specified value. You might want to see if making the width into a multiple of 16 fixes the problem.

Upvotes: 1

aardvarkk
aardvarkk

Reputation: 15996

I think it's likely an image stride/pitch issue. Make sure you support images where the number of allocated bytes per line may not be equal to the width times the size of a single pixel. Unfortunately I'm not familiar with EmguCV, but I would expect it would support this.

Upvotes: 0

Related Questions