Implemented the calibration interface.
This commit is contained in:
@@ -23,13 +23,14 @@
|
|||||||
|
|
||||||
#ifdef CAN_LOG
|
#ifdef CAN_LOG
|
||||||
#define log(TAG, MSG) (__android_log_write(ANDROID_LOG_DEBUG, TAG, MSG))
|
#define log(TAG, MSG) (__android_log_write(ANDROID_LOG_DEBUG, TAG, MSG))
|
||||||
const char * TAG = "CVPROC_NATIVE";
|
|
||||||
#else
|
#else
|
||||||
#define log(TAG, MSG) (1 + 1)
|
#define log(TAG, MSG) (1 + 1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern "C"{
|
extern "C"{
|
||||||
|
|
||||||
|
const char * TAG = "CVPROC_NATIVE";
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_ve_ucv_ciens_ccg_nxtar_MainActivity_getMarkerCodesAndLocations(JNIEnv* env, jobject jobj, jlong addrMatIn, jlong addrMatOut, jintArray codes){
|
JNIEXPORT void JNICALL Java_ve_ucv_ciens_ccg_nxtar_MainActivity_getMarkerCodesAndLocations(JNIEnv* env, jobject jobj, jlong addrMatIn, jlong addrMatOut, jintArray codes){
|
||||||
char codeMsg[128];
|
char codeMsg[128];
|
||||||
std::vector<int> vCodes;
|
std::vector<int> vCodes;
|
||||||
@@ -39,12 +40,13 @@ JNIEXPORT void JNICALL Java_ve_ucv_ciens_ccg_nxtar_MainActivity_getMarkerCodesAn
|
|||||||
cv::Mat& myuv = *(cv::Mat*)addrMatIn;
|
cv::Mat& myuv = *(cv::Mat*)addrMatIn;
|
||||||
cv::Mat& mbgr = *(cv::Mat*)addrMatOut;
|
cv::Mat& mbgr = *(cv::Mat*)addrMatOut;
|
||||||
jint * _codes = env->GetIntArrayElements(codes, 0);
|
jint * _codes = env->GetIntArrayElements(codes, 0);
|
||||||
|
cv::Mat temp;
|
||||||
|
|
||||||
log(TAG, "getMarkerCodesAndLocations(): Converting color space before processing.");
|
log(TAG, "getMarkerCodesAndLocations(): Converting color space before processing.");
|
||||||
cv::cvtColor(myuv, mbgr, CV_RGB2BGR);
|
cv::cvtColor(myuv, temp, CV_RGB2BGR);
|
||||||
|
|
||||||
log(TAG, "getMarkerCodesAndLocations(): Finding markers.");
|
log(TAG, "getMarkerCodesAndLocations(): Finding markers.");
|
||||||
nxtar::getAllMarkers(vCodes, mbgr);
|
nxtar::getAllMarkers(vCodes, temp);
|
||||||
|
|
||||||
log(TAG, "getMarkerCodesAndLocations(): Copying marker codes.");
|
log(TAG, "getMarkerCodesAndLocations(): Copying marker codes.");
|
||||||
for(int i = 0; i < vCodes.size() && i < 15; i++){
|
for(int i = 0; i < vCodes.size() && i < 15; i++){
|
||||||
@@ -52,8 +54,9 @@ JNIEXPORT void JNICALL Java_ve_ucv_ciens_ccg_nxtar_MainActivity_getMarkerCodesAn
|
|||||||
}
|
}
|
||||||
vCodes.clear();
|
vCodes.clear();
|
||||||
|
|
||||||
log(TAG, "getMarkerCodesAndLocations(): Releasing native data.");
|
cv::cvtColor(temp, mbgr, CV_BGR2RGB);
|
||||||
|
|
||||||
|
log(TAG, "getMarkerCodesAndLocations(): Releasing native data.");
|
||||||
env->ReleaseIntArrayElements(codes, _codes, 0);
|
env->ReleaseIntArrayElements(codes, _codes, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,12 +69,13 @@ JNIEXPORT jboolean JNICALL Java_ve_ucv_ciens_ccg_nxtar_MainActivity_findCalibrat
|
|||||||
cv::Mat& myuv = *(cv::Mat*)addrMatIn;
|
cv::Mat& myuv = *(cv::Mat*)addrMatIn;
|
||||||
cv::Mat& mbgr = *(cv::Mat*)addrMatOut;
|
cv::Mat& mbgr = *(cv::Mat*)addrMatOut;
|
||||||
jfloat * _points = env->GetFloatArrayElements(points, 0);
|
jfloat * _points = env->GetFloatArrayElements(points, 0);
|
||||||
|
cv::Mat temp;
|
||||||
|
|
||||||
log(TAG, "findCalibrationPattern(): Converting color space before processing.");
|
log(TAG, "findCalibrationPattern(): Converting color space before processing.");
|
||||||
cv::cvtColor(myuv, mbgr, CV_RGB2BGR);
|
cv::cvtColor(myuv, temp, CV_RGB2BGR);
|
||||||
|
|
||||||
log(TAG, "findCalibrationPattern(): Finding calibration pattern.");
|
log(TAG, "findCalibrationPattern(): Finding calibration pattern.");
|
||||||
found = nxtar::findCalibrationPattern(v_points, mbgr);
|
found = nxtar::findCalibrationPattern(v_points, temp);
|
||||||
|
|
||||||
log(TAG, "findCalibrationPattern(): Copying calibration points.");
|
log(TAG, "findCalibrationPattern(): Copying calibration points.");
|
||||||
for(size_t i = 0, p = 0; i < v_points.size(); i++, p += 2){
|
for(size_t i = 0, p = 0; i < v_points.size(); i++, p += 2){
|
||||||
@@ -79,6 +83,8 @@ JNIEXPORT jboolean JNICALL Java_ve_ucv_ciens_ccg_nxtar_MainActivity_findCalibrat
|
|||||||
_points[p + 1] = (jfloat)v_points[i].y;
|
_points[p + 1] = (jfloat)v_points[i].y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cv::cvtColor(temp, mbgr, CV_BGR2RGB);
|
||||||
|
|
||||||
log(TAG, "findCalibrationPattern(): Releasing native data.");
|
log(TAG, "findCalibrationPattern(): Releasing native data.");
|
||||||
env->ReleaseFloatArrayElements(points, _points, 0);
|
env->ReleaseFloatArrayElements(points, _points, 0);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.opencv.imgproc.Imgproc;
|
|||||||
|
|
||||||
import ve.ucv.ciens.ccg.nxtar.interfaces.CVProcessor;
|
import ve.ucv.ciens.ccg.nxtar.interfaces.CVProcessor;
|
||||||
import ve.ucv.ciens.ccg.nxtar.interfaces.OSFunctionalityProvider;
|
import ve.ucv.ciens.ccg.nxtar.interfaces.OSFunctionalityProvider;
|
||||||
|
import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ActivityInfo;
|
import android.content.pm.ActivityInfo;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
@@ -45,6 +46,7 @@ public class MainActivity extends AndroidApplication implements OSFunctionalityP
|
|||||||
private static final String CLASS_NAME = MainActivity.class.getSimpleName();
|
private static final String CLASS_NAME = MainActivity.class.getSimpleName();
|
||||||
|
|
||||||
private static boolean ocvOn = false;
|
private static boolean ocvOn = false;
|
||||||
|
private static Mat cameraMatrix, distortionCoeffs;
|
||||||
|
|
||||||
private WifiManager wifiManager;
|
private WifiManager wifiManager;
|
||||||
private MulticastLock multicastLock;
|
private MulticastLock multicastLock;
|
||||||
@@ -71,6 +73,9 @@ public class MainActivity extends AndroidApplication implements OSFunctionalityP
|
|||||||
public void onCreate(Bundle savedInstanceState){
|
public void onCreate(Bundle savedInstanceState){
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
cameraMatrix = new Mat();
|
||||||
|
distortionCoeffs = new Mat();
|
||||||
|
|
||||||
if(!Ouya.runningOnOuya){
|
if(!Ouya.runningOnOuya){
|
||||||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
||||||
}else{
|
}else{
|
||||||
@@ -134,7 +139,7 @@ public class MainActivity extends AndroidApplication implements OSFunctionalityP
|
|||||||
// CVProcessor interface methods. //
|
// CVProcessor interface methods. //
|
||||||
////////////////////////////////////
|
////////////////////////////////////
|
||||||
@Override
|
@Override
|
||||||
public CVData findMarkersInFrame(byte[] frame, int w, int h) {
|
public CVMarkerData findMarkersInFrame(byte[] frame){
|
||||||
if(ocvOn){
|
if(ocvOn){
|
||||||
int codes[] = new int[15];
|
int codes[] = new int[15];
|
||||||
Bitmap tFrame, mFrame;
|
Bitmap tFrame, mFrame;
|
||||||
@@ -147,14 +152,14 @@ public class MainActivity extends AndroidApplication implements OSFunctionalityP
|
|||||||
|
|
||||||
getMarkerCodesAndLocations(inImg.getNativeObjAddr(), outImg.getNativeObjAddr(), codes);
|
getMarkerCodesAndLocations(inImg.getNativeObjAddr(), outImg.getNativeObjAddr(), codes);
|
||||||
|
|
||||||
Mat temp = new Mat();
|
//Mat temp = new Mat();
|
||||||
Imgproc.cvtColor(outImg, temp, Imgproc.COLOR_BGR2RGB);
|
//Imgproc.cvtColor(outImg, temp, Imgproc.COLOR_BGR2RGB);
|
||||||
|
|
||||||
mFrame = Bitmap.createBitmap(temp.cols(), temp.rows(), Bitmap.Config.RGB_565);
|
mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
|
||||||
Utils.matToBitmap(temp, mFrame);
|
Utils.matToBitmap(outImg, mFrame);
|
||||||
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
||||||
|
|
||||||
CVData data = new CVData();
|
CVMarkerData data = new CVMarkerData();
|
||||||
data.outFrame = outputStream.toByteArray();
|
data.outFrame = outputStream.toByteArray();
|
||||||
data.markerCodes = codes;
|
data.markerCodes = codes;
|
||||||
|
|
||||||
@@ -164,15 +169,78 @@ public class MainActivity extends AndroidApplication implements OSFunctionalityP
|
|||||||
|
|
||||||
return data;
|
return data;
|
||||||
}else{
|
}else{
|
||||||
Gdx.app.debug(TAG, CLASS_NAME + ".processFrame(): OpenCV is not ready or failed to load.");
|
Gdx.app.debug(TAG, CLASS_NAME + ".findMarkersInFrame(): OpenCV is not ready or failed to load.");
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void calibrateCamera() {
|
public CVCalibrationData findCalibrationPattern(byte[] frame){
|
||||||
// TODO Auto-generated method stub
|
if(ocvOn){
|
||||||
|
boolean found;
|
||||||
|
float points[] = new float[ProjectConstants.CALIBRATION_PATTERN_POINTS * 2];
|
||||||
|
Bitmap tFrame, mFrame;
|
||||||
|
Mat inImg = new Mat(), outImg = new Mat();
|
||||||
|
CVCalibrationData data = new CVCalibrationData();
|
||||||
|
|
||||||
|
// Decode the input frame and convert it to an OpenCV Matrix.
|
||||||
|
tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
|
||||||
|
Utils.bitmapToMat(tFrame, inImg);
|
||||||
|
|
||||||
|
// Attempt to find the calibration pattern in the input frame.
|
||||||
|
found = findCalibrationPattern(inImg.getNativeObjAddr(), outImg.getNativeObjAddr(), points);
|
||||||
|
|
||||||
|
// Encode the output image as a JPEG image.
|
||||||
|
mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
|
||||||
|
Utils.matToBitmap(outImg, mFrame);
|
||||||
|
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
||||||
|
|
||||||
|
// Prepare the output data structure.
|
||||||
|
data.outFrame = outputStream.toByteArray();
|
||||||
|
data.calibrationPoints = found ? points : null;
|
||||||
|
|
||||||
|
// Clean up memory.
|
||||||
|
tFrame.recycle();
|
||||||
|
mFrame.recycle();
|
||||||
|
outputStream.reset();
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}else{
|
||||||
|
Gdx.app.debug(TAG, CLASS_NAME + ".findCalibrationPattern(): OpenCV is not ready or failed to load.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public byte[] undistortFrame(byte[] frame){
|
||||||
|
if(ocvOn){
|
||||||
|
byte undistortedFrame[];
|
||||||
|
Bitmap tFrame, mFrame;
|
||||||
|
Mat inImg = new Mat(), outImg = new Mat();
|
||||||
|
|
||||||
|
// Decode the input frame and convert it to an OpenCV Matrix.
|
||||||
|
tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
|
||||||
|
Utils.bitmapToMat(tFrame, inImg);
|
||||||
|
|
||||||
|
// Apply the undistort correction to the input frame.
|
||||||
|
Imgproc.undistort(inImg, outImg, cameraMatrix, distortionCoeffs);
|
||||||
|
|
||||||
|
// Encode the output image as a JPEG image.
|
||||||
|
mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
|
||||||
|
Utils.matToBitmap(outImg, mFrame);
|
||||||
|
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
||||||
|
|
||||||
|
// Prepare the return frame.
|
||||||
|
undistortedFrame = outputStream.toByteArray();
|
||||||
|
|
||||||
|
// Clean up memory.
|
||||||
|
tFrame.recycle();
|
||||||
|
mFrame.recycle();
|
||||||
|
outputStream.reset();
|
||||||
|
|
||||||
|
return undistortedFrame;
|
||||||
|
}else{
|
||||||
|
Gdx.app.debug(TAG, CLASS_NAME + ".undistortFrame(): OpenCV is not ready or failed to load.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user