Ask Your Question
0

remap android

asked 2016-10-25 04:49:59 -0500

pizza gravatar image

updated 2016-10-25 10:05:06 -0500

hi, New here. I'm following your documentation in android platform with opencv4android. One line of the code there is:

map_x.at<float>(j,i) = 2*( i - src.cols*0.25 ) + 0.5 ;

and my question is: 1 there's no method of map_x.at<float>(j,i) and map_x.get(j,i) will return double[]. 2 map_x.get(j,i) is an array, then why map_x.at<float>(j,i) is one value not an array? 3 how can I translate it to java version?

I've google a lot of related answers, please help. Thank you.

edit:

My code is like this ,just translate the code from documentation to android:

Utils.bitmapToMat(originalBitmap, inMat);

int rows = inMat.rows();
int cols = inMat.cols();
Mat mapX = new Mat(rows, cols, CvType.CV_32FC1);
Mat mapY = new Mat(rows, cols, CvType.CV_32FC1);

for (int j = 0; j < rows; j++) {
  for (int i = 0; i < cols; i++) {
        if (i > cols * 0.25 && i < cols * 0.75 && j > rows * 0.25 && j < rows * 0.75) {
          float[] pixelsX = new float[rows * cols];
          float[] pixelsY = Arrays.copyOf(pixelsX, pixelsX.length);
          pixelsX[j * cols + i] = (float) (2 * (i - cols * 0.25) + 0.5);
          pixelsY[j * cols + i] = (float) (2 * (i - rows * 0.25) + 0.5);
          mapX.put(j, i, pixelsX);
          mapY.put(j, i, pixelsY);
        }
  }
}

Imgproc.remap(inMat, outMat, mapX, mapY, CV_INTER_LINEAR, BORDER_CONSTANT, new Scalar(0, 0, 0));
Utils.matToBitmap(outMat, bitmap);

It didn't show the image well, where go wrong?

edit 2:

still not good:

int rows = inMat.rows();
int cols = inMat.cols();
float[] pixelsX = new float[rows * cols];
float[] pixelsY = new float[rows * cols];

for (int j = 0; j < rows; j++) {
  for (int i = 0; i < cols; i++) {
        if (i > cols * 0.25 && i < cols * 0.75 && j > rows * 0.25 && j < rows * 0.75) {
          pixelsX[j * cols + i] = (float) (2 * (i - cols * 0.25) + 0.5);
          pixelsY[j * cols + i] = (float) (2 * (i - rows * 0.25) + 0.5);
        }else {
        //default float value should be 0, but no harm.
          pixelsX[j * cols + i] = 0;
          pixelsY[j * cols + i] = 0;
        }
  }
}

Mat mapX = new Mat(rows, cols, CvType.CV_32FC1);
Mat mapY = new Mat(rows, cols, CvType.CV_32FC1);
mapX.put(0, 0, pixelsX); //why 0 row 0 col?
mapY.put(0, 0, pixelsY);

Imgproc.remap(inMat, outMat, mapX, mapY, CV_INTER_LINEAR, BORDER_CONSTANT, new Scalar(0, 0, 0));

original pic: image description

after zooming pic: image description

edit retag flag offensive close merge delete

Comments

1

try the other cases from the docs page. (formula for case 0 just might not work for your situation)

(you're not a dog, and your head is not in the same position as in the hardcoded example there)

berak gravatar imageberak ( 2016-10-25 10:11:42 -0500 )edit

I tried the dog, still same as my pic. Upside down , left-right reverse and both way operation will cause a black line at the top or left.

pizza gravatar imagepizza ( 2016-10-25 10:35:25 -0500 )edit

again, no idea, where that formula came from, and what it intended to show.

once , case 1 or 2 works for you (a simple "flip" operation), you're good to go for your own experiments

berak gravatar imageberak ( 2016-10-25 10:50:53 -0500 )edit

alright, I'll keep trying. sure you've heard a lot but ,again, thanks.

pizza gravatar imagepizza ( 2016-10-25 19:46:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-10-25 05:06:02 -0500

berak gravatar image

updated 2016-10-25 09:40:08 -0500

if using java, please avoid to useget() or set() on a per-pixel basis in general (far too slow.) instead:

// get *all* pixels  at once:
float [] pixels = new float[w*h];
mapx.get(0,0, pixels);
// manipulate pixels:
pixels[y*w+x] = 17;
// write them back:
mapx.put(0,0, pixels);

[edit]

(it seems, i was not clear enough about the all pixels part):

int rows = inMat.rows();
int cols = inMat.cols();

float[] pixelsX = new float[rows * cols];
float[] pixelsY = new float[rows * cols];
for (int j = 0; j < rows; j++) {
  for (int i = 0; i < cols; i++) {
        if (i > cols * 0.25 && i < cols * 0.75 && j > rows * 0.25 && j < rows * 0.75) {
          pixelsX[j * cols + i] = (float) (2 * (i - cols * 0.25) + 0.5);
          pixelsY[j * cols + i] = (float) (2 * (i - rows * 0.25) + 0.5);
        }
        // hmm, how does java handle uninitialized memory here ?
        // you might need an "else" clause, since above code leaves "holes" in the map.
   }
}

Mat mapX = new Mat(rows, cols, CvType.CV_32FC1);
Mat mapY = new Mat(rows, cols, CvType.CV_32FC1);
mapX.put(0,0, pixelsX);
mapY.put(0,0, pixelsY);
edit flag offensive delete link more

Comments

Thank for your help. After your guide, I change the code but it didn't work well and I edit my question. Could you see it please?

pizza gravatar imagepizza ( 2016-10-25 09:24:45 -0500 )edit

^^ problem is, that you're still trying to do get/put per-pixel

berak gravatar imageberak ( 2016-10-25 09:54:17 -0500 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2016-10-25 04:49:59 -0500

Seen: 226 times

Last updated: Oct 25 '16