Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MachineIntelligence6 opened this issue Jun 29, 2020 · 6 comments
Labels

Comments

@MachineIntelligence6
Copy link

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

@saudet
Copy link
Member

saudet commented Jun 29, 2020

That's a known limitation of the official Java API. Don't use it if you need custom strides like this, or please report upstream.

@MachineIntelligence6
Copy link
Author

If I use same video and extract frame from java2Dframeconverter and save bufferedimage, I get correct image. If we use OpenCVFrameConverter and get Mat image and save it the image get distorted and give schew/stretch or distort image. I think there is some problem with open cv converter.

@saudet
Copy link
Member

saudet commented Jun 29, 2020

Like I said, this is a problem with the Java API of OpenCV. There is no such limitations with the C++ API of OpenCV. Instead of using Java API, please use the C++ API, and you will not have this issue.

If you cannot use the C++ API of OpenCV, there is nothing else I can do here. Please report this issue upstream.

@MachineIntelligence6
Copy link
Author

MachineIntelligence6 commented Jun 30, 2020

ok got it now, i was thinking there was problem with java from your last comment, but its now clear to me its problem with Java API of OpenCV, i will try to find a work around and report this error. Thank you

saudet added a commit that referenced this issue Jun 30, 2020
@saudet
Copy link
Member

saudet commented Jun 30, 2020

Actually, it looks like the Java API was fixed. There's a new constructor that we can use to set the stride here:
http://bytedeco.org/javacpp-presets/opencv/apidocs/org/opencv/core/Mat.html#Mat-int-int-int-java.nio.ByteBuffer-long-
I've updated OpenCVFrameConverter in the last commit. Please give it a try with the snapshots: http://bytedeco.org/builds/

@MachineIntelligence6
Copy link
Author

Thank you, working perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants