bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.48k stars 581 forks source link

How do I convert a javacv Frame fetched from FrameGrabber to a opencv.core.Mat object #765

Closed wweverma1 closed 3 months ago

wweverma1 commented 3 months ago

Hello! First of all thanks to the author for building such an amazing library 👏

Can someone please help me with this- I am working on a project where I have to retrieve frames from a RTSP stream and then convert it to opencv.core.Mat for further processing. Currently I'm using FFmpegFrameGrabber to get the frames. then converting that frame into a bitmap using AndroidFrameConverter followed by creating a opencv.core.Mat object and populating it with the bitmap using Utils.bitmapToMat further processing it to RGB.

Is there any easy way to directly get to my requirement without these intermediate steps to optimize my code.

Current Implementation-

val converter = AndroidFrameConverter()
val img = Mat()
val Frame: Frame?
frame = grabber.grabImage() ?: break
val bitmap = converter.convert(frame)
val orgImg = Mat(bitmap.height, bitmap.width, CvType.CV_8UC3)
Utils.bitmapToMat(bitmap, orgImg)
Imgproc.cvtColor(org, img, Imgproc.COLOR_BGR2RGB)

//doing further processing with img

Any help will be appreciated :)

saudet commented 3 months ago

We can use OpenCVFrameConverter.ToOrgOpenCvCoreMat for that: http://bytedeco.org/javacv/apidocs/org/bytedeco/javacv/OpenCVFrameConverter.ToOrgOpenCvCoreMat.html

wweverma1 commented 3 months ago

Thanks @saudet for the heads up, One more thing will the Mat object received from OpenCVFrameConverter.ToOrgOpenCvCoreMat have RGB or BGR color scheme??

saudet commented 3 months ago

OpenCV is usually BGR, if that's what you're asking

wweverma1 commented 3 months ago

Got it, thanks for taking out your time :)