Home Memory issue with Android OpenCV application
Reply: 0

Memory issue with Android OpenCV application

user10046 Published in September 21, 2018, 2:55 am

I want to load 5 images and convert them into Mat floats for openCV processing. I'm using the Android NDK by the way, and this is the C++ part of the code.

My code below works fine on the Android emulator, but it stops when it gets to the float conversions when testing on my device (marked below), which leads me to think that it's a memory issue.

I'm new to managing memory with Android so I'd like to know how I'd go about fixing this issue.

//Obtain training image 1
AAsset *trainOne = AAssetManager_open(mgr, "1.JPG", AASSET_MODE_UNKNOWN);
long sizeOfTrainOne = AAsset_getLength(trainOne);
char *bufferTrainOne = (char *) AAsset_getBuffer(trainOne);
std::vector<char> trainOneData(bufferTrainOne, bufferTrainOne + sizeOfTrainOne);
cv::Mat trainOneMat = cv::imdecode(trainOneData, IMREAD_UNCHANGED);

//Obtain training image 2
AAsset *trainTwo = AAssetManager_open(mgr, "2.JPG", AASSET_MODE_UNKNOWN);
long sizeOfTrainTwo = AAsset_getLength(trainTwo);
char *bufferTrainTwo = (char *) AAsset_getBuffer(trainTwo);
std::vector<char> trainTwoData(bufferTrainTwo, bufferTrainTwo + sizeOfTrainTwo);
cv::Mat trainTwoMat = cv::imdecode(trainTwoData, IMREAD_UNCHANGED);

//Obtain training image 3
AAsset *trainThree = AAssetManager_open(mgr, "3.JPG", AASSET_MODE_UNKNOWN);
long sizeOfTrainThree = AAsset_getLength(trainThree);
char *bufferTrainThree = (char *) AAsset_getBuffer(trainThree);
std::vector<char> trainThreeData(bufferTrainThree, bufferTrainThree + sizeOfTrainThree);
cv::Mat trainThreeMat = cv::imdecode(trainThreeData, IMREAD_UNCHANGED);

//Obtain training image 4
AAsset *trainFour = AAssetManager_open(mgr, "c1.JPG", AASSET_MODE_UNKNOWN);
long sizeOfTrainFour = AAsset_getLength(trainFour);
char *bufferTrainFour = (char *) AAsset_getBuffer(trainFour);
std::vector<char> trainFourData(bufferTrainFour, bufferTrainFour + sizeOfTrainFour);
cv::Mat trainFourMat = cv::imdecode(trainFourData, IMREAD_UNCHANGED);

//Obtain training image 5 
AAsset *trainFive = AAssetManager_open(mgr, "c2.JPG", AASSET_MODE_UNKNOWN);
long sizeOfTrainFive = AAsset_getLength(trainFive);
char *bufferTrainFive = (char *) AAsset_getBuffer(trainFive);
std::vector<char> trainFiveData(bufferTrainFive, bufferTrainFive + sizeOfTrainFive);
cv::Mat trainFiveMat = cv::imdecode(trainFiveData, IMREAD_UNCHANGED);

//Change all mats into floats
cv::Mat float1;
cv::Mat float2;
cv::Mat float3;
cv::Mat float4;
cv::Mat float5;

trainOneMat.convertTo(float1, CV_32FC1);
trainTwoMat.convertTo(float2, CV_32FC1);  <--------- stops here
trainThreeMat.convertTo(float3, CV_32FC1);
trainFourMat.convertTo(float4, CV_32FC1);
trainFiveMat.convertTo(float5, CV_32FC1);

//Combine into training Mat
train_data.push_back(float1.reshape(1, 1));
train_data.push_back(float2.reshape(1, 1));
train_data.push_back(float3.reshape(1, 1));
train_data.push_back(float4.reshape(1, 1));
train_data.push_back(float5.reshape(1, 1));

float labelOne = 1;
float labelTwo = 1;
float labelThree = 1;
float labelFour = 2;
float labelFive = 2;


knn->train(train_data, ml::ROW_SAMPLE, train_label); <-------

jintArray resultImage;
return resultImage;
share|improve this question
  • 1. There should be stacktrace or error code or both in logcat (you would probably need to set filter from "Show only selected application" to "No filter"). 2. What happens if you comment everything except for code related to trainTwoMat and its conversion? – Dmitrii Z. Jan 11 at 21:59
  • When I comment everything, it gets to the knn->train part (I've extended my question and included more of my code, indicating where it now stops). I'm not sure where to begin with memory issues. – Ber12345 Jan 12 at 2:06
  • @DmitriiZ. I've added another question containing all of my code with a better description: stackoverflow.com/questions/48224874/… – Ber12345 Jan 12 at 11:00

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323412 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO