Skip to content

Image is distorted when converting from OpenCVFrameConverter.ToOrgOpenCvCoreMat using a mp4 video #1460

@MachineIntelligence6

Description

@MachineIntelligence6

Im trying to converting Frame from to Mat but the image is distorted. below is the link of the video and code.

`package com.mi6.surf;

import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.opencv.core.Mat;

public class Grabber {

private FFmpegFrameGrabber grabber;

private final OpenCVFrameConverter.ToOrgOpenCvCoreMat converter;

public Grabber() {
	avutil.av_log_set_level(avutil.AV_LOG_QUIET);
	converter = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
}

public void setVideo(String videoFilename) throws Exception {
	System.out.println("Setting Video in Grabber. ");
	grabber = new FFmpegFrameGrabber(videoFilename);
	
	System.out.println("Starting Grabber. ");
	grabber.start();
}

public long getTotalTime() {
	return grabber.getLengthInTime();
}

public Mat getFrameAt(long timestamp) throws Exception {
	System.out.println("Getting Frame at "+ timestamp);
	grabber.setTimestamp(timestamp);
	Frame frame = grabber.grabImage();
	
	if(frame==null || frame.imageWidth ==0 || frame.imageHeight==0)
		throw new Exception("Cannot Extract Frame at timestamp: "+ timestamp);
	
	return converter.convert(frame);
}

public void stopGrabber() throws Exception {
	System.out.println("Stopping Grabber. ");
	grabber.stop();
}

}

`package com.mi6.opencv;

import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.opencv.opencv_java;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;

import com.mi6.surf.Grabber;

/**

  • Hello world!

*/
public class Test2 {

public static void main(String[] args) throws Exception {
	
	Loader.load(opencv_java.class);
	
	String videoFilename = "C:\\Users\\Mi6\\Desktop\\Jobs 2013.mp4";

	long timestamp = (long) ((2) * 1000_000); //2 sec

	Grabber grabber = new Grabber();

	grabber.setVideo(videoFilename);
	
	Mat objectImage = grabber.getFrameAt(timestamp).clone();
	
	HighGui.imshow("Object Image", objectImage);
	HighGui.waitKey(0);
	
}

}
[Distorted Image](https://1drv.ms/u/s!As2o7s_R_SwDghtwCI07dH87epJI?e=H8tMQG)
Video Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions