Java Code Examples for org.opencv.core.Mat#empty()

The following examples show how to use org.opencv.core.Mat#empty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DetectObject.java    From classchecks with Apache License 2.0 7 votes vote down vote up
/**
 * 
* @Title: detectMany 
* @Description: 检测多人图片,返回检测的人脸区域对象
* @param mImgSRC
* @return
* MatOfRect
 */
public static MatOfRect detectMany(Mat mImgSRC) {
	
	if(mImgSRC.empty()) {
		LOG.info("检测多人图片检测时没有找到图片");
		return null;
	}
	// 人脸检测器文件的所在路径的文件夹名称数组
	String [] pathKey = {ServletContextHolder.getOpenCVHaarcascades(), "haarcascade_frontalface_alt.xml"};
	CascadeClassifier cascade = new CascadeClassifier(FileUtils.buildFilePath(pathKey));
	if(cascade.empty()) {
		LOG.info("人脸检测级联加载器为null");
		return null;
	}
	// 记录搜索到的人脸区域
	MatOfRect mOfRect = new MatOfRect();
	// 用于计算缩放比例
	int scaledWidth = mImgSRC.width();
	detectManyObject(mImgSRC, cascade, mOfRect, scaledWidth);
	if(mOfRect.toArray().length <= 0) {
		LOG.info("没有检测到人脸...");
		return null;
	}
	return mOfRect;
}
 
Example 2
Source File: TextRecognizer.java    From SikuliX1 with MIT License 6 votes vote down vote up
protected <SFIRBS> String doRead(SFIRBS from) {
  try {
    String text = "";
    if (from instanceof Mat) {
      Mat img = ((Mat) from).clone();
      if (!img.empty()) {
        img = SXOpenCV.optimize(img, options.factor(), options.resizeInterpolation());
        byte[] bytes = new byte[img.width() * img.height()];
        int n = img.get(0, 0, bytes);
        text = getTesseractAPI().doOCR(img.width(), img.height(), ByteBuffer.wrap(bytes), null, 8);
      } else {
        return "";
      }
    } else {
      BufferedImage bimg = SXOpenCV.optimize(Element.getBufferedImage(from), options.factor(), options.resizeInterpolation());
      text = getTesseractAPI().doOCR(bimg);
    }
    return text.trim().replace("\n\n", "\n");
  } catch (TesseractException e) {
    Debug.error("OCR: read: Tess4J: doOCR: %s", e.getMessage());
    return "";
  }
}
 
Example 3
Source File: ObjectDetection.java    From FTCVision with MIT License 6 votes vote down vote up
/**
 * Analyzes an object in preparation to search for the object in a frame.
 * <p/>
 * This method should be called in an initialize() method.
 * Calling the analyzeObject method twice will overwrite the previous objectAnalysis.
 * <p/>
 * It is recommended to use a GFTT (Good Features To Track) detector for this phase.
 *
 * @param object Object image
 * @return The object descriptor matrix to be piped into locateObject() later
 */
public ObjectAnalysis analyzeObject(Mat object) throws IllegalArgumentException {
    Mat descriptors = new Mat();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();

    Log.d("FTCVision", "Analyzing object...");

    if (object == null || object.empty()) {
        throw new IllegalArgumentException("Object image cannot be empty!");
    }

    //Detect object keypoints
    detector.detect(object, keypoints);

    //Extract object keypoints
    extractor.compute(object, keypoints, descriptors);

    return new ObjectAnalysis(keypoints, descriptors, object);
}
 
Example 4
Source File: Morphology.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Mat process(@NonNull Mat frame) {
    if (frame.empty()) {
        Log.e("Invalid input frame.");
        return null;
    }
    Mat tmp = frame.clone();
    // Step 1: erode to remove legs
    Imgproc.erode(tmp, tmp, KERNEL3);
    // Step 2: dilate to join bodies and heads
    Imgproc.dilate(tmp, tmp, KERNEL2);
    for (int i = 0; i < REPETITIONS_DILATE; i++) {
        Imgproc.dilate(tmp, tmp, kernelDilate);
    }
    // Step 3: erode to recover original size
    Imgproc.erode(tmp, tmp, KERNEL1);
    for (int i = 0; i < REPETITIONS_ERODE; i++) {
        Imgproc.erode(tmp, tmp, kernelErode);
    }
    return tmp;
}
 
Example 5
Source File: Element.java    From SikuliX1 with MIT License 6 votes vote down vote up
private static Mat possibleReload(Element element, URL url, List<Object> items) {
  Mat newContent = new Mat();
  if (isFile(url)) {
    long modified = new File(url.getPath()).lastModified();
    long lastMod = (long) items.get(ITEM_LASTMOD);
    if (modified > lastMod) {
      newContent = reload(url);
      if (!newContent.empty()) {
        items.set(ITEM_COUNT, -1);
        items.set(ITEM_LASTMOD, modified);
        element.wasReloaded();
      }
    }
  }
  return newContent;
}
 
Example 6
Source File: MotionDetectionTest.java    From opencv-fun with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void loop (Mat mat, ImgWindow window, VideoCapture video) {
	video.read(mat);
	if (!mat.empty()) {
		boolean result = detector.detect(mat);
		if(result) System.out.println("motion detected, " + System.nanoTime());
		window.setImage(detector.getMask());
	}
}
 
Example 7
Source File: FXController.java    From Face-Recognition with Apache License 2.0 5 votes vote down vote up
/**
 * Get a frame from the opened video stream (if any)
 * 
 * @return the {@link Image} to show
 */
private Image grabFrame()
{
	// init everything
	Image imageToShow = null;
	Mat frame = new Mat();
	
	// check if the capture is open
	if (this.capture.isOpened())
	{
		try
		{
			// read the current frame
			this.capture.read(frame);
			
			// if the frame is not empty, process it
			if (!frame.empty())
			{
				// face detection
				this.detectAndDisplay(frame);
				
				// convert the Mat object (OpenCV) to Image (JavaFX)
				imageToShow = mat2Image(frame);
			}
			
		}
		catch (Exception e)
		{
			// log the (full) error
			System.err.println("ERROR: " + e);
		}
	}
	
	return imageToShow;
}
 
Example 8
Source File: BackgroundSubtractor.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Mat process(@NonNull Mat frame) {
    if (frame.empty()) {
        Log.e("Invalid input frame.");
        return null;
    }
    Mat foreground = new Mat();
    // Apply background substraction
    mog.apply(frame, foreground);
    return foreground;
}
 
Example 9
Source File: Blur.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Mat process(@NonNull Mat frame) {
    if (frame.empty()) {
        Log.e("Invalid input frame.");
        return null;
    }
    Mat tmp = frame.clone();
    // Apply gaussian blur
    for (int i = 0; i < REPETITIONS; i++) {
        Imgproc.GaussianBlur(tmp, tmp, new Size(KERNEL_SIZE, KERNEL_SIZE), 0);
    }
    return tmp;
}
 
Example 10
Source File: ContoursFinder.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Mat process(@NonNull Mat frame) {
    if (frame.empty()) {
        Log.e("Invalid input frame.");
        return null;
    }
    Mat tmp = frame.clone();
    // Finding outer contours
    contourList.clear();
    Imgproc.findContours(tmp, contourList, hierarchy,
            Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    // Filter bees
    Mat contours = new Mat(tmp.rows(), tmp.cols(), CvType.CV_8UC3);
    tmp.release();
    double area;
    Scalar color;
    numBees = 0;
    for (int i = 0; i < contourList.size(); i++) {
        area = Imgproc.contourArea(contourList.get(i));
        if (area > minArea && area < maxArea) {
            color = GREEN;
            numBees++;
        } else {
            color = RED;
        }
        // Draw contour
        Imgproc.drawContours(contours, contourList, i, color, -1);
    }
    return contours;
}
 
Example 11
Source File: ImgprocessUtils.java    From classchecks with Apache License 2.0 5 votes vote down vote up
/**
	 * 
	* @Title: cutOutImage 
	* @Description: 以Rect坐标裁剪图片 
	* @param src
	* @param mOfRect
	* @return
	* List<Mat> 
	 */
	public static List<Mat> cutOutImage(Mat src, MatOfRect mOfRect) {
		
		List<Mat> outs = new ArrayList<Mat>();
		
		if(src.empty() || mOfRect.toArray().length <= 0) {
			return null;
		}
		
		int enlarge = 50; //  将图片的x,y坐标加大
		for(Rect r : mOfRect.toArray()) {
			int enlargeW = (r.x + enlarge) / r.x;
			int enlargeH = (r.y + enlarge) / r.y;
			r.x -= enlarge;
			r.y -= enlarge;
			r.width += (enlargeW * r.width);
			r.height += (enlargeH * r.height);
			
			// 确定人脸在图像中的整个边界,防止人脸在边框之外
			if (r.x < 0) {
				r.x = 0;
			}
			if (r.y < 0) {
				r.y = 0;
			}
			if (r.x + r.width > src.cols()) {
				r.x = src.cols() - r.width;
			}
			if (r.y + r.height > src.rows()) {
				r.y = src.rows() - r.height;
			}
		
			
			Mat cutting = new Mat(src, r); // 裁剪图片
//			System.out.println(cutting);
			outs.add(cutting);
		}
		return outs;
	}
 
Example 12
Source File: Recognition.java    From classchecks with Apache License 2.0 5 votes vote down vote up
public static Mat subspaceReconstruct(Mat W, Mat mean, Mat src) {
	int n = src.rows();
	int d = src.cols();
	Mat X = new Mat();
	Mat Y = new Mat();
	src.convertTo(Y, W.type());
	Core.gemm(Y, W, 1.0, new Mat(), 0.0, X, 2);
	if(!mean.empty()) {
		for(int i = 0; i < n; i ++) {
			Mat r_i = X.row(i);
			Core.add(r_i, mean.reshape(1, 1), r_i);
		}
	}
	return X;
}
 
Example 13
Source File: Recognition.java    From classchecks with Apache License 2.0 5 votes vote down vote up
public static Mat subspaceProject(Mat W, Mat mean, Mat src) {
	int n = src.rows();
	int d = src.cols();
	Mat X = new Mat();
	Mat Y = new Mat();
	src.convertTo(X, W.type());
	if(!mean.empty()) {
		for(int i = 0; i < n; i ++) {
			Mat r_i = X.row(i);
			Core.subtract(r_i, mean.reshape(1, 1), r_i);
		}
	}
	Core.gemm(X, W, 1.0, new Mat(), 0.0, Y);
	return Y;
}
 
Example 14
Source File: FaceDetectionController.java    From ExoVisix with MIT License 5 votes vote down vote up
/**
 * Get a frame from the opened video stream (if any)
 * 
 * @return the {@link Image} to show
 */
private Image grabFrame()
{
	// init everything
	Image imageToShow = null;
	Mat frame = new Mat();
	
	// check if the capture is open
	if (this.capture.isOpened())
	{
		try
		{
			// read the current frame
			this.capture.read(frame);
			
			// if the frame is not empty, process it
			if (!frame.empty())
			{
				// face detection
				this.detectAndDisplay(frame);
				
				// convert the Mat object (OpenCV) to Image (JavaFX)
				imageToShow = mat2Image(frame);
			}
			
		}
		catch (Exception e)
		{
			// log the (full) error
			System.err.println("ERROR: " + e);
		}
	}
	
	return imageToShow;
}
 
Example 15
Source File: ScreensComparator.java    From QVisual with Apache License 2.0 5 votes vote down vote up
public static String saveImage(Mat image, StringBuffer error) {
    if (!image.empty()) {
        try {
            String fileName = String.format("%s-%s.png", new Date().toInstant().getEpochSecond(), randomAlphanumeric(10));
            imwrite(REPORTS_PATH + fileName, image);

            return fileName;
        } catch (Exception e) {
            error.append(System.currentTimeMillis() + "Could not save image: " + e.getMessage()).append("\n");
            logger.error("[save image]", e);
        }
    }

    return "";
}
 
Example 16
Source File: Utils.java    From BlindWatermark with Apache License 2.0 5 votes vote down vote up
public static Mat read(String image, int type) {
    Mat src = imread(image, type);
    if (src.empty()) {
        System.out.println("File not found!");
        System.exit(-1);
    }
    return src;
}
 
Example 17
Source File: FindAttributes.java    From SikuliX1 with MIT License 5 votes vote down vote up
private Mat possibleImageResizeOrCallback(Image image, Mat what) {
  Mat originalContent = what;
  if (Settings.ImageCallback != null) {
    Mat contentResized = SXOpenCV.makeMat(Settings.ImageCallback.callback(image), false);
    if (!contentResized.empty()) {
      return contentResized;
    }
  } else {
    double factor = image.resize() == 1 ? Settings.AlwaysResize : image.resize();
    if (factor > 0.1 && factor != 1) {
      return SXOpenCV.cvResize(originalContent.clone(), factor, Image.Interpolation.CUBIC);
    }
  }
  return originalContent;
}
 
Example 18
Source File: example.java    From ImShow-Java-OpenCV with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	/*
	 * Declare a New Frame Here -------------------------
	 */
	Imshow im = new Imshow("Video Preview");
	// This create a Window with Title Video Preview and is autoSized to the image it contains
	// or
	// new Imshow("Title",int Height,int Width); to set custom height and width 
	
	  
	 
	/* You can even Customise the ImShow Frame or Window 
	 * The image is loaded onto a JFrame which is a public member 
	 * so that anyone could customise it
	 * Imshow.Window is the JFrame that one could customise
	 * For example : 
	 * By default the Window is not Resizable so to make it resizable:
	 */
		im.Window.setResizable(true);
	// -------------------------
	Mat m = new Mat();
	VideoCapture vcam = new VideoCapture(0);
	
	// loop until VideoCamera is Available
	while (vcam.isOpened() == false)
		;

	// Bug Fix: Loop until initial image frames are empty
	while (m.empty()) {
		vcam.retrieve(m);
		
	}

	while (true) {
		
		vcam.retrieve(m);
		/***
		 * Show the image
		 */
		//System.out.println(m.dump());
		im.showImage(m);
		/************/
	}
}
 
Example 19
Source File: CaptureVideo.java    From opencv-fun with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void loop (Mat mat, ImgWindow window, VideoCapture video) {
	video.read(mat);
	if (!mat.empty()) {
		window.setImage(mat);
	}
}
 
Example 20
Source File: MainActivity.java    From OpenCV-Android-Object-Detection with MIT License 4 votes vote down vote up
public Mat recognize(Mat aInputFrame) {

        Imgproc.cvtColor(aInputFrame, aInputFrame, Imgproc.COLOR_RGB2GRAY);
        descriptors2 = new Mat();
        keypoints2 = new MatOfKeyPoint();
        detector.detect(aInputFrame, keypoints2);
        descriptor.compute(aInputFrame, keypoints2, descriptors2);

        // Matching
        MatOfDMatch matches = new MatOfDMatch();
        if (img1.type() == aInputFrame.type()) {
            matcher.match(descriptors1, descriptors2, matches);
        } else {
            return aInputFrame;
        }
        List<DMatch> matchesList = matches.toList();

        Double max_dist = 0.0;
        Double min_dist = 100.0;

        for (int i = 0; i < matchesList.size(); i++) {
            Double dist = (double) matchesList.get(i).distance;
            if (dist < min_dist)
                min_dist = dist;
            if (dist > max_dist)
                max_dist = dist;
        }

        LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
        for (int i = 0; i < matchesList.size(); i++) {
            if (matchesList.get(i).distance <= (1.5 * min_dist))
                good_matches.addLast(matchesList.get(i));
        }

        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(good_matches);
        Mat outputImg = new Mat();
        MatOfByte drawnMatches = new MatOfByte();
        if (aInputFrame.empty() || aInputFrame.cols() < 1 || aInputFrame.rows() < 1) {
            return aInputFrame;
        }
        Features2d.drawMatches(img1, keypoints1, aInputFrame, keypoints2, goodMatches, outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
        Imgproc.resize(outputImg, outputImg, aInputFrame.size());

        return outputImg;
    }