Opencv import highgui in android studio is showing error.Cannot resolve(I added the library still the error remain)

12,915

Solution 1

In opencv3.0, there is no more highgui module in java.

the functionality was split up into new videoio and imgcodecs (that's where you will find imread) modules.

import org.opencv.core.*;
import org.opencv.imgcodecs; // imread, imwrite, etc
import org.opencv.videoio;   // VideoCapture

EDIT: Change img=Highgui.imread(pathtoimage); to img = Imgcodecs.imread(pathtoimage);

and Core.rectangle(img, tl, br, color); to Imgproc.rectangle(img, t1, br, color);

I will also suggest go through OpenCV 3.0 modules to resolve these errors.

Solution 2

Depending on the file structure it may instead be:

import org.opencv.imgcodecs.Imgcodecs;
Share:
12,915

Related videos on Youtube

Anamika Agrwal
Author by

Anamika Agrwal

Updated on June 04, 2022

Comments

  • Anamika Agrwal
    Anamika Agrwal almost 2 years

    I have the following code which is for image segmentation using grabcut opencv. I am using opencv for the first time. I added the opencv library. While others worked perfectly highgui is still showing cannot resolve symbol.Do i need to add something else. (If you can help me with the code too, will be an additional help) Here is the code: Edit : I have posted the updated code:

           package com.example.android.seg;
    
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    
    
    import java.io.IOException;
    
    import org.opencv.android.OpenCVLoader;
    import org.opencv.android.Utils;
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import org.opencv.core.Scalar;
    import org.opencv.imgproc.Imgproc;
    import org.opencv.core.*;
    import org.opencv.imgcodecs.Imgcodecs; // imread, imwrite, etc
       // VideoCapture
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    
    
    public class seg extends Activity implements OnTouchListener {
        static{
            if (!OpenCVLoader.initDebug()) {
                Log.i("TEST", "Cannot connect to OpenCV Manager");
                // Handle initialization error
            }
            else {
                Log.i("TEST", "Connected to OpenCV Manager");
            }
        }
        ImageView imageView;
        Bitmap bitmap;
        Canvas canvas;
        Scalar color = new Scalar(255, 0, 0, 255);
        Point tl, br;
        int counter;
        Bitmap bitmapResult, bitmapBackground;
        Mat dst = new Mat();
        //"sdcard/DCIM/wall.jpg"
        final String pathToImage  = "wall.jpg";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_seg);
            imageView = (ImageView) this.findViewById(R.id.imageView);
    
            bitmap = BitmapFactory.decodeFile(pathToImage);
            Toast msg = Toast.makeText(seg.this, "Press top left and bottom right of the      foreground image", Toast.LENGTH_LONG);
    
            msg.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
    
            msg.show();
            bitmapResult = bitmap.copy(bitmap.getConfig(), true);
            canvas = new Canvas(bitmapResult);
    
            imageView.setImageBitmap(bitmapResult);
            imageView.setOnTouchListener(this);
    
            tl = new Point();
            br = new Point();
            counter = 0;
        }
    
        //@Override
        public boolean onTouch(View v, MotionEvent event) {
    
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (counter == 0) {
                    tl.x = event.getX();
                    tl.y = event.getY();
                    counter++;
                } else if (counter == 1) {
                    br.x = event.getX();
                    br.y = event.getY();
                    counter++;
    
                    Mat img = new Mat();
                    //val mat = Imgcodecs.imread(pathToImage)
                    img = Imgcodecs.imread(pathToImage);
                    Mat background = new Mat();
                    try {
                        background = Utils.loadResource(getApplicationContext(),
                                R.drawable.wall );
                    } catch (IOException e) {
    
                        e.printStackTrace();
                    }
    
                    backgroundSubtracting(img, background);
                    Imgcodecs.imwrite("wall.jpg", dst);
                    //Highgui.imwrite("/mnt/sdcard/GRABCUT/rect.png", dst);
                    Bitmap jpg = BitmapFactory
                            .decodeFile("wall.jpg");
    
                    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    imageView.setAdjustViewBounds(true);
                    imageView.setPadding(2, 2, 2, 2);
                    imageView.setImageBitmap(jpg);
                    imageView.invalidate();
                }
            }
    
            return true;
        }
    
        private void backgroundSubtracting(Mat img, Mat background) {
    
            Mat firstMask = new Mat();
            Mat bgModel = new Mat();
            Mat fgModel = new Mat();
            Mat mask;
            Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
            dst = new Mat();
            Rect rect = new Rect(tl, br);
    
            Imgproc.grabCut(img, firstMask, rect, bgModel, fgModel, 1, 0 /* GC_INIT_WITH_RECT */);
            Core.compare(firstMask, source/* GC_PR_FGD */, firstMask, Core.CMP_EQ);
    
            Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
                    255, 255));
            img.copyTo(foreground, firstMask);
            Imgproc.rectangle(img, tl, br, color);
            //Core.rectangle(img, tl, br, color);
    
            Mat tmp = new Mat();
            Imgproc.resize(background, tmp, img.size());
            background = tmp;
            mask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));
    
            Imgproc.cvtColor(foreground, mask, 6/* COLOR_BGR2GRAY */);
            Imgproc.threshold(mask, mask, 254, 255, 1 /* THRESH_BINARY_INV */);
    
            Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(0.0));
            background.copyTo(dst);
    
            background.setTo(vals, mask);
            Core.add(background, foreground, dst, mask);
    
            firstMask.release();
            source.release();
            bgModel.release();
            fgModel.release();
            vals.release();
        }
    
    }
    

    here is my logcat:

      05-20 09:48:51.830  24423-24423/com.example.android.seg D/dalvikvm﹕ Late-enabling CheckJNI
    05-20 09:48:51.832  24423-24429/com.example.android.seg D/dalvikvm﹕ threadid=2: interp stack at 0x40000000
    05-20 09:48:51.840  24423-24431/com.example.android.seg D/dalvikvm﹕ threadid=3: interp stack at 0x40008000
    05-20 09:48:51.840  24423-24423/com.example.android.seg D/jdwp﹕ prepping for JDWP over ADB
    05-20 09:48:51.840  24423-24423/com.example.android.seg D/jdwp﹕ ADB transport startup
    05-20 09:48:51.841  24423-24432/com.example.android.seg D/dalvikvm﹕ threadid=4: interp stack at 0x49247000
    05-20 09:48:51.841  24423-24432/com.example.android.seg D/jdwp﹕ JDWP: thread running
    05-20 09:48:51.841  24423-24432/com.example.android.seg D/jdwp﹕ acceptConnection
    05-20 09:48:51.842  24423-24432/com.example.android.seg D/jdwp﹕ trying to receive file descriptor from ADB
    05-20 09:48:51.842  24423-24433/com.example.android.seg D/dalvikvm﹕ threadid=5: interp stack at 0x4bd75000
    05-20 09:48:51.851  24423-24432/com.example.android.seg D/jdwp﹕ received file descriptor 40 from ADB
    05-20 09:48:51.853  24423-24435/com.example.android.seg D/dalvikvm﹕ threadid=6: interp stack at 0x4bd7d000
    05-20 09:48:51.854  24423-24435/com.example.android.seg D/dalvikvm﹕ threadid=6: calling run()
    05-20 09:48:51.855  24423-24432/com.example.android.seg D/jdwp﹕ processIncoming
    05-20 09:48:51.855  24423-24432/com.example.android.seg D/jdwp﹕ processIncoming
    05-20 09:48:51.855  24423-24432/com.example.android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000044, flags=0x0, dataLen=0x8
    05-20 09:48:51.856  24423-24432/com.example.android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x40000044)
    05-20 09:48:51.857  24423-24432/com.example.android.seg D/jdwp﹕ reply: dataLen=9 err=NONE(0)
    05-20 09:48:51.857  24423-24432/com.example.android.seg D/jdwp﹕ processIncoming
    05-20 09:48:51.857  24423-24432/com.example.android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x40000045, flags=0x0, dataLen=0xC
    05-20 09:48:51.857  24423-24432/com.example.android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=12 id=0x40000045)
    05-20 09:48:51.859  24423-24432/com.example.android.seg D/jdwp﹕ reply: dataLen=50 err=NONE(0)
    05-20 09:48:51.860  24423-24432/com.example.android.seg D/jdwp﹕ processIncoming
    05-20 09:48:51.860  24423-24432/com.example.android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000046, flags=0x0, dataLen=0x8
    05-20 09:48:51.860  24423-24432/com.example.android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x40000046)
    05-20 09:48:51.862  24423-24432/com.example.android.seg D/jdwp﹕ reply: dataLen=216 err=NONE(0)
    05-20 09:48:51.862  24423-24432/com.example.android.seg D/jdwp﹕ processIncoming
    05-20 09:48:51.862  24423-24432/com.example.android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000047, flags=0x0, dataLen=0x8
    05-20 09:48:51.863  24423-24432/com.example.android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=8 id=0x40000047)
    05-20 09:48:51.863  24423-24432/com.example.android.seg D/jdwp﹕ reply: dataLen=9 err=NONE(0)
    05-20 09:48:51.863  24423-24432/com.example.android.seg D/jdwp﹕ processIncoming
    05-20 09:48:51.863  24423-24432/com.example.android.seg D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x40000048, flags=0x0, dataLen=0x9
    05-20 09:48:51.863  24423-24432/com.example.android.seg D/jdwp﹕ REQ: DDM.Chunk (cmd=199/1 dataLen=9 id=0x40000048)
    05-20 09:48:51.863  24423-24432/com.example.android.seg D/jdwp﹕ reply: dataLen=0 err=NONE(0)
    05-20 09:48:51.863  24423-24436/com.example.android.seg D/dalvikvm﹕ threadid=7: interp stack at 0x4bd85000
    05-20 09:48:51.864  24423-24436/com.example.android.seg D/dalvikvm﹕ threadid=7: calling run()
    05-20 09:48:51.872  24423-24437/com.example.android.seg D/dalvikvm﹕ threadid=8: interp stack at 0x4bd8d000
    05-20 09:48:51.873  24423-24437/com.example.android.seg D/dalvikvm﹕ threadid=8: calling run()
    05-20 09:48:52.050  24423-24439/com.example.android.seg D/dalvikvm﹕ threadid=9: interp stack at 0x4bd95000
    05-20 09:48:52.052  24423-24440/com.example.android.seg D/dalvikvm﹕ threadid=10: interp stack at 0x4bd9d000
    05-20 09:48:52.064  24423-24423/com.example.android.seg D/jdwp﹕ sendBufferedRequest : len=0x39
    05-20 09:48:52.918  24423-24423/com.example.android.seg D/jdwp﹕ sendBufferedRequest : len=0x45
    05-20 09:48:53.289  24423-24423/com.example.android.seg D/ActivityThread﹕ BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{41615870 com.example.android.seg}}
    05-20 09:48:53.296  24423-24423/com.example.android.seg D/OpenCV/StaticHelper﹕ Trying to get library list
    05-20 09:48:53.296  24423-24423/com.example.android.seg D/dalvikvm﹕ Trying to load lib /mnt/asec/com.example.android.seg-2/lib/libopencv_info.so 0x41618e00
    05-20 09:48:53.297  24423-24423/com.example.android.seg A/libc﹕ Fatal signal 7 (SIGBUS) at 0x4bf6d9b8 (code=2)
    
  • Taryn
    Taryn almost 9 years
    Comments are not for extended discussion; this conversation has been moved to chat.
  • Tecnologia da Net
    Tecnologia da Net almost 4 years
    Exception or can someone help me in this matter, I really need to do this. stackoverflow.com/questions/61876457/…