2016-03-12 3 views
0

Я работаю над программой, которая обнаруживает область зрачка от глаза, используя язык opencv. Ниже приведен фрагмент кода. Это не компиляция, исключение CV исключение. Я не знаю, что делать. Как я могу заставить его работать. (OpenCV 2,4)Показывает ошибку исключения

import java.util.ArrayList; 
import java.util.List; 
import java.lang.Math; 
import org.opencv.core.Scalar; 
import org.opencv.core.Point; 
import org.opencv.core.Rect; 
import org.opencv.core.Core; 
import org.opencv.core.CvException; 
import org.opencv.core.Mat; 
import org.opencv.core.MatOfPoint; 
import org.opencv.highgui.Highgui; 
import org.opencv.imgproc.Imgproc; 

public class Detect { 
    public static void main(String[] args) throws CvException{ 

     System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 

     // Load image 
     Mat src = Highgui.imread("tt.jpg"); 

     // Invert the source image and convert to grayscale 
     Mat gray = new Mat(); 
     Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); 
     Highgui.imwrite("gray.jpg", gray); 
     // Convert to binary image by thresholding it 
     Imgproc.threshold(gray, gray, 30, 255, Imgproc.THRESH_BINARY_INV); 
     Highgui.imwrite("binary.jpg", gray); 
     // Find all contours 
     List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
     Imgproc.findContours(gray.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); 

     // Fill holes in each contour 
     Imgproc.drawContours(gray, contours, -1, new Scalar(255,255,255), -1); 

     for (int i = 0; i < contours.size(); i++) 
     { 
      double area = Imgproc.contourArea(contours.get(i)); 
      Rect rect = Imgproc.boundingRect(contours.get(i)); 
      int radius = rect.width/2; 
      System.out.println("Area: "+area); 
      // If contour is big enough and has round shape 
      // Then it is the pupil 
      if (area >= 30 && 
       Math.abs(1 - ((double)rect.width/(double)rect.height)) <= 0.2 && 
        Math.abs(1 - (area/(Math.PI * Math.pow(radius, 2)))) <= 0.2) 
      { 
       Core.circle(src, new Point(rect.x + radius, rect.y + radius), radius, new Scalar(255,0,0), 2); 
       System.out.println("pupil"); 
      } 
     } 

     Highgui.imwrite("processed.jpg", src); 
    } 
} 

Показана следующая ошибка

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3739 
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor 
] 
    at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method) 
    at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4598) 
    at Detect.main(Detect.java:24) 

ответ

1

Я думаю, что OpenCV считает, что "tt.jpg" уже одноканальная.

Согласно documentation:

Функция определяет тип изображения по содержанию, а не расширение файла.

Чтобы обеспечить формат, вы можете использовать флаг:

Mat src = Highgui.imread("tt.jpg"); // OpenCV decides the type based on the content 
Mat src = Highgui.imread("tt.jpg", Highgui.IMREAD_GRAYSCALE); // single-channel image will be loaded, even if it is a 3-channel image 
Mat src = Highgui.imread("tt.jpg", Highgui.IMREAD_COLOR); // 3-channel image will be loaded, even if it is a single-channel image 

Если вам нужно только черно-белое изображение:

Mat src = Highgui.imread("tt.jpg", Highgui.IMREAD_GRAYSCALE); 
+0

ее не работает, его показывая IMREAD_GRAYSCALE ошибки не может быть решена к переменной –

+0

@MuhammadHussainRana Извините, я забыл добавить 'Highgui.' перед' IMREAD_ * '. Обновлен код. – Berriel

Смежные вопросы