how to attach overlay to detected object using android and OpenCV - android

I'm writing an android app using OpenCV for my masters that will be something like a game. The main goal is to a detect a car in selected area. The "prize" will be triggered randomly while detecting cars. When the user will hit the proper car I want to display a 3D object overlay on the screen and attach it to the middle of the car and keep it there so when the user will change the angle of his view on the car, the object will also be seen from diffrent angle.
at the moment I have EVERYTHING beside attaching the object. I've created detection, I'm drawing the 3D overlay, I've created functions that allow me to rotate the camera etc. BUT I do not have any clue how can I attach the overlay to the specific point. Cause I don't have this I have no point to recalculate the renderer to change the overlay perspective.
Please, I really need some help, even a small idea will be fine:
How can I attach the overlay to the specific in real world

(Sorry, I couldn't comment. Need at least 50 points to do that ... :P )
I assume your image of the car is coming from a camera feed and you are drawing 3d car in opengl. If so, then you can try this:
You set the pixel format of the opengl layer as RGBA_8888, so that you can set the background of the opengl camera as a transparent color.
You take a relative layout as layout of your activity.
first you add the opencv camera layout to it as full height and width.
then you add opengl layer as full height and width.
you get the position of the real car from opencv layer as pixel value or something you did.
then scale it to your opengl parameters so that you can draw it on the right spot.
it worked for me. hope it works for you too.

Related

How to render in Unity without the editor/scene/player being visible?

In my Android-Unity app I have some data such as text, text size, text coordinates etc that I will use to create the output and store it as a screenshot I will later use in my app.
But I need to make this happen when the user isn't seeing the Unity player/scene. So my question is that is it possible to render the contents and then take a screenshot of the same without the user seeing the Unity editor/player/scene whatever one may call it? Is there a way to do it in the background? Thanks!
I'm not sure exactly what you're trying to accomplish but I can clarify the use of a culling mask here. Here is what the scene looks like. I have embedded the view from Camera 2 into the bottom left of the game. You can see that Camera 2 is not displaying the floor. This is because I set it's culling to only respond to objects tagged with a custom layer here. Where as the Main Camera's culling mask is set to everything. Now, anything I tag with the "Custom" layer will be visible in the second camera and everything else would not be. I'm assuming what you want to do from here is to tag the things you want visible when you take a screenshot with a specific layer, then set the Culling Mask of your "Screenshot Camera" to that layer and take a screenshot with that Camera. That way you can specify what Objects/UI want visible in that camera.

Android game board with irregular board spaces

I'm creating an Android board game with several differently shaped board spaces (like Risk).
I want to be sure that my board appears correct and that OnTouchListeners stay in place on the GUI regardless of screen size/resolution.
Possible solutions I have thought of and their problems:
Create a single image for the board and assign OnTouchListeners based upon pixel geometry. Problem: If the user's display is a different resolution, my Listener might not be under the same pixels as my image (right?)
Create several ImageButtons and arrange them together. Problem: the ImageButtons might get rearranged based upon the display and I would end up with overlapping spaces or gaps.
Use Android custom drawing. If I do this, how would I link my Listeners to my Canvas and be sure that they are synced?
Basic question:
How to be sure that listeners sync with graphics in a GUI that uses irregular geometry?
I worked on an app with irregular touch areas so I can give you guidance on one way to achieve this.
Start with a single image for your entire board. This image is going to have a certain ("intrinsic") width and height regardless of any device resolutions.
Now here comes the tedious part. You (or maybe your graphic designer) will need to plot out coordinates of an irregular polygon for each touch area. These will be constants to your application.
When you are displaying your board, if you are zooming and panning on the image, you want to keep track of the transform matrix for the display. When the user touches the screen, you will get x,y coordinates from OnTouchListener and for those to be useful, you will have to "de-transform" the x,y to normalize it against the intrinsic dimensions of the board and your polygons.
We rolled our own hit-testing logic using an algorithm from http://alienryderflex.com/polygon/, but you can also try this: Create a Path out of your polygon coordinates (using moveTo(), lineTo(), and close()), then assign the Path to a Region using Region.setPath(). Once you have that, supposedly you should be able to hit-test using Region.contains(x,y), but I've never tried it so I can't guarantee that's going to work.

Android Draw a circle and detect bound of image

Now , i have the circle and image on the canvas
Ping-pong board and Ping-pong ball (draw by drawCircle)
The position of ball will depend on the accelerator
is possible to detect whether the ball the outside the board or not?
Or, i need to draw the board Programmatically without using the image
What you are looking for is called 'Collision Detection'. A technique used in game programming where the boundaries (area that defines where the object can be hit) are and then if an object enters those positions.
You can do this simply by saying that the boundaries are anything in the height / width of the image on the canvas. But I suspect that in your game you will want a subsection of that.
You will need to define a related object to your image that holds the 'Collision Boundary'. On a 2D game that will be the starting X,Y and then the height and width. While on a 3D game you will also need to store the Z position.
This is probably quite confusing to start with but I found you this little guide that explains it in more detail than I have space for here:
http://www.kilobolt.com/day-4-collision-detection-part-1.html
Let me know if you have any questions and the game sounds exciting!

Adding button on an OPENGL Screen

I have created a rotating CUBE with the help of OPENGL in Android now i want to add a listener to handle the rotation of the cube i.e. When i click button then CUBE must start rotating and vice versa..How can i add a button on an OPENGL Screen?
There are two options here:
Create a layout that includes both your GLSurfaceView and whatever buttons/settings you want. Use the standard OnClickListener for the button and have the listener call a method in your renderer to do the rotations. This is pretty much the easiest way to get this working, but the style of the button will change based on OS version and vendor. If you want to have more control over the button, you can try the other option.
Draw the button using OpenGL. After you render the cube, switch the projection matrix to an orthographic projection (glOrtho) with the same dimensions as your screen and draw a textured rectangle with your button image (You can also scale your button image like Android does by using 9 rectangles to keep the corners and edges sharp and just scale the inner part of the image which is usually a gradient or solid color). You'll have to set your renderer up to receive touch input and do a simple containment check that the pressed point is within the bounds of the button. If it is then you can call the method that handles rotation.
If you want a full-blown UI library, I'm sure there are several out there that exist for OpenGL ES on Android, and most of them would probably be part of larger game engines, which would handle most of the work for you. If you just want the single button though, I would recommend the first option as it's the simplest.
You can draw a Button object (rectangle) which remains always perpendicular to the camera for the
visual effect of a button. Everytime camera gets transformed the button object should also be relatively transformed.
The click part needs to be handled seperately thorough picking, i.e. you need to cast a ray from the centre of the camera and see if it intersects your 3D button object
Also if you were doing this on desktop you would have searched for "overlay GUI" libraries like
CEGUI which is quite good
http://www.cegui.org.uk/wiki/index.php/Main_Page
or else you could also use Navilibrary and SDL
http://princeofcode.com/navilibrary.php
In case of android you can explore the above libraries code (open source) and try to get the ideas from there.

Android: how to have an animation on top of a background image

What I'm trying to do is have a background image, for sake of simplicity, lets say it's a picture of the front of a house. Then, I want to have a red ball move from window to window.
**I want to have a background picture, and a picture on top of it.
**I then want to be able to tell the top picture EXACTLY where to go.
How can I do this?
I'm just beginning to learn about animations in Android, and have not yet run across any way to do this.
There are two routes to animation in android: Canvas and OpenGL ES.
I would recommend OpenGL for anything requiring smoothness and speed, like a moving ball.
You should create a view using the helper class GLSurfaceView
http://android-developers.blogspot.com/2009/04/introducing-glsurfaceview.html, and implement a Renderer.
I assume you have the images saved in your res/drawable folders, in a format like png and the ball file contains an alpha channel.
You can see many tutorials online, but basically you need to load your background image and your ball resource at onSurfaceCreated and store it in a texture using GLUtils.texImage2D.
In the onDrawFrame method, you should set up a 2D projection such as glOrtho2D, then draw the background.
Then just before you draw the ball texture, you can use the glTranslate(x,y,0) function to move the ball over the house. Use an alpha blend for the ball:
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ONE_MINUS_ALPHA);
glEnable(GL_BLEND);
Unfortunately writing in OpenGL isn't as straightforward as you might hope. Everything is done with 3D coordinates, despite the fact you want only a 2D image. But hopefully this gives you enough info to google for good exmaples, which are abundant!

Categories

Resources