Ask Your Question

Ftahir192's profile - activity

2017-01-03 12:47:58 -0500 received badge  Scholar (source)
2017-01-03 11:55:53 -0500 received badge  Enthusiast
2016-12-31 10:31:52 -0500 received badge  Supporter (source)
2016-12-31 10:31:48 -0500 commented answer Opencv4android mask for matchTemplate()

Thanks for the edit, that is perfect! When I call minMaxLoc, the coordinates of the object will be in terms of the new, shrinked image. How can I make it such that these coordinates map to the full sized image? I am drawing a rectangle over the frame to show the location of the object being matched.

2016-12-31 08:08:42 -0500 commented answer Opencv4android mask for matchTemplate()

Hi. Thank you for the reply. I'm wondering if you can explain the numbers you used to calculate the rectangles parameters? Thanks.

2016-12-30 13:18:38 -0500 asked a question Opencv4android mask for matchTemplate()


I'm trying to speed up matchTemplate() for Android. I have already downsampled the image and template to half the original size, but the matching is still somewhat (understandably) slow on some older devices.

I acknowledged that I can pass in a mask to the matchTemplate() method. I'm trying to utilize this in the following way:

1) I know the location of the object from the previous frame, lets say its (180, 30). 2) I want to search only around some specified distance around this point, rather than the whole frame. 3) I create a region of interest based on this distance.

My question is, how can I utilize this roi such that I can take the current image being searched, and set everything outside the ROI to 0? I can then pass this in as a mask, and thus matchTemplate() would only search the image where pixel values are not 0 (so everything in the ROI)?

I have something such as the following:

cameraFrameROI = new Rect(topLeftX, topLeftY, width, height);
Mat mask = new Mat(mCameraMatResized);
// mask = 0 if not part of ROI, otherwise keep original value
Imgproc.matchTemplate(new Mat(mCameraMatResized, cameraFrameROI), mTemplateMatResized, mResult, mMatchMethod);

But I now need to utilize the ROI to create the actual mask.

Would this speed up template matching, and if so, I'm hoping if anyone can provide pointers.


2016-11-19 12:55:13 -0500 commented question OpenCV4android template matching

Ah I see, I'm calling it for every frame! I'll look into this, I'll see firstly if downscaling helps. Thanks a lot for your help!

2016-11-19 12:48:59 -0500 commented question OpenCV4android template matching

Hmm, I've been advised on SO to stick to matchTemplate as it is already well optimised.

2016-11-19 12:35:30 -0500 commented question OpenCV4android template matching

Sounds good, if I also scale the template matching, I'd be doing only 1/4 of the evaluations as opposed to non-scaling, which is a massive difference in and of itself.

Regarding downscaling, would using the resize() function built into OpenCV be considered a fast-scaling method, or can I do better?

2016-11-19 12:16:10 -0500 commented question OpenCV4android template matching

If I go with downscaling, what image and template size is recommended? As stated currently, the image size is 1280x720, and template is normally no more than half of that resolution. Thanks for the help.

2016-11-19 11:07:19 -0500 commented question OpenCV4android template matching

Wouldn't this affect the end result if I decrease the source image size? Furthermore, I would have to resize it for each image that comes in, which could be 20+ resizes a second. Would that not be costly also? The size of the source images are 1280x720 currently.

I am using the template size as an indicator to what size the rectangle bounding box would be, and if I decrease the template size further, I'm not sure how I could indicate what size the bounding box (to indicate detected object) should be drawn at.

2016-11-19 10:16:38 -0500 asked a question OpenCV4android template matching


I'm using OpenCV4Android to try out template matching on an android phone. Essentially what I'm doing is taking a picture of some object that I wish to detect, cropping it and saving it as a template.

I then use my android camera and a surface view to constantly get images from the camera. I am then applying template matching on each image, converting the image to a Mat first. However, when applying template matching I only get around 3-4 fps.

What I am essentially doing is this:

** mCameraMat = inputFrame.rgba();

            int matchMethod = Imgproc.TM_CCOEFF_NORMED;

            // mTemplateMat resized in terms of video size in prepareMediaRecorder.
            // Very hacky solution so need to fix it!
            int result_cols = mCameraMat.cols() - mTemplateMat.cols() + 1;
            int result_rows = mCameraMat.rows() - mTemplateMat.rows() + 1;

            mResult = new Mat(result_rows, result_cols, CvType.CV_32F);

            // Move this to a new thread.
            Imgproc.matchTemplate(mCameraMat, mTemplateMat, mResult, matchMethod);
            Core.normalize(mResult, mResult, 0, 1, Core.NORM_MINMAX, -1, new Mat());

            // Localizing the best match with minMaxLoc
            MinMaxLocResult mmr = Core.minMaxLoc(mResult);

            Point matchLoc;
            if (matchMethod == Imgproc.TM_SQDIFF || matchMethod == Imgproc.TM_SQDIFF_NORMED) {
                matchLoc = mmr.minLoc;
            } else {
                matchLoc = mmr.maxLoc;

            // Draw a boundary around the detected object.
            Imgproc.rectangle(mCameraMat, matchLoc, new Point(matchLoc.x + mTemplateMat.cols(),
                matchLoc.y + mTemplateMat.rows()), new Scalar(TrackingActivity.r, TrackingActivity.g,
                TrackingActivity.b, TrackingActivity.a), 2);


Where mTemplateMat is the template bitmap image converted into a Mat object.

The bottleneck is on the line

Imgproc.matchTemplate(mCameraMat, mTemplateMat, mResult, matchMethod);

If I remove that line, I get around 25 fps, which is much more acceptable. I'd be fine with anything above 13-14. I understand that template matching is a very expensive process and doing it every frame can be costly. I have tried to do it every 20 frames, but it still slows down the processing considerably, and the end video looks worse as there is a constant transition from a smooth fps display to a low fps display.

What are my options in optimising matchTemplate? Any tips are much appreciated.

2016-10-13 18:46:31 -0500 asked a question Recording from JavaCameraView

I've been attempting to record the JavaCameraView for a few days now, to no avail. I'm trying to use MediaRecorder to record.

I have a JavaCameraView in my layout file, have added a CvCameraViewListener as follows:


where mCameraView = (CameraBridgeViewBase) findViewById(;

and is


I am therefore getting a preview on the screen correctly, and onCameraFrame is being called.

The problem I am having with using MediaRecorder and the Camera API to record is that already has an instance of the camera open, and so I cannot open another one to record with.

The mCamera object in JavaCameraViewis protected, so I've attempted to make a class CameraView that extends JavaCameraView, and through there, call the mCamera.lock()/mCamera/unlock() methods needed. This isn't working either.

My last resort was to record the surface instead, and this is being done as follows:

// Step 1: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT );

// Step 2: Set a CamcorderProfile (requires API Level 8 or higher)

// Step 3: Set output file (handled in CameraHelper)
String date = CreateFiles.getDate();
mRootFolder = CreateFiles.getOutputFolder(date);
Log.i(TAG, "Output folder is " + mRootFolder.toString());

mMediaFile = CreateFiles.getOutputMediaFile(
            CreateFiles.MEDIA_TYPE_VIDEO, mRootFolder, date);
mDataFile = CreateFiles.getOutputDataFile(mRootFolder, date);

mMediaRecorder.setVideoSize(mCameraView.getmFrameWidth(), mCameraView.getmFrameHeight());


However when I stop the MediaRecorder, it crashes with the following stack trace:

**10-14 00:28:48.239 23590-23590/ E/MediaRecorder: stop failed: -1007
10-14 00:28:48.239 23590-23590/ D/AndroidRuntime: Shutting down VM
10-14 00:28:48.249 23590-23590/ E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process:, PID: 23590
                                                                           java.lang.AssertionError: java.lang.RuntimeException: stop failed.
                                                                               at android.view.View.performClick(
                                                                               at android.view.View$
                                                                               at android.os.Handler.handleCallback(
                                                                               at android.os.Handler.dispatchMessage(
                                                                               at android.os.Looper.loop(
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                            Caused by: java.lang.RuntimeException: stop failed.**

And I have no idea what gives. So now I'm asking for help here and would greatly appreciate anybody give some hints as to what I might be doing wrong in my code above, or how I can use the Camera to record (even though it's being used by JavaCameraView).

Thanks a lot in advance.

2016-10-12 11:56:09 -0500 asked a question OpenCV4Android LockCanvas error


I've come across an error in the Opencv4android SDK which I can't quite explain. This bug only shows on my Google Tango tablet, but is non existent when I run my app on my Galaxy S6.

10-12 17:41:53.625 24608-24847/ E/SurfaceHolder: Exception locking surface
                                        at android.view.Surface.nativeLockCanvas(Native Method)
                                        at android.view.Surface.lockCanvas(
                                        at android.view.SurfaceView$4.internalLockCanvas(
                                        at android.view.SurfaceView$4.lockCanvas(

The error originates from JavaCameraView and I've done some research but can't find anything about it. It's not part of my own code, so I'm at a bit of a loss here.

Any advice on what this could be? I can't even explain the error well because I'm not sure exactly what's causing it.

Any help is much appreciated.