bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.53k stars 1.58k forks source link

rror: Assertion failed (!_src.empty()) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/travis/build/javacpp-presets/opencv/cppbuild/android-arm/opencv-3.4.3/modules/imgproc/src/color.cpp, #1086

Closed kigkrazy closed 5 years ago

kigkrazy commented 5 years ago

environment

code

 public static MatchResult match(String source, String target, double precision) {
        //read in image default colors
        opencv_core.Mat sourceColor = imread(source);
        opencv_core.Mat sourceGrey = new opencv_core.Mat(sourceColor.size(), CV_8UC1);
        cvtColor(sourceColor, sourceGrey, COLOR_BGR2GRAY);//crash
        //load in template in grey
        opencv_core.Mat template = imread(target, CV_LOAD_IMAGE_GRAYSCALE);//int = 0
        //Size for the result image
        opencv_core.Size size = new opencv_core.Size(sourceGrey.cols() - template.cols() + 1, sourceGrey.rows() - template.rows() + 1);
        opencv_core.Mat result = new opencv_core.Mat(size, CV_32FC1);
        matchTemplate(sourceGrey, template, result, TM_CCORR_NORMED);

        DoublePointer minVal = new DoublePointer();
        DoublePointer maxVal = new DoublePointer(2);
        opencv_core.Point min = new opencv_core.Point();
        opencv_core.Point max = new opencv_core.Point();
        minMaxLoc(result, minVal, maxVal, min, max, null);//寻找最佳匹配
        if (maxVal.get(0) >= precision) {
            MatchResult matchResult = new MatchResult();
            matchResult.setMatch(true);
            matchResult.setX(max.x());
            matchResult.setY(max.y());
            matchResult.setWidth(template.cols());
            matchResult.setHeight(template.rows());
            matchResult.setMiddleX(max.x() + (template.cols() / 2));
            matchResult.setMiddleX(max.y() + (template.rows() / 2));
        }
        return new MatchResult();
    }

    @Data
    public static class MatchResult {
        private boolean isMatch = false;//是否匹配
        private int x = 0;//X坐标
        private int y = 0;//Y坐标
        private int middleX = 0;//图像中间中心点X坐标
        private int middleY = 0;//图像中间中心点Y坐标
        private int width = 0;//宽
        private int height = 0;//高
    }

dependencies


    //JAVACV ---- 图像识别相关
    api group: 'org.bytedeco', name: 'javacv', version: '1.4.3'
    api group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.4.3-1.4.3', classifier: 'android-arm'
    api group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.4.3-1.4.3', classifier: 'android-x86'
    api group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '4.0.2-1.4.3', classifier: 'android-arm'
    api group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '4.0.2-1.4.3', classifier: 'android-x86'
saudet commented 5 years ago

You'll need to provide a non-empty image to cvtColor(). My guess is the image file is missing or you don't have permission to read it.

kigkrazy commented 5 years ago

I'm sorry. this problem is caused by Andoird-permission.