First time here? Check out the FAQ!

Ask Your Question
0

ColorDetecting with opencv 3.4.1

asked Apr 19 '18

Mus gravatar image

I can't find where is the error ! My code :

public static void main(String[] args) {
    System.load("C:\\Users\\MustaProd\\Desktop\\opencv\\build\\java\\x64\\opencv_java341.dll");

    Mat img;
    img = imread("../feu/feu.png",1);

    Mat outRED = new Mat();
    Mat outOrange = new Mat();
    Mat outGREEN = new Mat();

    //BGR

    //obtenir RED
    Scalar redDebut = new Scalar(28,28,112);
    Scalar redFin = new Scalar(28,28,112);
    inRange(img,redDebut,redFin,outRED);

    //obtenir Orange
    inRange(img,new Scalar(0,85,204),new Scalar(206,231,247),outOrange);

    //obtenir GREEN
    inRange(img,new Scalar(0,128,0),new Scalar(93,236,178),outGREEN);


    int r = Core.countNonZero(outRED);
    int o = Core.countNonZero(outOrange);
    int g = Core.countNonZero(outGREEN);

    if(r>o && r>g) {
        System.out.println("Feu rouge!!!");
    }
    else if(o>g && o>r){
        System.out.println("Feu Orange!!!");
    }
    else {
        System.out.println("Feu Vert");
    }


}

output :

Exception in thread "main" java.lang.Exception: unknown exception
at org.opencv.core.Core.inRange_0(Native Method)
at org.opencv.core.Core.inRange(Core.java:1531)
at sample.Main.main(Main.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

Preview: (hide)

Comments

Can you check img size ?

LBerger gravatar imageLBerger (Apr 19 '18)edit
1

please check for img.empty() after imread()

berak gravatar imageberak (Apr 19 '18)edit
1

1 answer

Sort by » oldest newest most voted
1

answered Apr 19 '18

always, but really always, start with absolute paths, instead of relative ones. You would be amazed for how many people that would solve their issues. Keep in mind that different IDE's use different execution locations, and thus relative paths could simply be wrong, resulting in an image that does not exist, hence all the subsequent issues.

Preview: (hide)

Comments

1

I runs well after using the absolute path! Thanks! :D

Mus gravatar imageMus (May 8 '18)edit

Question Tools

1 follower

Stats

Asked: Apr 19 '18

Seen: 262 times

Last updated: Apr 18 '18