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

The following examples show how to use org.opencv.core.Mat#submat() . 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: Detectable.java    From FTCVision with MIT License 6 votes vote down vote up
/**
 * Gets the average color of the object
 *
 * @param img      The image matrix, of any color size
 * @param imgSpace The image's color space
 * @return The average color of the region
 */
public Color averageColor(Mat img, ColorSpace imgSpace) {
    //Coerce values to stay within screen dimensions
    double leftX = MathUtil.coerce(0, img.cols() - 1, left());
    double rightX = MathUtil.coerce(0, img.cols() - 1, right());

    double topY = MathUtil.coerce(0, img.rows() - 1, top());
    double bottomY = MathUtil.coerce(0, img.rows() - 1, bottom());

    //Input points into array for calculation
    //TODO rectangular submatrix-based calculation isn't perfectly accurate when you have ellipses or weird shapes
    Mat subMat = img.submat((int) topY, (int) bottomY, (int) leftX, (int) rightX);

    //Calculate average and return new color instance
    return Color.create(Core.mean(subMat), imgSpace);
}
 
Example 2
Source File: Puzzle15Processor.java    From OpenCV-AndroidSamples with MIT License 6 votes vote down vote up
public synchronized void prepareGameSize(int width, int height) {
    mRgba15 = new Mat(height, width, CvType.CV_8UC4);
    mCells15 = new Mat[GRID_AREA];

    for (int i = 0; i < GRID_SIZE; i++) {
        for (int j = 0; j < GRID_SIZE; j++) {
            int k = i * GRID_SIZE + j;
            mCells15[k] = mRgba15.submat(i * height / GRID_SIZE, (i + 1) * height / GRID_SIZE, j * width / GRID_SIZE, (j + 1) * width / GRID_SIZE);
        }
    }

    for (int i = 0; i < GRID_AREA; i++) {
        Size s = Imgproc.getTextSize(Integer.toString(i + 1), 3/* CV_FONT_HERSHEY_COMPLEX */, 1, 2, null);
        mTextHeights[i] = (int) s.height;
        mTextWidths[i] = (int) s.width;
    }
}
 
Example 3
Source File: Tld.java    From OpenTLDAndroid with Apache License 2.0 5 votes vote down vote up
/**
	 * Generate Positive data 
	 * Inputs: 
	 * - good_boxes 
	 * - best_box 
	 * - bbhull
	 * Outputs: 
	 * - Positive fern features (pFerns) 
	 * - Positive NN examples (pExample)
	 */
	void generatePositiveData(final Mat frame, final int numWarps, final Grid aGrid) {
		resizeZeroMeanStdev(frame.submat(aGrid.getBestBox()), _pExample, _params.patch_size);
		//Get Fern features on warped patches
		final Mat img = new Mat();
		Imgproc.GaussianBlur(frame, img, new Size(9, 9), 1.5);
		final BoundingBox bbhull = aGrid.getBBhull();
		final Mat warped = img.submat(bbhull);
		// centre of the hull
		final Point pt = new Point(bbhull.x + (bbhull.width - 1) * 0.5f, bbhull.y + (bbhull.height - 1) * 0.5f);
		
		_pFerns.clear();
		_pPatterns.clear();
		
		for(int i = 0; i < numWarps; i++){
			if(i > 0){
				// this is important as it introduces the necessary noise / fuziness in the initial examples such that the Fern classifier recognises similar shapes not only Exact ones ! 
				// warped is a reference to a subset of the img data, so this will affect the img object
				_patchGenerator.generate(frame, pt, warped, bbhull.size(), _rng);
			}

			final BoundingBox[] goodBoxes = aGrid.getGoodBoxes();
			for(BoundingBox goodBox : goodBoxes){
				final Mat patch = img.submat(goodBox);
				final int[] allFernsHashCodes = _classifierFern.getAllFernsHashCodes(patch, goodBox.scaleIdx);
				_pFerns.add(new Pair<int[], Boolean>(allFernsHashCodes, true));
				
//				// this will be used for display only
//				final Mat tempPattern = new Mat();
//				Imgproc.resize(patch, tempPattern, new Size(_params.patch_size, _params.patch_size));
//				_pPatterns.add(tempPattern);
			}
		}
		
		Log.i(Util.TAG, "Positive examples generated( ferns: " + _pFerns.size() + " NN: 1/n )");
	}
 
Example 4
Source File: TransmissionEstimate.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
public static Mat transEstimateEachChannel(Mat img, int patchSz, double airlight, double lambda, double fTrans) {
	int rows = img.rows();
	int cols = img.cols();
	Mat T = new Mat(rows, cols, img.type());
	for (int i = 0; i < rows; i += patchSz) {
		for (int j = 0; j < cols; j += patchSz) {
			int endRow = i + patchSz > rows ? rows : i + patchSz;
			int endCol = j + patchSz > cols ? cols : j + patchSz;
			Mat blkIm = img.submat(i, endRow, j, endCol);
			double Trans = BlkTransEstimate.blkEstimateEachChannel(blkIm, airlight, lambda, fTrans);
			for (int m = i; m < endRow; m++) for (int n = j; n < endCol; n++) T.put(m, n, Trans);
		}
	}
	return T;
}
 
Example 5
Source File: TransmissionEstimate.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
private static Mat computeTrans (Mat img, int patchSz, int rows, int cols, int type, double[] airlight, double lambda, double fTrans) {
	Mat T = new Mat(rows, cols, type);
	for (int i = 0; i < rows; i += patchSz) {
		for (int j = 0; j < cols; j += patchSz) {
			int endRow = i + patchSz > rows ? rows : i + patchSz;
			int endCol = j + patchSz > cols ? cols : j + patchSz;
			Mat blkIm = img.submat(i, endRow, j, endCol);
			double Trans = BlkTransEstimate.blkEstimate(blkIm, airlight, lambda, fTrans);
			for (int m = i; m < endRow; m++) for (int n = j; n < endCol; n++) T.put(m, n, Trans);
		}
	}
	return T;
}
 
Example 6
Source File: ImageProcessor.java    From Document-Scanner with GNU General Public License v3.0 5 votes vote down vote up
public Result[] zxing( Mat inputImage ) throws ChecksumException, FormatException {

        int w = inputImage.width();
        int h = inputImage.height();

        Mat southEast;

        if (mBugRotate) {
            southEast = inputImage.submat(h-h/4 , h , 0 , w/2 - h/4 );
        } else {
            southEast = inputImage.submat(0, h / 4, w / 2 + h / 4, w);
        }

        Bitmap bMap = Bitmap.createBitmap(southEast.width(), southEast.height(), Bitmap.Config.ARGB_8888);
        org.opencv.android.Utils.matToBitmap(southEast, bMap);
        southEast.release();
        int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];
        //copy pixel data from the Bitmap into the 'intArray' array
        bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());

        LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray);

        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Result[] results = {};
        try {
            results = qrCodeMultiReader.decodeMultiple(bitmap);
        }
        catch (NotFoundException ignored) {
        }
        return results;
    }
 
Example 7
Source File: FaceDetection.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 5 votes vote down vote up
public Eyes getEyes(Mat img){
    double halfWidth = img.cols() / 2;
    double height = img.rows();
    double[] values = new double[4];
    values[0] = 0;
    values[1] = 0;
    values[2] = halfWidth;
    values[3] = height;
    Rect rightHalf = new Rect(values);
    values[0] = halfWidth;
    Rect leftHalf = new Rect(values);
    MatOfRect rightEyes = new MatOfRect();
    MatOfRect leftEyes = new MatOfRect();

    Mat rightHalfImg = img.submat(rightHalf);
    rightEyeDetector.detectMultiScale(rightHalfImg, rightEyes);
    Mat leftHalfImg = img.submat(leftHalf);
    leftEyeDetector.detectMultiScale(leftHalfImg, leftEyes);

    if (rightEyes.empty() || leftEyes.empty() || rightEyes.toArray().length > 1 || leftEyes.toArray().length > 1){
        return null;
    }

    Rect rightEye = rightEyes.toArray()[0];
    Rect leftEye = leftEyes.toArray()[0];

    MatOfFloat rightPoint = new MatOfFloat(rightEye.x + rightEye.width / 2, rightEye.y + rightEye.height / 2);
    MatOfFloat leftPoint = new MatOfFloat(img.cols() / 2 + leftEye.x + leftEye.width / 2, leftEye.y + leftEye.height / 2);

    MatOfFloat diff = new MatOfFloat();
    Core.subtract(leftPoint, rightPoint, diff);
    double angle = Core.fastAtan2(diff.toArray()[1], diff.toArray()[0]);
    double dist = Core.norm(leftPoint, rightPoint, Core.NORM_L2);
    Eyes eyes = new Eyes(dist, rightPoint, leftPoint, angle);
    return eyes;
}
 
Example 8
Source File: Crop.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 5 votes vote down vote up
public PreProcessor preprocessImage(PreProcessor preProcessor) {
    Mat img = preProcessor.getImages().get(0);
    List<Mat> processed = new ArrayList<Mat>();
    if (preProcessor.getFaces().length == 0){
        return null;
    } else {
        for (Rect rect : preProcessor.getFaces()){
            Mat subImg = img.submat(rect);
            processed.add(subImg);
        }
    }
    preProcessor.setImages(processed);
    return preProcessor;
}
 
Example 9
Source File: ImageProcessor.java    From react-native-documentscanner-android with MIT License 5 votes vote down vote up
public Result[] zxing( Mat inputImage ) throws ChecksumException, FormatException {

        int w = inputImage.width();
        int h = inputImage.height();

        Mat southEast;

        if (mBugRotate) {
            southEast = inputImage.submat(h-h/4 , h , 0 , w/2 - h/4 );
        } else {
            southEast = inputImage.submat(0, h / 4, w / 2 + h / 4, w);
        }

        Bitmap bMap = Bitmap.createBitmap(southEast.width(), southEast.height(), Bitmap.Config.ARGB_8888);
        org.opencv.android.Utils.matToBitmap(southEast, bMap);
        southEast.release();
        int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];
        //copy pixel data from the Bitmap into the 'intArray' array
        bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());

        LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray);

        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Result[] results = {};
        try {
            results = qrCodeMultiReader.decodeMultiple(bitmap);
        }
        catch (NotFoundException e) {
        }

        return results;

    }
 
Example 10
Source File: ImageTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Test
public void testSift1() {
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	
	Mat src = Imgcodecs.imread("g:/test/find-src.jpg");
	Mat dst = Imgcodecs.imread("g:/test/find-dest.jpg");
	
	MatOfRect mr = getFace(dst);
	Mat sub = dst.submat(mr.toArray()[0]);
	
	Imgcodecs.imwrite("g:/test/sift-result.jpg", FeatureSiftLannbased(src.t(), sub));
}
 
Example 11
Source File: UtilTest.java    From OpenTLDAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * This is OpenCV functionality, but just need to clarify for myself how it works.
 * Does a Mat created with "submat()" still points to the same underlying data !?
 */
public void testSubmat(){
	final Mat greyMat = new Mat();
	Imgproc.cvtColor(getTestMat(), greyMat, Imgproc.COLOR_RGB2GRAY);
	final Mat submat = greyMat.submat(new Rect(0, 0, 10, 10));
	assertEquals(2, Util.getByte(0,  2, greyMat));
	assertEquals(2, Util.getByte(0,  2, submat));
	
	submat.put(0, 2, new byte[]{33});
	assertEquals(33, Util.getByte(0,  2, greyMat));
	assertEquals(33, Util.getByte(0,  2, submat));		
}
 
Example 12
Source File: Puzzle15Processor.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
public synchronized Mat puzzleFrame(Mat inputPicture) {
    Mat[] cells = new Mat[GRID_AREA];
    int rows = inputPicture.rows();
    int cols = inputPicture.cols();

    rows = rows - rows%4;
    cols = cols - cols%4;

    for (int i = 0; i < GRID_SIZE; i++) {
        for (int j = 0; j < GRID_SIZE; j++) {
            int k = i * GRID_SIZE + j;
            cells[k] = inputPicture.submat(i * inputPicture.rows() / GRID_SIZE, (i + 1) * inputPicture.rows() / GRID_SIZE, j * inputPicture.cols()/ GRID_SIZE, (j + 1) * inputPicture.cols() / GRID_SIZE);
        }
    }

    rows = rows - rows%4;
    cols = cols - cols%4;

    // copy shuffled tiles
    for (int i = 0; i < GRID_AREA; i++) {
        int idx = mIndexes[i];
        if (idx == GRID_EMPTY_INDEX)
            mCells15[i].setTo(GRID_EMPTY_COLOR);
        else {
            cells[idx].copyTo(mCells15[i]);
            if (mShowTileNumbers) {
                Imgproc.putText(mCells15[i], Integer.toString(1 + idx), new Point((cols / GRID_SIZE - mTextWidths[idx]) / 2,
                        (rows / GRID_SIZE + mTextHeights[idx]) / 2), 3/* CV_FONT_HERSHEY_COMPLEX */, 1, new Scalar(255, 0, 0, 255), 2);
            }
        }
    }

    for (int i = 0; i < GRID_AREA; i++)
        cells[i].release();

    drawGrid(cols, rows, mRgba15);

    return mRgba15;
}
 
Example 13
Source File: Crop.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 4 votes vote down vote up
public Mat preprocessImage(Mat img, Rect rect){
    return img.submat(rect);
}
 
Example 14
Source File: Tld.java    From OpenTLDAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * Structure the classifier into 3 stages:
 * a) patch variance
 * b) ensemble of ferns classifier
 * c) nearest neighbour
 */
private Pair<List<DetectionStruct>, List<DetectionStruct>> detect(final Mat frame){
	Log.i(Util.TAG, "[DETECT]");
	
	final List<DetectionStruct> fernClassDetected = new ArrayList<Tld.DetectionStruct>(); //dt
	final List<DetectionStruct> nnMatches = new ArrayList<Tld.DetectionStruct>(); //dbb
	
	
	// 0. Cleaning
	_boxClusterMap.clear();
	
	// 1. DETECTION
	final Mat img = new Mat(frame.rows(), frame.cols(), CvType.CV_8U);
	updateIntegralImgs(frame);
	Imgproc.GaussianBlur(frame, img, new Size(9, 9), 1.5);
	
	// Apply the Variance filter TODO : Bottleneck
	int a=0;
	for(BoundingBox box : _grid){
		// a) speed up by doing the features/ferns check ONLY if the variance is high enough !
		if(Util.getVar(box, _iisumJava, _iisqsumJava, _iiCols) >= _var ){
			a++;
			final Mat patch = img.submat(box);
			final int[] allFernsHashCodes = _classifierFern.getAllFernsHashCodes(patch, box.scaleIdx);
			final double averagePosterior = _classifierFern.averagePosterior(allFernsHashCodes);
			_fernDetectionNegDataForLearning.put(box,  allFernsHashCodes);// store for later use in learning
			
			// b)
			if(averagePosterior > _classifierFern.getFernPosThreshold()){
				fernClassDetected.add(new DetectionStruct(box, allFernsHashCodes, averagePosterior, patch));
			}
		}
	}
	
	Log.i(Util.TAG, a + " Bounding boxes passed the variance filter (" + _var + ")");
	Log.i(Util.TAG, fernClassDetected.size() + " Initial detected from Fern Classifier");
	if(fernClassDetected.size() == 0){
		Log.i(Util.TAG, "[DETECT END]");
		return null;
	}
	
	// keep only the best
	Util.keepBestN(fernClassDetected, MAX_DETECTED, new Comparator<DetectionStruct>() {
		@Override
		public int compare(DetectionStruct detS1, DetectionStruct detS2) {
			return Double.compare(detS1.averagePosterior, detS2.averagePosterior);
		}
	});
	
	
	// 2. MATCHING using the NN classifier  c)
	for(DetectionStruct detStruct : fernClassDetected){
		// update detStruct.patch to params.patch_size and normalise it
		Mat pattern = new Mat();
		resizeZeroMeanStdev(detStruct.patch, pattern, _params.patch_size);
		detStruct.nnConf = _classifierNN.nnConf(pattern);
		
		Log.i(Util.TAG, "NNConf: " + detStruct.nnConf.relativeSimilarity + " / " + detStruct.nnConf.conservativeSimilarity + " Threshold: " + _classifierNN.getNNThreshold());
		// only keep valid boxes
		if(detStruct.nnConf.relativeSimilarity > _classifierNN.getNNThreshold()){
			nnMatches.add(detStruct); 
		}
	}
	
	Log.i(Util.TAG, "[DETECT END]");
	return new Pair<List<DetectionStruct>, List<DetectionStruct>>(fernClassDetected, nnMatches);
}
 
Example 15
Source File: ResistorImageProcessor.java    From ResistorScanner with MIT License 4 votes vote down vote up
public Mat processFrame(CvCameraViewFrame frame)
{
    Mat imageMat = frame.rgba();
    int cols = imageMat.cols();
    int rows = imageMat.rows();

    Mat subMat = imageMat.submat(rows/2, rows/2+30, cols/2 - 50, cols/2 + 50);
    Mat filteredMat = new Mat();
    Imgproc.cvtColor(subMat, subMat, Imgproc.COLOR_RGBA2BGR);
    Imgproc.bilateralFilter(subMat, filteredMat, 5, 80, 80);
    Imgproc.cvtColor(filteredMat, filteredMat, Imgproc.COLOR_BGR2HSV);

    findLocations(filteredMat);

    if(_locationValues.size() >= 3)
    {
        // recover the resistor value by iterating through the centroid locations
        // in an ascending manner and using their associated colour values
        int k_tens = _locationValues.keyAt(0);
        int k_units = _locationValues.keyAt(1);
        int k_power = _locationValues.keyAt(2);

        int value = 10*_locationValues.get(k_tens) + _locationValues.get(k_units);
        value *= Math.pow(10, _locationValues.get(k_power));

        String valueStr;
        if(value >= 1e3 && value < 1e6)
            valueStr = String.valueOf(value/1e3) + " KOhm";
        else if(value >= 1e6)
            valueStr = String.valueOf(value/1e6) + " MOhm";
        else
            valueStr = String.valueOf(value) + " Ohm";

        if(value <= 1e9)
            Core.putText(imageMat, valueStr, new Point(10, 100), Core.FONT_HERSHEY_COMPLEX,
                         2, new Scalar(255, 0, 0, 255), 3);
    }

    Scalar color = new Scalar(255, 0, 0, 255);
    Core.line(imageMat, new Point(cols/2 - 50, rows/2), new Point(cols/2 + 50, rows/2 ), color, 2);
    return imageMat;
}
 
Example 16
Source File: AirlightEstimate.java    From OptimizedImageEnhance with MIT License 4 votes vote down vote up
public static double[] estimate(Mat img, int blockSize) {
	int rows = img.rows();
	int cols = img.cols();
	while (rows * cols > blockSize) {
		int midRow = (int) Math.floor(rows / 2.0);
		int midCol = (int) Math.floor(cols / 2.0);
		// divided image into 4 rectangular region
		Mat[] subIm = new Mat[4];
		subIm[0] = img.submat(0, midRow, 0, midCol); // left-top corner
		subIm[1] = img.submat(midRow, rows, 0, midCol); // right-top corner
		subIm[2] = img.submat(0, midRow, midCol, cols); // left-bottom corner
		subIm[3] = img.submat(midRow, rows, midCol, cols); // right-bottom corner
		// for each sub-image, calculate its score (mean - standard deviation)
		double[] score = new double[4];
		score[0] = calculateScore(subIm[0]);
		score[1] = calculateScore(subIm[1]);
		score[2] = calculateScore(subIm[2]);
		score[3] = calculateScore(subIm[3]);
		int index = 0;
		for (int i = 1; i < score.length; i++) {
			if (score[index] < score[i]) index = i;
		}
		img = subIm[index].clone();
		rows = img.rows();
		cols = img.cols();
	}
	// get the selected region and select the correct air-light
	int index_X = 0;
	int index_Y = 0;
	double pointValue = Double.MAX_VALUE;
	for (int i = 0; i < img.rows(); i++) {
		for (int j = 0; j < img.cols(); j++) {
			double[] data = img.get(i, j);
			double tmpValue = Math.sqrt(Math.pow(data[0] - 255.0, 2.0) + Math.pow(data[1] - 255.0, 2.0) + Math.pow(data[2] - 255.0, 2.0));
			if (pointValue > tmpValue) {
				index_X = i;
				index_Y = j;
				pointValue = tmpValue;
			}
		}
	}
	return img.get(index_X, index_Y);
}
 
Example 17
Source File: EyesDetectionInteractorImpl.java    From Image-Detection-Samples with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Build a template from a specific eye area previously substracted
 * uses detectMultiScale for this area, then uses minMaxLoc method to
 * detect iris from the detected eye</p>
 *
 * @param area Preformatted Area
 * @param size minimum iris size
 * @param grayMat image in gray
 * @param rgbaMat image in color
 * @param detectorEye Haar Cascade classifier
 * @return built template
 */
@NonNull
private static Mat buildTemplate(Rect area, final int size,
                                 @NonNull Mat grayMat,
                                 @NonNull Mat rgbaMat,
                                 CascadeClassifier detectorEye) {
    Mat template = new Mat();
    Mat graySubMatEye = grayMat.submat(area);
    MatOfRect eyes = new MatOfRect();
    Rect eyeTemplate;
    detectorEye.detectMultiScale(graySubMatEye, eyes, 1.15, 2,
            Objdetect.CASCADE_FIND_BIGGEST_OBJECT
                    | Objdetect.CASCADE_SCALE_IMAGE, new Size(EYE_MIN_SIZE, EYE_MIN_SIZE),
            new Size());

    Rect[] eyesArray = eyes.toArray();
    if (eyesArray.length > 0) {
        Rect e = eyesArray[0];
        e.x = area.x + e.x;
        e.y = area.y + e.y;
        Rect eyeRectangle = getEyeArea((int) e.tl().x,
                (int) (e.tl().y + e.height * 0.4),
                e.width,
                (int) (e.height * 0.6));
        graySubMatEye = grayMat.submat(eyeRectangle);
        Mat rgbaMatEye = rgbaMat.submat(eyeRectangle);


        Core.MinMaxLocResult minMaxLoc = Core.minMaxLoc(graySubMatEye);

        FaceDrawerOpenCV.drawIrisCircle(rgbaMatEye, minMaxLoc);
        Point iris = new Point();
        iris.x = minMaxLoc.minLoc.x + eyeRectangle.x;
        iris.y = minMaxLoc.minLoc.y + eyeRectangle.y;
        eyeTemplate = getEyeArea((int) iris.x - size / 2,
                (int) iris.y
                        - size / 2, size, size);
        FaceDrawerOpenCV.drawEyeRectangle(eyeTemplate, rgbaMat);
        template = (grayMat.submat(eyeTemplate)).clone();
    }
    return template;
}