PDA

View Full Version : Edge-detection (maybe using QGL?)



mwgobetti
17th January 2012, 11:28
Hi everybody.

I have a working QGLWidget which inserts colored cones according to mouse click, and shows part of them by depth-comparison, like in the image at the link below:
http://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Coloured_Voronoi_2D.svg/220px-Coloured_Voronoi_2D.svg.png

The boundaries where the colors change represent points with the same depth. I've spent now many days searching and trying via OpenGL coding, using stencil and depth buffers, but I'm not managing to get into a result like in the image at the link below:
http://mathworld.wolfram.com/images/eps-gif/VoronoiDiagram_700.gif

i.e., a white image with only the boundaries filled with black.

I found this tutorial: http://igortrindade.wordpress.com/2010/04/23/fun-with-opengl-and-shaders/
which now makes me wonder if there's a very simple way to achieve the result I want only using some basic QGL properties.

Thank you once again, masters.

stampede
17th January 2012, 19:21
If you are interested only in displaying the edges (and not collecting the edge data), then google for something like "edge detection shader", there should be many solutions ready to use. I've had one implemented in GLSL using the sobel matrix, but can't find it now...
Using image processing lib like OpenCV could be another solution, but probably you don't need that, simple edge detection shader should do the job.

mwgobetti
17th January 2012, 20:26
Hi stampede, thanks a lot for your support. Actually, before posting here, I tried several different combinations of convolution without success. It simply doesn't work. Most probably it's my fault, since I'm still a newbie, but I can't see why. I tried to assign some convolution code to the mouse right button click event. The event is ok, I included some other things just to debug it, but the convolution doesn't happen at all.

I've found some examples in the internet using glConvolutionFilter2DEXT but Qt doesn't recognize this function ("not declared"), though glConvolutionFilter2D is supported, so I tried many combinations of it but all without success.

Please, check if you see something really wrong or missing here:

[...]
if (event->button() == Qt::RightButton) {
GLfloat laplacian[3][3] = {
{ -0.125, -0.125, -0.125 },
{ -0.125, 1.0 , -0.125 },
{ -0.125, -0.125, -0.125 },
};
glConvolutionFilter2D(GL_CONVOLUTION_2D, GL_LUMINANCE, 3, 3, GL_LUMINANCE, GL_FLOAT, laplacian);
glEnable(GL_CONVOLUTION_2D);
}
[...]


This is only one of my 50+ tries...

stampede
17th January 2012, 23:53
Can you verify that:
1) this code actually does the job, by testing for example in separate app ?
2) your qglwidget OpenGL context is the current one - by calling makeCurrent() before any opengl method calls you do outside the resizeGL, paintGL methods (and call doneCurrent() after that)

mwgobetti
18th January 2012, 20:30
I don't have enough knowledge to try a separate app, actually my app is already the simplest I could think of... And I tried inserting makeCurrent() before and doneCurrent() after that convolution block, but still nothing happens. Thanks again for your support.

stampede
18th January 2012, 21:13
You've said that this code is from the internet, maybe a full compilable example is included as well, just to test that this code is really doing edge detection.
What about shaders, have you tried that ? Edge detection shader should be just few lines of code.

mwgobetti
20th January 2012, 13:32
Yes, after a lot of research it looks like that QGLShader and QGLShaderProgram are the answer to my problem. Qt always has its own easy way of doing everything, that's why I created this thread, I knew someone would link me to that way.