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



我目前正在按照本教程进行图像分割: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:buildmaster_winpack-bindings-win64-vc14-staticopencvmodulesimgprocsrccolor.cpp, line 9815
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:buildmaster_winpack-bindings-win64-vc14-staticopencvmodulesimgprocsrccolor.cpp:9815: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor**

无论如何我可以解决这个问题吗?还是去除背景的替代方法?

谢谢。

您必须向doBackgroundRemoval(Mat frame)方法提供彩色图像。您正在提供主函数中带有this.doBackgroundRemoval(frameImg)的灰度图像,该图像不适用于doBackgroundRemoval(Mat frame)的嵌入。

相关内容