2

I am looking for a way to overlay an image in openCV(2.4.3) for Android. Basically, I am doing some image filtering, and I want the user to be able to see the cleanly filtered video stream, but in the upper hand corner see a preview of what the filter is doing:

I have tried setting an ROI on the filtered image, and the using AddWeighted in JNI code like this

In Activity:

liveFrame.copyTo(mRgba); //going to be used as the unfiltered stream

//image filtering happends here...

Rect roi = new Rect(0, 0, liveFrame.cols()/4, liveFrame.rows()/4);
mProcMat = new Mat(liveFrame, roi);
AddMats(mRgba.getNativeObjAddr(),mProcMat.getNativeObjAddr()); //will add the two, assign the result to mRgba

AddMats (Native Method):

Mat* image1=(Mat*)mat1;
Mat* image2 = (Mat*)mat2;
float alpha = 0.5;
float beta = 0.5;
addWeighted( (InputArray)*image1, alpha, (InputArray)*image2, beta, 0.0, (OutputArray)*image1);

But this wont work because the size of the images has to be the same, so I cant seem to think of a way to get this to work. I can do this easily in iOS, because you can simply just apply the frame to a new view on top of the live stream. For Android, as far as I know, you can only have 1 surface view per camera input stream. So thats why I went down the path of doing this in opencv, but I would consider ANY solution at this point

Rui Marques
  • 8,567
  • 3
  • 60
  • 91
Jameo
  • 4,507
  • 8
  • 40
  • 66

2 Answers2

3

If I understood correctly, you want a live image to be displayed unaltered, and in the upper hand corner the result of applying some filter to the live image, is this correct? You want the filtered preview to represent the whole image or just part of it?

Here is an algorithm that I think does what you want:

Step 1: resize liveFrame (without altering it, so you either create a copy or use a resize method which creates a copy internally) so it is a new image (called mProcMat) smaller than the original. You might want to check this question.

Step 2: apply image filtering to mProcMat.

Step 3: to place the mProcMat on the liveFrame, create a ROI on liveFrame which is positioned on its upper right and has the same size of mProcMat. Use submat for the ROI, has you have seen in your other question, it is more flexible because you have 3 different ways to set it up.

Step 4: AddMats(liveFrameROI.getNativeObjAddr(), mProcMat.getNativeObjAddr());

Does this sound good?

EDIT: I don't even think you need to do addWeighted of the 4th step, to get the preview you could just paint mProcMat on the roi from step 3. I didn't understand completely your idea to use addWeighted.

You might also want to check this thread, it is somehow similar.

Community
  • 1
  • 1
Rui Marques
  • 8,567
  • 3
  • 60
  • 91
1

I've talked about this before, but if you want to overlay with transparency I suggest taking a look at: Transparent image overlays in OpenCV.

If you need to resize one of the images, just check this link.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426