Ask Your Question

Whenever i use Imgproc.rectangle and function following error appears

asked 2016-02-01 12:09:39 -0600

Poovamraj gravatar image

updated 2016-02-03 07:51:40 -0600

package org.opencv.samples.tutorial1;

import; import; import; import; import; import; import org.opencv.core.*; //import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc;

import android.os.Bundle; import; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.View.OnTouchListener; import android.view.WindowManager;

import com.example.imgloader.R;

import java.util.ArrayList; import java.util.Iterator; import java.util.List;

public class Tutorial1Activity extends Activity implements CvCameraViewListener2 {

private static final String TAG = "OCVSample::Activity";
private Mat mRgba;
private Mat mGray;

private CameraBridgeViewBase mOpenCvCameraView;
Scalar min = new Scalar(0, 30, 60, 0);
Scalar max = new Scalar(20, 150, 255, 0);
Mat pointMatHsv;
Mat heirarchy;
Mat overlay;

private static double mMinContourArea = 0.1;
Mat hullMat;

Scalar CONTOUR_COLOR = new Scalar(255, 0, 0, 255);

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS: {
                Log.i(TAG, "OpenCV loaded successfully");

            default: {

public Tutorial1Activity() {
    Log.i(TAG, "Instantiated new " + this.getClass());

protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");


    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(;
    mOpenCvCameraView.setMaxFrameSize(720, 480);



public void onPause() {
    if (mOpenCvCameraView != null)

public void onResume() {
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);

public void onDestroy() {
    if (mOpenCvCameraView != null)

public void onCameraViewStarted(int width, int height) {
    mGray = new Mat();
    mRgba = new Mat();
    pointMatHsv = new Mat(1, 1, CvType.CV_8UC3);
    heirarchy = new Mat();
    overlay = new Mat(1,1, CvType.CV_8UC3);


public void onCameraViewStopped() {

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();

    Scalar CONTOUR_COLOR = new Scalar(255, 0, 0, 255);
    Imgproc.cvtColor(pointMatHsv, mGray, Imgproc.COLOR_RGB2HSV);
    Core.inRange(mGray, min, max, pointMatHsv);
    Imgproc.dilate(pointMatHsv, pointMatHsv, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
    //Imgproc.dilate(mRgba, mRgba, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
    Imgproc.erode(pointMatHsv, pointMatHsv, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));

    mGray = retContours(pointMatHsv,mRgba);

    return mRgba;

public Mat retContours(Mat pointMatHsv, Mat mRgba) {
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(pointMatHsv, contours, heirarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = 0;
    Iterator<MatOfPoint> each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint wrapper =;
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea)
            maxArea = area;
   // Imgproc.cvtColor(pointMatHsv,pointMatHsv,Imgproc.COLOR_HSV2RGB_FULL);

    each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint contour =;
        if (Imgproc.contourArea(contour) > mMinContourArea * maxArea) {
           // Core.multiply(contour, new Scalar(4, 4), contour);

    Imgproc.drawContours(mRgba,mContours,-1,new Scalar(0, 255, 0));

    for(int i=0;i<mContours.size();i++) {
        MatOfPoint currentContour = mContours.get(i);


   return mRgba ...
edit retag flag offensive close merge delete


Is it the rectangle in the for loop or the one after it? Is CONTOUR_COLOR a valid input? It looks like you need a Scalar there no matter what version you're using.

Tetragramm gravatar imageTetragramm ( 2016-02-01 17:46:00 -0600 )edit

yes i have declared a Scalar value for it Scalar CONTOUR_COLOR = new Scalar(255, 0, 0, 255); however i try the rectangle there is same error saying no implementation found

Poovamraj gravatar imagePoovamraj ( 2016-02-02 09:51:49 -0600 )edit

Is it possible that my library does not have the implementation of Rect ?

Poovamraj gravatar imagePoovamraj ( 2016-02-02 11:01:18 -0600 )edit

That is very unlikely. Can you set a breakpoint and see if which of the two calls it's crashing on? You do slightly different things with them. Or check your line numbers and see which one is line 185.

Tetragramm gravatar imageTetragramm ( 2016-02-02 15:34:22 -0600 )edit

I tried them both and both the time the error is same.

Poovamraj gravatar imagePoovamraj ( 2016-02-03 07:40:18 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2016-02-04 06:53:01 -0600

Poovamraj gravatar image

Actually I found the answer. The problem is with importing the library. Even if we dont use the native files we have to copy it to the src folder of the project it seems. For a detailed answer please refer this link

edit flag offensive delete link more

answered 2016-02-03 09:23:13 -0600

panc gravatar image

As you can see from your stacktrace, openCV cannot find an implementation of Rect if you use this prototype :

Imgproc.rectangle_1(long, double, double, double, double, double, double, double, double, int)

Looking at the official documentation here, openCV expects something like this:

Imgproc.rectangle(Mat src,Point pt1, Point pt2, Scalar color, int thickness, int lineType, int)

and so its prototype is Imgproc.rectangle(Mat,Point, Point, Scalar, int, int, int) that is different from yours.

Alternatively, passing a Rect instead of Points:

 Imgproc.rectangle(Mat src,Rectangle rect, Scalar color, int thickness, int lineType, int)

and so its prototype is Imgproc.rectangle(Mat,Rect, Scalar, int, int, int) .

I think that yu have to double check your code to match these prototypes.

edit flag offensive delete link more



nice try, you're correct about the java interface, but the native c++ wrapper is indeed using 4 double values instead of a Rect (or 2 points).

berak gravatar imageberak ( 2016-02-04 02:09:58 -0600 )edit

Oh sorry, thank's for the correction!

panc gravatar imagepanc ( 2016-02-04 04:26:55 -0600 )edit

Question Tools

1 follower


Asked: 2016-02-01 12:09:39 -0600

Seen: 5,668 times

Last updated: Feb 04 '16