删除黑色背景时出现OpenCV 3.2错误

时间:2018-05-14 22:57:54

标签: java opencv

我目前正在按照本教程进行图像分割:http://opencv-java-tutorials.readthedocs.io/en/latest/07-image-segmentation.html

我将它应用于支票图像。我能够检测到边缘,但是我遇到了去除背景的问题。

我的代码:

public static void main(String args[]){
    InputStream inputStream = this.getClassLoader().getResourceAsStream("check.jpg");
            image = ImageIO.read(inputStream);
 Mat colorImg = this.bufferedImageToMat(image);
        Mat grayImg = new Mat();
        Mat draw = new Mat();
        Mat frameImg = new Mat();
        Imgproc.cvtColor(colorImg, grayImg, Imgproc.COLOR_BGR2GRAY);
        Imgproc.blur(grayImg, colorImg, new Size(3, 3));
        Imgproc.Canny(grayImg, frameImg, 50, 150, 3, false);
        frameImg.convertTo(draw, CvType.CV_8U);

      Mat fg= this.doBackgroundRemoval(frameImg);

}
 private Mat doBackgroundRemoval(Mat frame) throws Exception{
        // init

        Mat hsvImg = new Mat();
        List<Mat> hsvPlanes = new ArrayList<>();
        Mat thresholdImg = new Mat();

        // threshold the image with the histogram average value
        System.out.println(frame.type());
        hsvImg.create(frame.size(),CvType.CV_8U);

        BufferedImage image = this.Mat2BufferedImage(hsvImg);

        Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV); //**** THIS IS WHERE IT BLOWS UP
        Core.split(hsvImg, hsvPlanes);

        double threshValue = this.getHistAverage(hsvImg, hsvPlanes.get(0));

        Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY_INV);
    /*    else
            Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY);
*/
        Imgproc.blur(thresholdImg, thresholdImg, new Size(5, 5));

        // dilate to fill gaps, erode to smooth edges
        Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, 1), 6);
        Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, 1), 6);

        Imgproc.threshold(thresholdImg, thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY);

        // create the new image
        Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
        frame.copyTo(foreground, thresholdImg);

        return foreground;
    }

当我们在doBackgroundRemoval方法中获得Imgproc.cvtColor(frame,hsvImg,Imgproc.COLOR_BGR2HSV)时,我收到此错误:

**OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cv::cvtColor, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\color.cpp, line 9815
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\color.cpp:9815: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor**

无论如何我能解决这个问题吗?或者另一种删除背景的方法?

感谢。

1 个答案:

答案 0 :(得分:1)

您必须为doBackgroundRemoval(Mat frame)方法提供彩色图像。您在主函数中提供了this.doBackgroundRemoval(frameImg)的灰度图像,该图像不适用于doBackgroundRemoval(Mat frame)的实现。