Forums - Problem with Image Rectification

2 posts / 0 new
Last post
Problem with Image Rectification
fastcvandroidapp
Join Date: 20 Oct 12
Posts: 3
Posted: Fri, 2012-12-28 07:11

Hello to all,

I need some help to use some functions of image transformation. My objective is to rectify the paper in this photo (http://dl.dropbox.com/u/28086295/test3.jpg), but i can't do it. The code is the following:

RectifyImage.h

#ifndef IMAGEPROCESSING_H
#define IMAGEPROCESSING_H

#include <jni.h>

extern "C" {

JNIEXPORT void JNICALL Java_com_example_rectifyimage_ImageProcessing_applyRectificationNative
(
    JNIEnv* env,
    jobject obj,
    jintArray src,
    jbooleanArray dst,
    jint width,
    jint height
);

void convertRGBtoYUV(jboolean * yuv420p, jint * argb, jint width, jint height);
}
#endif



RectifyImage.cpp
#include "RectifyImage.h"

#include <stdlib.h>
#include <fastcv.h>
#include <android/log.h>
#include <math.h>

#define LOG_TAG    "RectifyImage.cpp"

#define DPRINTF(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define IPRINTF(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define EPRINTF(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

JNIEXPORT void JNICALL Java_com_example_rectifyimage_ImageProcessing_applyRectificationNative
(
    JNIEnv* env,
    jobject obj,
    jintArray src,
    jbooleanArray dst,
    jint width,
    jint height
)
{
    jboolean yuvData[width * height * 3 / 2];
    jint* srcData = NULL;
    jboolean* dstData = NULL;
    jboolean isCopy = 0;
   
    jint stride = width;
    jint frameSize = width*height;
    jfloat projMatrix[];
    jfloat srcPoint[]={77.0f, 241.0f, 327.0f, 43.0f, 490.0f, 183.0f, 227.0f, 425.0f};
    jfloat dstPoint[]={0.0f, 0.0f, 297.0f, 0.0f, 297.0f, 210.0f, 0.0f, 210.0f};
   
    srcData = env->GetIntArrayElements(src, &isCopy);
    dstData = env->GetBooleanArrayElements(dst, &isCopy);

    convertRGBtoYUV(yuvData, srcData, width, height);
   
    fcvGetPerspectiveTransformf32(srcPoint, dstPoint, projMatrix);
    fcvWarpPerspectiveu8_v2(yuvData, width, height, width, dstData, width, height, width, projMatrix);
   
    // copy U,V data that are not used by the filters
    memcpy(dstData+frameSize, yuvData+frameSize, frameSize/2);
   
    env->ReleaseIntArrayElements(src, srcData, JNI_ABORT);
    env->ReleaseBooleanArrayElements(dst, dstData, JNI_ABORT);
}

void convertRGBtoYUV(jboolean * yuv420p, jint * argb, jint width, jint height)
{
    int frameSize = width * height;

    int yIndex = 0;
    int uvIndex = frameSize;

    int R, G, B, Y, U, V;
    int index = 0;
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            // a = (argb[index] & 0xff000000) >> 24;
            R = (argb[index] & 0xff0000) >> 16;
            G = (argb[index] & 0xff00) >> 8;
            B = (argb[index] & 0xff) >> 0;

            Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
            U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
            V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

            yuv420p[yIndex++] = (jboolean) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
            if (j % 2 == 0 && index % 2 == 0) {
                yuv420p[uvIndex++] = (jboolean) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
                yuv420p[uvIndex++] = (jboolean) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
            }

            index++;
        }
    }
}



I'm sure that transformation from RGB to YUV is working, because i've used it in another piece of code. The part that does not work is the rectification. I obtain an image totally different from image that i would like to. Moreover the projection matrix calculated with matlab is totally different from the projection matrix obtained with fcvGetPerspectiveTransformf32. It's normal?
I've also tried to use the matlab projection matrix with the function fcvWarpPerspectiveu8_v2 but i obtain a black image.

Someone can help me?
Thanks a lot in advance!

  • Up0
  • Down0
jeff4s Moderator
Join Date: 4 Nov 12
Posts: 98
Posted: Fri, 2013-02-08 00:51
The problem could be due to a bug in fcvGetPerspectiveTransformf32, which is fixed in FastCV 1.2.0 release. Sorry about the trouble. The new release should come out in any moment now.
  • Up0
  • Down0
or Register

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.