Topic locked

Get Trackable Angle

March 21, 2012 - 4:33am #1

Hi, I would like to change the position of some textview I've put over the camera view dynamically with the position of the trackable in it. However, I can't find any method in Vuforia API which provide me the ange of the trackable. I try to get the top-left corner and top-right coordinates to work out by myself, but I couldn't find. Can you help me? Thanks for any help, Cesar.

Get Trackable Angle

October 23, 2015 - 10:25pm #8

this code solve my problem!!
Thank You :))

Get Trackable Angle

December 11, 2012 - 11:31pm #7

Kim! I just would like to thank you for this post. I've been looking for days (ToT) on how to get the coordinates of the four corners of the trackable. Your other posts have helped me greatly in the past as well, so I would just like to express my gratitude via this short post.

Re: Get Trackable Angle

April 13, 2012 - 3:26am #6

Ok, I think I've fixed the position problem. I've made another algorithm more complex. I will ask again if I cna't fix the other one, refreshing one.

Thank,
Cesar

Re: Get Trackable Angle

April 13, 2012 - 1:15am #5

I want to put some textviews in diferent position of the trackable, no the screen. For example, in a photo of me and my friends, which I convert to trackable, I want to add our names just in our faces with previous coordinates I gave in a file, not recognizing our faces. But it only work if I see the image just from the front.

Yes, I'd tried passing up the four corners and rendering 4 textview there and the corner are right, but for other point it didn't worked.

Re: Get Trackable Angle

April 12, 2012 - 7:49pm #4

I'm not sure what you're trying to do with the angle and width and height here. Have you tried passing up the four corners to Java and rendering something at each corner to verify that that much is correct? When I tested this I used a canvas to draw lines between the corners to outline the target.

You can force a view to be redrawn using the invalidate() method.

- Kim

Re: Get Trackable Angle

April 12, 2012 - 9:25am #3

Hi again.
I'd been having troubles with this part. With the code you gave me I was able to positioned my TexViews on the coordinates only if I was watching the trackable just from the front, if I move the position were they appear were wrong. What is more, the textview didn't reposition dinamically, it only appear with the first values.

Here are the steeps I follow:
First I take the parameters from ImageTarget.cpp wich I will use and I send to ImageTargetRenderer:

Quote:

float angle = atan((v2.data[1]-v1.data[1])/(v2.data[0]-v1.data[0]));
float height = sqrt(pow((v3.data[0]-v1.data[0]),2)+pow((v3.data[1]-v1.data[1]),2));
float width = sqrt(pow((v2.data[0]-v1.data[0]),2)+pow((v2.data[1]-v1.data[1]),2));
float param[5] = {width,height,angle,v1.data[0],v1.data[1]};

jclass javaClass = env->GetObjectClass(obj);
jfloatArray jf = env->NewFloatArray(5);
env->SetFloatArrayRegion(jf,0,5,param);
jmethodID method = env->GetMethodID(javaClass, "setParam", "([F)V");
env->CallVoidMethod(obj, method,jf);

Then I save them in a new variable:

Quote:

static float[] coord = new float[5]; //Altura, anchura y angulo
//Guardado y retorno de coordenadas
public void setParam(float[] cord){
for(int i=0; i coord[i] = cord[i];
}
Message message = new Message();
message.obj = "new_cord";
mainActivityHandler.sendMessage(message);
}
public static float[] getParam(){
return coord;
}

Finally I call the method from ImageTarget.java everytime I find a trackable to refresh the coordinates:

Quote:

protected int[] coordenadas(float cX, float cY){
int[] cord = {0,0};
float[] param = ImageTargetsRenderer.getParam();
cord[0] = (int)(param[3] + (param[0]*cX/100.0* Math.cos(param[2]) + (param[1]*cY/100.0* Math.sin(param[2]))));
cord[1] = (int)(param[4] + (param[1]*cY/100.0* Math.cos(param[2]) - (param[0]*cX/100.0* Math.sin(param[2]))));
return cord;
}
...
ImageTargetsRenderer.mainActivityHandler = new Handler() {
int[][] carteles = new int[10][3]; //Relative Id;coordinate X, coordinate Y
int[] cord;
@Override
public void handleMessage(Message msg) {
...
else if( text == "new_cord"){
int j=0;
while(carteles[j][0] != 0){
RelativeLayout rela = (RelativeLayout) findViewById(carteles[j][0]);
cord = coordenadas(carteles[j][1],carteles[j][2]);
rela.setPadding(cord[0],cord[1],0,0);
j++;
}
}

Thanks
Cesar

Re: Get Trackable Angle

March 21, 2012 - 6:44pm #2

Here's some code to get the four corners of the trackable in screen coordinates:

const QCAR::ImageTarget* imageTarget = static_cast<const QCAR::ImageTarget*>(trackable);
QCAR::Vec2F targetSize = imageTarget->getSize();
float halfWidth = targetSize.data[0] / 2.0f;
float halfHeight = targetSize.data[1] / 2.0f;

QCAR::Vec2F v1 = QCAR::Tool::projectPoint(cameraCalibration, trackable->getPose(), QCAR::Vec3F(-halfWidth, halfHeight, 0));
QCAR::Vec2F v2 = QCAR::Tool::projectPoint(cameraCalibration, trackable->getPose(), QCAR::Vec3F(halfWidth, halfHeight, 0));
QCAR::Vec2F v3 = QCAR::Tool::projectPoint(cameraCalibration, trackable->getPose(), QCAR::Vec3F(halfWidth, -halfHeight, 0));
QCAR::Vec2F v4 = QCAR::Tool::projectPoint(cameraCalibration, trackable->getPose(), QCAR::Vec3F(-halfWidth, -halfHeight, 0));

v1 = cameraPointToScreenPoint(v1);
v2 = cameraPointToScreenPoint(v2);
v3 = cameraPointToScreenPoint(v3);
v4 = cameraPointToScreenPoint(v4);

Note the projectPoint method returns the point in camera coordinates, so you need an extra step to take this to screen coordinates:

QCAR::Vec2F cameraPointToScreenPoint(QCAR::Vec2F cameraPoint)
{
    QCAR::VideoMode videoMode = QCAR::CameraDevice::getInstance().getVideoMode(QCAR::CameraDevice::MODE_DEFAULT);
    QCAR::VideoBackgroundConfig config = QCAR::Renderer::getInstance().getVideoBackgroundConfig();
    
    int xOffset = ((int) screenWidth - config.mSize.data[0]) / 2.0f + config.mPosition.data[0];
    int yOffset = ((int) screenHeight - config.mSize.data[1]) / 2.0f - config.mPosition.data[1];
    
    if (isActivityInPortraitMode)
    {
        // camera image is rotated 90 degrees
        int rotatedX = videoMode.mHeight - cameraPoint.data[1];
        int rotatedY = cameraPoint.data[0];
        
        return QCAR::Vec2F(rotatedX * config.mSize.data[0] / (float) videoMode.mHeight + xOffset,
                           rotatedY * config.mSize.data[1] / (float) videoMode.mWidth + yOffset);
    }
    else
    {
        return QCAR::Vec2F(cameraPoint.data[0] * config.mSize.data[0] / (float) videoMode.mWidth + xOffset,
                           cameraPoint.data[1] * config.mSize.data[1] / (float) videoMode.mHeight + yOffset);
    }
}

- Kim

Topic locked