opencv opengl drawing on a frame - opengl

How can I use both Opencv and OpenGl to draw some 3D shape onto a Mat frame ? The scenario is simple I am grabbing some frames ( cv::Mat) and I would like to visualise them with some 3D cool target arrows like this ---> target <---. Any ideas on how to do that? What would be a good reference to search for ?

Firstly use latest OpenCV version, or use the version which comes with Qt support. Make sure that you install OpenCV with Qt support, if you are building yourself then build with Qt support. Next go to this link http://docs.opencv.org/modules/highgui/doc/qt_new_functions.html#setopengldrawcallback
A single function will let you draw on the top of the image using a callback function.

Related

Realsense SDK draw 3D scanning boundaries

I am writing a c++ app for 3D scanning. I'd like to add modify the image i get from
PXCImage* image = scanner->AcquirePreviewImage();
or directly the QImage i create from
QImage* qimage = new QImage(imageData.planes[0], width, height, QImage::Format_RGB32);
to display the rectangle which represent the scanned area as in the image below (taken from the c# example).
I am probably just missing some understanding of the SDK, but I'd be grateful if someone could explain it to me.
(I am using the PXC3DScan::ScanningMode::VARIABLE if that could affect the process).
I received an answer from Intel:
It will not support to draw scan boundary when you selected variable scan mode. We will provide this feature in the future release. Thanks!
(Current SDK version 7.0.23.8048)

Rendering VTK visualization using OpenCV instead

Is it possible to get a rendered frame from a VTK visualization and pass it to OpenCV as an image without actually rendering a VTK window?
Looks like I should be able to follow this answer to get the rendered VTK frame from a window and then pass it to OpenCV code, but I don't want to render the VTK window. (I want to render a PLY mesh using VTK to control the camera pose, then output the rendered view to OpenCV so I can distort it for an Oculus Rift application).
Can I do this using the vtkRenderer class and not the vtkRenderWindow class?
Also, I'm hoping to do this all using the OpenCV VTK module if that is possible.
EDIT: I'm starting to think I should just be doing this with VTK functions alone since there is plenty of attention being paid to VTK and Oculus Rift paired together. I would still prefer to use OpenCV since that side of the code is complete and works nicely already.
You must make your render windows to render offline like this:
renderWindow->SetOffScreenRendering( 1 );
Then use a vtkWindowToImageFilter:
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();
windowToImageFilter->SetInput(renderWindow);
windowToImageFilter->Update();
This is called Offscreen Rendering in VTK. Here is a complete example
You can render the image offscreen as mentioned by El Marce and then convert the image to OpenCV cv::Mat using
unsigned char* Ptr = static_cast<unsigned char*>(windowToImageFilter->GetOutput()->GetScalarPointer(0, 0, 0));
cv::Mat RGBImage(dims[1], dims[0], CV_8UC3, Ptr);

show tracked object in Video using OpenGL

I am extending an existing OpenGL project with new functionality.
I can play a video stream using OpenGL with FFMPEG.
Some objects are moving in the video stream. Co-ordinates of those objects are know to me.
I need to show tracking of motion for that object, like continuously drawing a point or rectangle around the object as it moves on the screen.
Any idea how to start with it?
Are you sure you want to use OpenGL for this task? Usually for computer vision algorithms, like motion tracking one uses OpenCV. In this case you could simply use the drawing functions of OpenCV as documented here.
If you are using OpenGL you might have a look at this question because in this case I guess you draw the frames as textures.

Drawing in OpenCV with OpenGL

OpenCV library, when compiled with GPU and OpenGL support, allows for displaying images with OpenGL. For example, video_reader.cpp (located in gpu samples) uses OpenGL to render display graphics directly from cv::gpu::GpuMat.
cv::gpu::GpuMat d_frame;
namedWindow("OpenGL", WINDOW_OPENGL);
cv::gpu::VideoReader_GPU d_reader(fname);
d_reader.dumpFormat(std::cout);
if (!d_reader.read(d_frame))
break;
cv::imshow("GPU", d_frame);
This is a very useful feature. However, it is not documented in the documentation on-line. For example, for namedWindow the flag WINDOW_OPENGL is not listed in the docs. Where can I find the documentation for OpenGL -related functionality of OpenCV?
The documentation is not very complete for 2.4.5. I don't think there is any more documentation than what you see on http://docs.opencv.org.
There are older documentation, such as http://opencv.willowgarage.com/documentation/cpp/ for 2.1, but I didn't find it having the documentation for the flag that you wanted.
OpenCV/OpenGL interop documentation
https://docs.opencv.org/4.x/d2/d3c/group__core__opengl.html
Highgui/OpenGL documentation
https://docs.opencv.org/4.x/df/d24/group__highgui__opengl.html
Especially the second link gives you two easy options for rendering.
imshow with a Texture2D object
setOpenGlDrawCallback() (https://docs.opencv.org/4.x/df/d24/group__highgui__opengl.html#gaf80dcbc168a6ce40f6d1ad9d79a10bb8) which allows you to draw on top of a nameWindow("OpenGL Window", WINDOW_OPENGL) using OpenGL.
The function
cv::imshow(const & string, cv::InputArray)
does not natively support gpu matrices, but supports GPU matrices by casting.
i.e. anything which supports a cv::Mat on its input should also support cv::gpu::GpuMat. I presume this means that when you call the function, it will automatically download the matrix to a cv::Mat and go from there, i.e. your imshow function call is not drawing directly gpu->gpu, but going gpu->cpu->gpu.

Blitting zoomed images using SDL

Is there any way to Blit a zoomed (in) version of a certain image/sprite using SDL? (Besides manually saving a zoomed version of the image..)
Thanks :-)
Not with the SDL API itself.
I think there exist libraries (for SDL) who support zooming (so called resizing).
EDIT: http://www.ferzkopp.net/joomla/content/view/19/14/
Generally speaking, SDL isn't suitable for graphics that require real-time rotations and zooming. For that, you'd need an library based around an accelerated API such as DirectX or OpenGL. I understand that SFML meets this requirement.