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

The following examples show how to use org.opencv.core.Mat#create() . 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: MainActivity.java    From MOAAP with MIT License 8 votes vote down vote up
void Contours() {
    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat hierarchy = new Mat();

    List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); //A list to store all the contours

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Imgproc.Canny(originalMat, cannyEdges, 10, 100);

    //finding contours
    Imgproc.findContours(cannyEdges, contourList, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    //Drawing contours on a new image
    Mat contours = new Mat();
    contours.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC3);
    Random r = new Random();
    for (int i = 0; i < contourList.size(); i++) {
        Imgproc.drawContours(contours, contourList, i, new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255)), -1);
    }
    //Converting Mat back to Bitmap
    Utils.matToBitmap(contours, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 2
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 6 votes vote down vote up
private void calcBoardCornerPositions(Mat corners) {
    final int cn = 3;
    float positions[] = new float[mCornersSize * cn];

    for (int i = 0; i < mPatternSize.height; i++) {
        for (int j = 0; j < mPatternSize.width * cn; j += cn) {
            positions[(int) (i * mPatternSize.width * cn + j + 0)] =
                    (2 * (j / cn) + i % 2) * (float) mSquareSize;
            positions[(int) (i * mPatternSize.width * cn + j + 1)] =
                    i * (float) mSquareSize;
            positions[(int) (i * mPatternSize.width * cn + j + 2)] = 0;
        }
    }
    corners.create(mCornersSize, 1, CvType.CV_32FC3);
    corners.put(0, 0, positions);
}
 
Example 3
Source File: CameraCalibrator.java    From OpenCV-AndroidSamples with MIT License 6 votes vote down vote up
private double computeReprojectionErrors(List<Mat> objectPoints,
                                         List<Mat> rvecs, List<Mat> tvecs, Mat perViewErrors) {
    MatOfPoint2f cornersProjected = new MatOfPoint2f();
    double totalError = 0;
    double error;
    float viewErrors[] = new float[objectPoints.size()];

    MatOfDouble distortionCoefficients = new MatOfDouble(mDistortionCoefficients);
    int totalPoints = 0;
    for (int i = 0; i < objectPoints.size(); i++) {
        MatOfPoint3f points = new MatOfPoint3f(objectPoints.get(i));
        Calib3d.projectPoints(points, rvecs.get(i), tvecs.get(i),
                mCameraMatrix, distortionCoefficients, cornersProjected);
        error = Core.norm(mCornersBuffer.get(i), cornersProjected, Core.NORM_L2);

        int n = objectPoints.get(i).rows();
        viewErrors[i] = (float) Math.sqrt(error * error / n);
        totalError  += error * error;
        totalPoints += n;
    }
    perViewErrors.create(objectPoints.size(), 1, CvType.CV_32FC1);
    perViewErrors.put(0, 0, viewErrors);

    return Math.sqrt(totalError / totalPoints);
}
 
Example 4
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
void HoughLines() {

        Mat grayMat = new Mat();
        Mat cannyEdges = new Mat();
        Mat lines = new Mat();

        //Converting the image to grayscale
        Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

        Imgproc.Canny(grayMat, cannyEdges, 10, 100);

        Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 50, 20, 20);

        Mat houghLines = new Mat();
        houghLines.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);

        //Drawing lines on the image
        for (int i = 0; i < lines.cols(); i++) {
            double[] points = lines.get(0, i);
            double x1, y1, x2, y2;

            x1 = points[0];
            y1 = points[1];
            x2 = points[2];
            y2 = points[3];

            Point pt1 = new Point(x1, y1);
            Point pt2 = new Point(x2, y2);

            //Drawing lines on an image
            Imgproc.line(houghLines, pt1, pt2, new Scalar(255, 0, 0), 1);
        }

        //Converting Mat back to Bitmap
        Utils.matToBitmap(houghLines, currentBitmap);
        imageView.setImageBitmap(currentBitmap);

    }
 
Example 5
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
void HoughCircles() {
    Mat grayMat = new Mat();
    Mat cannyEdges = new Mat();
    Mat circles = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Imgproc.Canny(grayMat, cannyEdges, 10, 100);

    Imgproc.HoughCircles(cannyEdges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, cannyEdges.rows() / 15);//, grayMat.rows() / 8);

    Mat houghCircles = new Mat();
    houghCircles.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);

    //Drawing lines on the image
    for (int i = 0; i < circles.cols(); i++) {
        double[] parameters = circles.get(0, i);
        double x, y;
        int r;

        x = parameters[0];
        y = parameters[1];
        r = (int) parameters[2];

        Point center = new Point(x, y);

        //Drawing circles on an image
        Imgproc.circle(houghCircles, center, r, new Scalar(255, 0, 0), 1);
    }

    //Converting Mat back to Bitmap
    Utils.matToBitmap(houghCircles, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 6
Source File: PatchGenerator.java    From OpenTLDAndroid with Apache License 2.0 5 votes vote down vote up
/**
	 * 
	 * @param image
	 * @param T
	 * @param patch OUTPUT
	 * @param patchSize
	 */
	void generate(final Mat image, final Mat T, Mat patch, Size patchSize, final RNG rng){
	    patch.create( patchSize, image.type() );
	    if( backgroundMin != backgroundMax ) {
	    	Core.randu(patch, backgroundMin, backgroundMax);
	    	// TODO if that null scalar OK or should it be new Scalar(0) ?
	    	Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_TRANSPARENT, null);
	    } else {
	    	Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT, new Scalar(backgroundMin));
	    }

	    int ksize = randomBlur ? rng.nextInt() % 9 - 5 : 0;
	    if( ksize > 0 ) {
	        ksize = ksize * 2 + 1;
	        Imgproc.GaussianBlur(patch, patch, new Size(ksize, ksize), 0, 0);
	    }

	    if( noiseRange > 0 ) {
	        final Mat noise = new Mat(patchSize, image.type());
	        int delta = (image.depth() == CvType.CV_8U ? 128 : (image.depth() == CvType.CV_16U ? 32768 : 0));
	        Core.randn(noise, delta, noiseRange);
	        
	        // TODO this was different !!
	        Core.addWeighted(patch, 1, noise, 1, -delta, patch);
	        
//	        if( backgroundMin != backgroundMax )
//	            addWeighted(patch, 1, noise, 1, -delta, patch);
//	        else
//	        {
//	            for( int i = 0; i < patchSize.height; i++ )
//	            {
//	                uchar* prow = patch.ptr<uchar>(i);
//	                const uchar* nrow =  noise.ptr<uchar>(i);
//	                for( int j = 0; j < patchSize.width; j++ )
//	                    if( prow[j] != backgroundMin )
//	                        prow[j] = saturate_cast<uchar>(prow[j] + nrow[j] - delta);
//	            }
//	        }
	    }		
	}
 
Example 7
Source File: ProcessHelper.java    From OpenCV-android with Apache License 2.0 4 votes vote down vote up
/**
 * 直线检测
 *
 * @param origin   原始bitmap
 * @param callback 回调
 */
public void hough(Bitmap origin, ProcessCallback callback) {
    if (origin == null) {
        return;
    }
    try {
        Bitmap bitmap = Bitmap.createBitmap(origin.getWidth(), origin.getHeight(), Bitmap.Config.RGB_565);
        Utils.bitmapToMat(origin, rgbMat);
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);
        Mat edges = new Mat();
        Mat src = new Mat(origin.getHeight(), origin.getWidth(), CvType.CV_8UC4);
        Mat lines = new Mat();
        // 拷贝
        Mat origination = new Mat(src.size(), CvType.CV_8UC1);
        src.copyTo(origination);
        // 通过Canny得到边缘图
        Imgproc.cvtColor(origination, grayMat, Imgproc.COLOR_BGR2GRAY);
        Imgproc.Canny(grayMat, edges, 50, 200);
        // 获取直线图
        Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180, 10, 0, 10);
        Mat houghLines = new Mat();
        houghLines.create(edges.rows(), edges.cols(), CvType.CV_8UC1);
        // 绘制直线
        for (int i = 0; i < lines.rows(); i++) {
            double[] points = lines.get(i, 0);
            if (null != points) {
                double x1, y1, x2, y2;
                x1 = points[0];
                y1 = points[1];
                x2 = points[2];
                y2 = points[3];
                Point pt1 = new Point(x1, y1);
                Point pt2 = new Point(x2, y2);
                Imgproc.line(houghLines, pt1, pt2, new Scalar(55, 100, 195), 3);
            }
        }
        Utils.matToBitmap(houghLines, bitmap);
        callback.onSuccess(bitmap);
    } catch (Exception e) {
        callback.onFailed(e.getMessage());
    }
}
 
Example 8
Source File: PatchGenerator.java    From OpenTLDAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param srcCenter
 * @param dstCenter
 * @param transform OUTPUT
 * @param inverse
 */
private void generateRandomTransform(Point srcCenter, Point dstCenter, Mat transform, boolean inverse) {
	MatOfDouble tempRand = new MatOfDouble(0d, 0d);
	Core.randu(tempRand, lambdaMin, lambdaMax);
	final double[] rands = tempRand.toArray();
	final double lambda1 = rands[0];
	final double lambda2 = rands[1];
	Core.randu(tempRand, thetaMin, thetaMax);
	final double theta = tempRand.toArray()[0];
	Core.randu(tempRand, phiMin, phiMax);
	final double phi = tempRand.toArray()[0];
	
	
	// Calculate random parameterized affine transformation A,
    // A = T(patch center) * R(theta) * R(phi)' * S(lambda1, lambda2) * R(phi) * T(-pt)
    final double st = Math.sin(theta);
    final double ct = Math.cos(theta);
    final double sp = Math.sin(phi);
    final double cp = Math.cos(phi);
    final double c2p = cp*cp;
    final double s2p = sp*sp;

    final double A = lambda1*c2p + lambda2*s2p;
    final double B = (lambda2 - lambda1)*sp*cp;
    final double C = lambda1*s2p + lambda2*c2p;

    final double Ax_plus_By = A*srcCenter.x + B*srcCenter.y;
    final double Bx_plus_Cy = B*srcCenter.x + C*srcCenter.y;

    transform.create(2, 3, CvType.CV_64F);
    transform.put(0, 0, A*ct - B*st);
    transform.put(0, 1, B*ct - C*st);
    transform.put(0, 2, -ct*Ax_plus_By + st*Bx_plus_Cy + dstCenter.x);
    transform.put(1, 0, A*st + B*ct);
    transform.put(1, 1, B*st + C*ct);
    transform.put(1, 2, -st*Ax_plus_By - ct*Bx_plus_Cy + dstCenter.y);

    if( inverse ){
        Imgproc.invertAffineTransform(transform, transform);
    }
}