简体   繁体   English

无法在Android设备opencv java的中心绘制圆圈?

[英]Unable to draw circle at the center of the android device opencv java?

I am trying to draw a circle on the screen such that it scales up on any android device irrespective of device size. 我正在尝试在屏幕上绘制一个圆圈,以使其在任何android设备上均可放大,而与设备大小无关。 But for some reason, I am unable to draw the circle exactly at the center of the screen. 但是由于某些原因,我无法在屏幕中心精确地绘制圆圈。 This is what I tried so far. 这是我到目前为止尝试过的。

@Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        mrgba = inputFrame.rgba();
       // Imgproc.cvtColor(mrgba,mgray,Imgproc.COLOR_RGB2GRAY);
       // Imgproc.Canny(mgray,mcanny,50,150);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
        float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
        Imgproc.circle (
                mrgba,                 //Matrix obj of the image
                new Point(dpWidth*0.5,dpHeight*0.5),    //Center of the circle
                10,                    //Radius
                new Scalar(0, 0, 255),  //Scalar object for color
                10                     //Thickness of the circle
        );

        return mrgba;
    }

Circle image 圆形图片

To draw a circle on the center of screen with OpenCV don't use DisplayMetrics , but use Mat.height() and Mat.width() : 要使用OpenCV在屏幕中心绘制一个圆,请不要使用DisplayMetrics ,而要使用Mat.height()Mat.width()

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mrgba = inputFrame.rgba();
    Imgproc.circle (
            mrgba,                 //Matrix obj of the image
            new Point(mrgba.width() * 0.5,mrgba.height() * 0.5),    //Center of the circle
            10,                    //Radius
            new Scalar(0, 0, 255),  //Scalar object for color
            10                     //Thickness of the circle
    );

    return mrgba;
}

in case 如果

<org.opencv.android.JavaCameraView
    android:id="@+id/camera_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

or even better get width and height from onCameraViewStarted() params: 甚至更好的是从onCameraViewStarted()参数获取widthheight

@Override
public void onCameraViewStarted(int width, int height) {
    mWidth = width;
    mHeight = height;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM