gpt4 book ai didi

Android OpenCV 无法检测圆圈

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:38 24 4
gpt4 key购买 nike

目前我正在开发 Android 应用程序来检测相机 View 中的圆圈。我是 OpenCV 的新手,现在我正在尝试检测图像中的圆圈而不是初学者的相机。我写了这段代码:

Bitmap photo = BitmapFactory.decodeResource(getResources(), 
R.drawable.circle);

Toast.makeText(getBaseContext(), "It works!", Toast.LENGTH_LONG).show();
Mat imgSource = new Mat(), imgCirclesOut = new Mat();

Utils.bitmapToMat(photo , imgSource);

Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur( imgSource, imgSource, new Size(9, 9), 2, 2 );
Imgproc.HoughCircles( imgSource, imgCirclesOut, Imgproc.CV_HOUGH_GRADIENT, 1, imgSource.rows()/8, 200, 100, 0, 0 );

float circle[] = new float[3];

for (int i = 0; i < imgCirclesOut.cols(); i++)
{
imgCirclesOut.get(0, i, circle);
org.opencv.core.Point center = new org.opencv.core.Point();
center.x = circle[0];
center.y = circle[1];
Core.circle(imgSource, center, (int) circle[2], new Scalar(0,0,255), 5);
}
Bitmap bmp = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);

Utils.matToBitmap(imgSource, bmp);




image.setImageBitmap(bmp);

当我按下一个按钮时,它就起作用了。根据所有问题、教程等,我检查过它应该可以工作并检测到我的圆圈,但它所做的只是将我的图像处理为灰度。这是它在我的应用中的样子:

Before click After click

帮助。

最佳答案

你需要先加载opencv native so's,然后才能执行任何opencv相关代码:

private BaseLoaderCallback  mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// either call your opencv code from **here**
// or from onCameraViewStarted(). either way, you will have
// to wait, until this thing finished (async!)
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};


@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

关于Android OpenCV 无法检测圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com