Ask Your Question

ColorDetecting with opencv 3.4.1

asked 2018-04-18 20:59:52 -0600

Mus gravatar image

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

public static void main(String[] args) {

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

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


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

    //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(
at sample.Main.main(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.intellij.rt.execution.application.AppMain.main(

Process finished with exit code 1

edit retag flag offensive close merge delete


Can you check img size ?

LBerger gravatar imageLBerger ( 2018-04-19 01:18:46 -0600 )edit

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

berak gravatar imageberak ( 2018-04-19 02:49:04 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-04-19 03:23:33 -0600

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.

edit flag offensive delete link more



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

Mus gravatar imageMus ( 2018-05-08 14:32:45 -0600 )edit

Question Tools

1 follower


Asked: 2018-04-18 20:59:52 -0600

Seen: 235 times

Last updated: Apr 18 '18