using mouse click to detect contour in opencv - c

I have various contours/segments in an image. Right now I use the keyboard to input which segment I want to extract from the image (given below)
Consider the pink area as segment 1 and white area as segment 2. I use keyboard to input which segment(1 or 2) I want extracted.On drawing contour I get
I have saved the colours of each segments in an array and I use the array to extract the particular segment.
Now I want know if its possible to use mouse click to identify the segment/contour, So that I can draw multiple contours and identify the segment using mouse and not use keyboard to input the segment number.

I know this question was asked a while ago, but this is what I did to solve my problem. I was doing it in python, but it should be similar.
I used the OpenCV floodfill algorithm to fill where I clicked, using the drawn contours on a binary image as the floodfill mask, where the contours are white and background is black. This way the floodfill cannot pass the contour edges.
Once you fill the contour with a certain color, it's easy to extract it using some sort of thresholding.
Hope this helps.

Related

Dividing an image into Irregular Regions

I have a face image of 800*600. I want to divide it into different non-rectangular regions like one region for left eye, one for right eye, and so on.
I basically want to design a code - "Given (x,y) coordinate, in which region it lie."
How to do this??
You can use OpenCV. It has simple functions for eye detecting and it's free.
Read these material:
http://opencv-code.com/tutorials/eye-detection-and-tracking/
http://www.codeproject.com/Articles/23191/Face-and-Eyes-Detection-Using-OpenCV
Read this pdf

How to display the tiny triangles or recognize them quickly?

What I am doing is a pick program. There are many triangles and I want select the front and visible ones by a rectangular region. The main method is described below.
there are a lot of triangles and each triangle has its own color.
draw all the triangles to a frame buffer.
read the color of pixel in frame buffer and based on the color, we know which triangles are selected.
The problem is that there are some tiny triangles can not be displayed in the final frame buffer. Just like the green triangle in the picture. I think the triangle is too tiny and ignored by the graphic card.
My question is how to display the tiny triangles in the final frame buffer? or how to know which triangles are ignored by the graphic card?
Triangles are not skipped based on their size, but if a pixel center does not fall inside or lie on the top or left edge (this is referred to as coverage testing) they do not generate any fragments during rasterization.
That does mean that certain really small triangles are never rasterized, but it is not entirely because of their size, just that their position is such that they do not satisfy pixel coverage.
Take a moment to examine the following diagram from the DirectX API documentation. Because of the size and position of the the triangle I have circled in red, this triangle does not satisfy coverage for any pixels (I have illustrated the left edge of the triangle in green) and thus never shows up on screen despite having a tangible surface area.
If the triangle highlighted were moved about a half-pixel in any direction it would cover at least one pixel. You still would not know it was a triangle, because it would show up as a single pixel, but it would at least be pickable.
Solving this problem will require you to ditch color picking altogether. Multisample rasterization can fix the coverage issue for small triangles, but it will compute pixel colors as the average of all samples and that will break color picking.
Your only viable solution is to do point inside triangle testing instead of relying on rasterization. In fact, the typical alternative to color picking is to cast a ray from your eye position through the far clipping plane and test for intersection against all objects in the scene.
The usability aspect of what you seem to be doing seems somewhat questionable to me. I doubt that most users would expect a triangle to be pickable if it's so small that they can't even see it. The most obvious solution is that you let the user zoom in if they really need to selectively pick such small details.
On the part that can actually be answered on a technical level: To find out if triangles produced any visible pixels/fragments/samples, you can use queries. If you want to count the pixels for n "objects" (which can be triangles), you would first generate the necessary query object names:
GLuint queryIds[n]; // probably dynamically allocated in real code
glGenQueries(n, queryIds);
Then bracket the rendering of each object with glBeginQuery()/glEndQuery():
loop over objects
glBeginQuery(GL_SAMPLES_PASSED, queryIds[i]);
// draw object
glEndQuery(GL_SAMPLES_PASSED);
Then at the end, you can get all the results:
loop over objects
GLint pixelCount = 0;
glGetQueryObjectiv(queryIds[i], GL_QUERY_RESULT, &pixelCount);
if (pixelCount > 0) {
// object produced visible pixels
}
A couple more points to be aware of:
If you only want to know if any pixels were rendered, but don't care how many, you can use GL_ANY_SAMPLES_PASSED instead of GL_SAMPLES_PASSED.
The query counts samples that pass the depth test, as the rendering happens. So there is an order dependency. A triangle could have visible samples when it is rendered, but they could later be hidden by another triangle that is drawn in front of it. If you only want to count the pixels that are actually visible at the end of the rendering, you'll need a two-pass approach.

OpenGL - Add text without using GLUT

I want to add characters (x/y/z.., not even strings) to a window using OpenGL, WITHOUT using GLUT. I know about glutBitmapString(), but I want to avoid glut. Any suggestions...?
Last time I did this for a retro-style game, I created a bitmap font and wrote a small routine that would draw a quad with the specific character as a texture on it. Another option is to draw every pixel of the bitmap font in a seperate quad.
You can find example code here:
http://svn.berlios.de/wsvn/pong2/trunk/src/Interface.h
http://svn.berlios.de/wsvn/pong2/trunk/src/Interface.cpp
More specifically:
void Interface::createFont() initiates a bitmap font as a display list for each character
void Interface::drawText(const std::string& text) lets OpenGL call the display lists according to the string's characters
In this specific example, I wanted textured "pixels" within the characters, so each bitmap entry results in its own quad with a stock texture on it. Display lists are nowadays less favored as newer OpenGL features like FBOs and VBOs replace their functionality. I don't know if at some point display lists got deprecated as well.
The text in createFont() was created by The Gimp (http://www.gimp.org) export functionality.
Screenshot to celebrate the 20k:
I would suggest using a glyph map, which basically boils down to a bitmap texture with a bunch of letters distributed over it. Load in the texture and draw quads with texture coordinates mapped to the location of the glyph you want in the texture.
There are some drawbacks in a naive implementation that can be partially alleviated. For example, rather than drawing a ton of quads in separate draw calls, you could take a cue from Java and make immutable strings that tie to a GPU buffer and pack all the vertices and uvs you need to draw the word into that buffer. (They don't have to be immutable, just know that if you need to make a word longer or shorter, you'll have to reallocate the buffer or leave extra space to put the new letters).
The site that I used whenever I was trying to learn how to do this can be found here:
Bitmap Fonts
I have used this method with a WebGL implementation and it has worked quite well. I even have wrote a tool to generate the texture from a <canvas> element on the fly.

OpenGL: How to drag image and move it to the line by using the mouse

I want to drag an image to one line by using the mouse and when the image is close to the line, the image will automatically move on to the line, like some "floor planner" program ------------you create wall and drag the door to this wall and when the door is close to the wall, the door will automatically show up on the wall.
Can OpenGL do it?
if it can, can anyone tell me how? If it can not, can anyone tell me how I can do it?
Show me an example.
OpenGL is a rendering API, it's purpose is to generate rasterized images based on descriptions provided to it by an application.
It knows nothing about user input, and even less about the application's "domain objects" such as doors, walls, and so on. All it deals with is abstract coordinates and matrices that describe the transforms and projections to take those 3D coordinates into 2D for rasterization, as well as shading for surfaces and so on.
So, it's up to you to implement that, so that the coordinates you eventually pass to OpenGL end up being what you want them to be.
Snapping is typically a combination of measuring the distance to some guiding object, and the following quantization of the input coordinates to correspond to the the guide.

Converting mouse position to world position OpenGL

Hey, I'm working on a map editor for my game, and I'm trying to convert the mouse position to a position in the game world, the view is set up using gluPerspective
A good place to start would be the function gluUnProject, which takes mouse coordinates and calculates object space coordinates. Take a look at http://nehe.gamedev.net/data/articles/article.asp?article=13 for a basic tutorial.
UPDATE:
You must enable depth buffering for the code in that article to work. The Z value for mouse coordinates is determined based on the value in the depth buffer at that point.
In your initialization code, make sure you do the following:
glEnable(GL_DEPTH);
A point on the screen represents an entire line (an infinite set of points) in 3D space.
Most people with questions similar to yours are really trying to select an object by clicking on it. If that's what you're after, OpenGL offers a selection mode that's generally more effective than trying to convert the screen coordinate into real-world coordinates.
Using selection mode is (usually) pretty simple: you start with gluPickMatrix, which you use to specify a small box around the click point. You then draw your scene in selection mode. When you're done, instead of actually drawing anything, it gives you back records of what would have been drawn in the box you specified. If memory serves, those are arranged in Z order, so the first one in the list is what would have displayed front-most (i.e., the one you usually want).

Resources