Java Code Examples for org.opencv.imgproc.Imgproc#circle()

The following examples show how to use org.opencv.imgproc.Imgproc#circle() . 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: ProcessHelper.java    From OpenCV-android with Apache License 2.0 6 votes vote down vote up
/**
 * 角点检测
 *
 * @param origin   原始bitmap
 * @param callback 回调
 */
public void harris(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 corners = new Mat();
        Mat tempDst = new Mat();
        Mat tempDstNorm = new Mat();
        // 找出角点
        Imgproc.cornerHarris(grayMat, tempDst, 2, 3, 0.04);
        // 归一化Harris角点的输出
        Core.normalize(tempDst, tempDstNorm, 0, 255, Core.NORM_MINMAX);
        Core.convertScaleAbs(tempDstNorm, corners);
        // 绘制角点
        Random random = new Random();
        for (int i = 0; i < tempDstNorm.cols(); i++) {
            for (int j = 0; j < tempDstNorm.rows(); j++) {
                double[] value = tempDstNorm.get(j, i);
                if (value[0] > 250) {
                    // 决定了画出哪些角点,值越大选择画出的点就越少
                    Imgproc.circle(corners, new Point(i, j), 5, new Scalar(random.nextInt(255)), 2);
                }
            }
        }
        Utils.matToBitmap(corners, bitmap);
        callback.onSuccess(bitmap);
    } catch (Exception e) {
        callback.onFailed(e.getMessage());
    }
}
 
Example 2
Source File: ProcessorsTest.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testContoursFinder() throws Exception {
    cf.process(sourceContours);
    int num = cf.getNumBees();
    assertEquals(1, num);
    // Add another bee
    Imgproc.circle(sourceContours, new Point(150, 150), 10, new Scalar(255), -1);
    cf.process(sourceContours);
    num = cf.getNumBees();
    assertEquals(2, num);
    // Add an object with area out of rage (max)
    Imgproc.circle(sourceContours, new Point(300, 300), 15, new Scalar(255), -1);
    cf.process(sourceContours);
    num = cf.getNumBees();
    assertEquals(2, num);
    // Add an object with area out of rage (min)
    Imgproc.circle(sourceContours, new Point(50, 50), 2, new Scalar(255), -1);
    cf.process(sourceContours);
    num = cf.getNumBees();
    assertEquals(2, num);
}
 
Example 3
Source File: PaintUtils.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * 画实心圆
 *
 * @param src
 * @param point
 *            点
 * @param size
 *            点的尺寸
 * @param scalar
 *            颜色
 * @param path
 *            保存路径
 */
public static Mat paintCircle(Mat src, Point[] point, int size, Scalar scalar, String path) {
	if (src == null || point == null) {
		throw new RuntimeException("Mat 或者 Point 数组不能为NULL");
	}
	for (Point p : point) {
		Imgproc.circle(src, p, size, scalar, -1);
	}

	if (path != null && !"".equals(path)) {
		GeneralUtils.saveImg(src, path);
	}

	return src;
}
 
Example 4
Source File: FaceDrawerOpenCV.java    From Image-Detection-Samples with Apache License 2.0 5 votes vote down vote up
public static void drawFaceShapes(Rect face, Mat matrixRGBA) {
    Point start = face.tl();
    int h = (int) start.y + (face.height / 2);
    int w = (int) start.x + (face.width / 2);
    Imgproc.rectangle(matrixRGBA, start, face.br(),
            FACE_RECT_COLOR, 3);
    Point center = new Point(w, h);
    Imgproc.circle(matrixRGBA, center, 10, new Scalar(255, 0, 0, 255), 3);
}
 
Example 5
Source File: MainActivity.java    From real_time_circle_detection_android with MIT License 5 votes vote down vote up
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat input = inputFrame.gray();
    Mat circles = new Mat();
    Imgproc.blur(input, input, new Size(7, 7), new Point(2, 2));
    Imgproc.HoughCircles(input, circles, Imgproc.CV_HOUGH_GRADIENT, 2, 100, 100, 90, 0, 1000);

    Log.i(TAG, String.valueOf("size: " + circles.cols()) + ", " + String.valueOf(circles.rows()));

    if (circles.cols() > 0) {
        for (int x=0; x < Math.min(circles.cols(), 5); x++ ) {
            double circleVec[] = circles.get(0, x);

            if (circleVec == null) {
                break;
            }

            Point center = new Point((int) circleVec[0], (int) circleVec[1]);
            int radius = (int) circleVec[2];

            Imgproc.circle(input, center, 3, new Scalar(255, 255, 255), 5);
            Imgproc.circle(input, center, radius, new Scalar(255, 255, 255), 2);
        }
    }

    circles.release();
    input.release();
    return inputFrame.rgba();
}
 
Example 6
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 7
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
void HarrisCorner() {
    Mat grayMat = new Mat();
    Mat corners = new Mat();

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

    Mat tempDst = new Mat();
    //finding contours
    Imgproc.cornerHarris(grayMat, tempDst, 2, 3, 0.04);

    //Normalizing harris corner's output
    Mat tempDstNorm = new Mat();
    Core.normalize(tempDst, tempDstNorm, 0, 255, Core.NORM_MINMAX);
    Core.convertScaleAbs(tempDstNorm, corners);

    //Drawing corners on a new image
    Random r = new Random();
    for (int i = 0; i < tempDstNorm.cols(); i++) {
        for (int j = 0; j < tempDstNorm.rows(); j++) {
            double[] value = tempDstNorm.get(j, i);
            if (value[0] > 150)
                Imgproc.circle(corners, new Point(i, j), 5, new Scalar(r.nextInt(255)), 2);
        }
    }

    //Converting Mat back to Bitmap
    Utils.matToBitmap(corners, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 8
Source File: ProcessorsTest.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create needed mats and instances.
 */
@Before
public void setUp() throws Exception {
    // Image to test
    source = new Mat(4, 4, CvType.CV_8U) {
        {
            put(0, 0, 0, 0, 0, 0);
            put(1, 0, 0, 255, 255, 0);
            put(2, 0, 0, 255, 255, 0);
            put(3, 0, 0, 0, 0, 0);
        }
    };
    // Image with a circle with area=314 (simulating a bee)
    sourceContours = new Mat(480, 640, CvType.CV_8U, new Scalar(0));
    Imgproc.circle(sourceContours, new Point(200, 200), 10, new Scalar(255), -1);
    // Expected result for blur
    targetBlur = new Mat(4, 4, CvType.CV_8U) {
        {
            put(0, 0, 100, 110, 110, 100);
            put(1, 0, 110, 120, 120, 110);
            put(2, 0, 110, 120, 120, 110);
            put(3, 0, 100, 110, 110, 100);
        }
    };
    // Black image
    black = new Mat(4, 4, CvType.CV_8U, new Scalar(0));
    // Instaciate classes
    blur = new Blur();
    bs = new BackgroundSubtractor(); // To test default constructor
    bs = new BackgroundSubtractor(50, 0.7);
    morf = new Morphology();
    cf = new ContoursFinder(); // To test default constructor
    cf = new ContoursFinder(16, 600);
}
 
Example 9
Source File: FaceDrawerOpenCV.java    From Image-Detection-Samples with Apache License 2.0 4 votes vote down vote up
public static void drawIrisCircle(Mat matrixRgba, Core.MinMaxLocResult minMaxLocResult) {
    Imgproc.circle(matrixRgba, minMaxLocResult.minLoc, 2, new Scalar(255, 255, 255, 255), 2);
}
 
Example 10
Source File: MainActivity.java    From MOAAP with MIT License 4 votes vote down vote up
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        final int viewMode = mViewMode;
        switch (viewMode) {
            case VIEW_MODE_OPTICAL_FLOW:
                mGray = inputFrame.gray();
                if(features.toArray().length==0){
                    int rowStep = 50, colStep = 100;
                    int nRows = mGray.rows()/rowStep, nCols = mGray.cols()/colStep;

//                    Log.d(TAG, "\nRows: "+nRows+"\nCols: "+nCols+"\n");

                    Point points[] = new Point[nRows*nCols];
                    for(int i=0; i<nRows; i++){
                        for(int j=0; j<nCols; j++){
                            points[i*nCols+j]=new Point(j*colStep, i*rowStep);
//                            Log.d(TAG, "\nRow: "+i*rowStep+"\nCol: "+j*colStep+"\n: ");
                        }
                    }

                    features.fromArray(points);

                    prevFeatures.fromList(features.toList());
                    mPrevGray = mGray.clone();
                    break;
                }

                nextFeatures.fromArray(prevFeatures.toArray());
                Video.calcOpticalFlowPyrLK(mPrevGray, mGray, prevFeatures, nextFeatures, status, err);

                List<Point> prevList=features.toList(), nextList=nextFeatures.toList();
                Scalar color = new Scalar(255);

                for(int i = 0; i<prevList.size(); i++){
//                    Core.circle(mGray, prevList.get(i), 5, color);
                    Imgproc.line(mGray, prevList.get(i), nextList.get(i), color);
                }

                mPrevGray = mGray.clone();
                break;
            case VIEW_MODE_KLT_TRACKER:
                mGray = inputFrame.gray();

                if(features.toArray().length==0){
                    Imgproc.goodFeaturesToTrack(mGray, features, 10, 0.01, 10);
                    Log.d(TAG, features.toList().size()+"");
                    prevFeatures.fromList(features.toList());
                    mPrevGray = mGray.clone();
//                    prevFeatures.fromList(nextFeatures.toList());
                    break;
                }

//                OpticalFlow(mPrevGray.getNativeObjAddr(), mGray.getNativeObjAddr(), prevFeatures.getNativeObjAddr(), nextFeatures.getNativeObjAddr());
                Video.calcOpticalFlowPyrLK(mPrevGray, mGray, prevFeatures, nextFeatures, status, err);
                List<Point> drawFeature = nextFeatures.toList();
//                Log.d(TAG, drawFeature.size()+"");
                for(int i = 0; i<drawFeature.size(); i++){
                    Point p = drawFeature.get(i);
                    Imgproc.circle(mGray, p, 5, new Scalar(255));
                }
                mPrevGray = mGray.clone();
                prevFeatures.fromList(nextFeatures.toList());
                break;
            default: mViewMode = VIEW_MODE_KLT_TRACKER;
        }

        return mGray;
    }
 
Example 11
Source File: ShowCameraViewActivity.java    From FaceT with Mozilla Public License 2.0 4 votes vote down vote up
private void touchToCapture()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String currentDateandTime = sdf.format(new Date());
        String saveDir = Environment.getExternalStorageDirectory().getPath() + "/DCIM/OCV/TouchSave";
        File dirCheck = new File(saveDir);
        if (!dirCheck.exists()) {
            dirCheck.mkdirs();
        }
        String fileName = saveDir + "/touch_picture_" + currentDateandTime + ".jpg";
        try {
            mOpenCvCameraView.takePicture(fileName);

            Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        last_photo_name = fileName;

        int cols = mRgba.cols();
        int rows = mRgba.rows();

        int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
        int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

//        int x = (int)event.getX() - xOffset;
//        int y = (int)event.getY() - yOffset;

//the middle point of the face detection
        int x = (int) face_middle_x - xOffset;
        int y = (int) face_middle_y - yOffset;
        Imgproc.circle(mRgba, new Point(x, y), 10, FACE_RECT_COLOR);
        Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");
        Log.i(TAG, "Offset coordinates: (" + xOffset + ", " + yOffset + ")");


        if ((x > 0) || (y > 0) || (x <= cols) || (y <= rows)) {
            Rect touchedRect = new Rect();

            touchedRect.x = (x > 4) ? x - 4 : 0;
            touchedRect.y = (y > 4) ? y - 4 : 0;

            touchedRect.width = (x + 4 < cols) ? x + 4 - touchedRect.x : cols - touchedRect.x;
            touchedRect.height = (y + 4 < rows) ? y + 4 - touchedRect.y : rows - touchedRect.y;

            Mat touchedRegionRgba = mRgba.submat(touchedRect);

            Mat touchedRegionHsv = new Mat();
            Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);

//        // Calculate average color of touched region
//        mBlobColorHsv = Core.sumElems(touchedRegionHsv);
//        int pointCount = touchedRect.width*touchedRect.height;
//        for (int i = 0; i < mBlobColorHsv.val.length; i++)
//            mBlobColorHsv.val[i] /= pointCount;
//
//        //Add a Toast to display the HSV color
//        Toast.makeText(this, "HSV = " + mBlobColorHsv , Toast.LENGTH_SHORT).show();
//        mBlobColorRgba = converScalarHsv2Rgba(mBlobColorHsv);
//
//        Log.i(TAG, "Touched rgba color: (" + mBlobColorRgba.val[0] + ", " + mBlobColorRgba.val[1] +
//                ", " + mBlobColorRgba.val[2] + ", " + mBlobColorRgba.val[3] + ")");
//
//        mDetector.setHsvColor(mBlobColorHsv);
//
//        Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE);
//
//        mIsColorSelected = true;

            touchedRegionRgba.release();
            touchedRegionHsv.release();
        }
    }
 
Example 12
Source File: Drawing.java    From FTCVision with MIT License 4 votes vote down vote up
private static void drawCircle(Mat img, Point center, int diameter, Color color, int thickness) {
    Imgproc.circle(img, center, diameter, color.getScalarRGBA(), thickness);
}