Reputation: 626
I am reading an image in Android and I want to access the pixel values of the image.
I am reading the images like this
Mat img1 = new Mat();
img1 = Highgui.imread(filePath1);
Is there an efficient way to convert the Mat object to a float array?
Currently, I am doing it like this & this is very slow.
private float[][] convertToFloatArray(String filePath1){
img1 = Highgui.imread(filePath1);
String output = img1.dump();
float[][] flt = new float[img1.rows()][img1.cols()];
// slowest step but necessary to remove [, ;, etc.
String array1[] = output.split("[^0-9]+");
int k = 0;
for(int i = 0;i<img1.rows();i++) {
for(int j = 0;j<img1.cols();j++) {
flt[i][j] = (Float.valueOf(array1[k])).floatValue();
k += 1;
}
}
Upvotes: 1
Views: 2665
Reputation: 626
Was able to figure out a solution
img1 = new Mat();
img1 = Highgui.imread(filePath1, 0);
int buff[] = new int[(int)img1.total() * img1.channels()];
img1.get(0, 0, buff);
return buff;
Upvotes: 1
Reputation: 238
You mentioned int array would be fine. You can skip the string float and string lines you are using above and just use this:
int k = 0;
for(int i = 0;i<img1.rows();i++) {
for(int j = 0;j<img1.cols();j++) {
int_arr[i][j] = static_cast<unsigned>(img1.data[k]);
k += 1;
}
}
Also keep track of how many channels you have.
Upvotes: 0