Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android OpenCV简单形状检测应用程序圆形错误_Java_Android_Opencv_Bitmap - Fatal编程技术网

Java Android OpenCV简单形状检测应用程序圆形错误

Java Android OpenCV简单形状检测应用程序圆形错误,java,android,opencv,bitmap,Java,Android,Opencv,Bitmap,以下代码适用于正方形,但不适用于三角形。为什么? 以下是我的示例代码: public void onClick(View v) { ImageView resim = (ImageView)findViewById(R.id.imgview_gosterge); Bitmap image=((BitmapDrawable)resim.getDrawable()).getBitmap(); Bitmap bmp32 = im

以下代码适用于正方形,但不适用于三角形。为什么?

以下是我的示例代码:

public void onClick(View v) {

            ImageView resim = (ImageView)findViewById(R.id.imgview_gosterge);
            Bitmap image=((BitmapDrawable)resim.getDrawable()).getBitmap();
            Bitmap bmp32 = image.copy(Bitmap.Config.ARGB_8888, true);

            Mat tmp = new Mat (bmp32.getHeight(), bmp32.getWidth(), CvType.CV_8U);
            //Utils.bitmapToMat(image, tmp);
            Utils.bitmapToMat(bmp32, tmp);

            bmp32.recycle();

            Imgproc.cvtColor(tmp, tmp, Imgproc.COLOR_RGB2HSV, 4 );

            tmp = findLargestRectangle(tmp);

            Bitmap bmp = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(tmp, bmp);

            //Utils.matToBitmap(tmp, image);

            ImageView resim2 = (ImageView)findViewById(R.id.imgview_gosterge2);
            resim2.setImageBitmap(bmp);



        }
    });
findLargestRectangle:

findLargestRectangle(Mat original_image) {
        Mat imgSource = original_image;
            //Mat untouched = original_image.clone();
    //convert the image to black and white
    Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

    //convert the image to black and white does (8 bit)
    Imgproc.Canny(imgSource, imgSource, 50, 50);

    //apply gaussian blur to smoothen lines of dots
    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);

    //find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    double maxArea = -1;
    int maxAreaIdx = -1;
    MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    MatOfPoint2f maxCurve = new MatOfPoint2f();
    List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
    for (int idx = 0; idx < contours.size(); idx++) {
        temp_contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(temp_contour);
        //compare this contour to the previous largest contour found
        if (contourarea > maxArea) {
            //check if this contour is a square
            MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
            int contourSize = (int)temp_contour.total();
            Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
            if (approxCurve.total() == 4) {
                maxCurve = approxCurve;
                maxArea = contourarea;
                maxAreaIdx = idx;
                largest_contours.add(temp_contour);
            }
        }
    }


 //create the new image here using the largest detected square
        Mat new_image = new Mat(imgSource.size(), CvType.CV_8U); //we will create a new black blank image with the largest contour
        Imgproc.cvtColor(new_image, new_image, Imgproc.COLOR_BayerBG2RGB);
        Imgproc.drawContours(new_image, contours, maxAreaIdx, new Scalar(255, 255, 255), 1); //will draw the largest square/rectangle


    System.out.println("points length" + temp_contour.toArray().length);

    if( temp_contour.toArray().length == 5)
    {
        System.out.println("Pentagon");
        TextView temp_text = (TextView)findViewById(R.id.temp_text);
        temp_text.setText("\n Beşgen");

    }
    else if(temp_contour.toArray().length > 5)
    {
        System.out.println("Circle");
        TextView temp_text = (TextView)findViewById(R.id.temp_text);
        temp_text.setText("\n Daire");
    }
    else if(temp_contour.toArray().length == 4)
    {
        System.out.println("Square");

        double temp_double[] = maxCurve.get(0, 0);
        Point p1 = new Point(temp_double[0], temp_double[1]);
        Imgproc.circle(new_image, new Point(p1.x, p1.y), 20, new Scalar(255, 0, 0), 5); //p1 is colored red
        String temp_string = "Point 1: (" + p1.x + ", " + p1.y + ")";


        temp_double = maxCurve.get(1, 0);
        Point p2 = new Point(temp_double[0], temp_double[1]);
        Imgproc.circle(new_image, new Point(p2.x, p2.y), 20, new Scalar(0, 255, 0), 5); //p2 is colored green
        temp_string += "\nPoint 2: (" + p2.x + ", " + p2.y + ")";

        temp_double = maxCurve.get(2, 0);
        Point p3 = new Point(temp_double[0], temp_double[1]);
        Imgproc.circle(new_image, new Point(p3.x, p3.y), 20, new Scalar(0, 0, 255), 5); //p3 is colored blue
        temp_string += "\nPoint 3: (" + p3.x + ", " + p3.y + ")";

        temp_double = maxCurve.get(3, 0);
        Point p4 = new Point(temp_double[0], temp_double[1]);
        Imgproc.circle(new_image, new Point(p4.x, p4.y), 20, new Scalar(0, 255, 255), 5); //p1 is colored violet
        temp_string += "\nPoint 4: (" + p4.x + ", " + p4.y + ")";

        TextView temp_text = (TextView)findViewById(R.id.temp_text);
        temp_text.setText(temp_string+"\n Kare");
    }
    else if(temp_contour.toArray().length == 3)
    {
        System.out.println("Triangle");
        TextView temp_text = (TextView)findViewById(R.id.temp_text);
        temp_text.setText("\n Üçgen");
    }




    return new_image;
}
findLargestRectangle(Mat原始图像){
Mat imgSource=原始图像;
//Mat untouched=原始图像.clone();
//将图像转换为黑白
Imgproc.cvt颜色(imgSource,imgSource,Imgproc.COLOR\u bgr2灰色);
//将图像转换为黑白图像(8位)
Imgproc.Canny(imgSource,imgSource,50,50);
//将高斯模糊应用于平滑的点线
Imgproc.GaussianBlur(imgSource,imgSource,新尺寸(5,5,5));
//找到轮廓
列表等高线=新的ArrayList();
Imgproc.findContours(imgSource、contours、new Mat()、Imgproc.RETR\u LIST、Imgproc.CHAIN\u APPROX\u SIMPLE);
双最大面积=-1;
int maxAreaIdx=-1;
MatOfPoint temp_contour=contours.get(0);//最大值位于起点的索引0处
MatOfPoint2f approxCurve=新的MatOfPoint2f();
MatOfPoint2f maxCurve=新的MatOfPoint2f();
列出最大的_等高线=新的ArrayList();
对于(int idx=0;idx最大面积){
//检查此轮廓是否为正方形
MatOfPoint2f new_mat=新MatOfPoint2f(temp_contour.toArray());
int contourSize=(int)temp_contour.total();
Imgproc.approxPolyDP(新垫,approxCurve,轮廓尺寸*0.05,真);
如果(approxCurve.total()==4){
maxCurve=approxCurve;
最大面积=轮廓面积;
maxAreaIdx=idx;
最大等高线。添加(温度等高线);
}
}
}
//使用检测到的最大正方形在此处创建新图像
Mat new_image=new Mat(imgSource.size(),CvType.CV_8U);//我们将创建一个具有最大轮廓的新黑色空白图像
Imgproc.cvtColor(新图像,新图像,Imgproc.COLOR\bg2rgb);
Imgproc.drawContours(新图像,等高线,maxAreaIdx,新标量(255,255,255),1);//将绘制最大的正方形/矩形
System.out.println(“点长度”+温度轮廓toArray().length);
if(temp_contour.toArray().length==5)
{
System.out.println(“五角大楼”);
TextView temp_text=(TextView)findViewById(R.id.temp_text);
temp_text.setText(“\n Beşgen”);
}
否则,如果(温度轮廓toArray().长度>5)
{
System.out.println(“圆圈”);
TextView temp_text=(TextView)findViewById(R.id.temp_text);
临时文本设置文本(“\n Daire”);
}
else if(temp_contour.toArray().length==4)
{
System.out.println(“方形”);
double temp_double[]=maxCurve.get(0,0);
点p1=新点(温度双精度[0],温度双精度[1]);
Imgproc.circle(新的_图像,新的点(p1.x,p1.y),20,新的标量(255,0,0),5);//p1是红色的
String temp_String=“第1点:(“+p1.x+”,“+p1.y+”);
temp_double=maxCurve.get(1,0);
点p2=新点(温度双精度[0],温度双精度[1]);
Imgproc.circle(新的_图像,新的点(p2.x,p2.y),20,新的标量(0,255,0),5);//p2是绿色的
临时字符串+=“\n点2:(“+p2.x+”,“+p2.y+”);
temp_double=maxCurve.get(2,0);
点p3=新点(温度双精度[0],温度双精度[1]);
Imgproc.circle(新的_图像,新的点(p3.x,p3.y),20,新的标量(0,0,255),5);//p3是蓝色的
临时字符串+=“\n点3:(“+p3.x+”,“+p3.y+”);
temp_double=maxCurve.get(3,0);
点p4=新点(温度双精度[0],温度双精度[1]);
Imgproc.circle(新的_图像,新的点(p4.x,p4.y),20,新的标量(0,255,255),5);//p1是紫色的
临时字符串+=“\n点4:(“+p4.x+”,“+p4.y+”);
TextView temp_text=(TextView)findViewById(R.id.temp_text);
temp_text.setText(temp_字符串+“\n Kare”);
}
else if(temp_contour.toArray().length==3)
{
System.out.println(“三角形”);
TextView temp_text=(TextView)findViewById(R.id.temp_text);
临时文本设置文本(“\nÜçgen”);
}
返回新的图像;
}
正方形:

三角形:


请更好地解释为什么这不能像预期的那样工作。我说的是函数是描述的。也许三角形角上的曲线被识别为“轮廓”?是的,它能感知轮廓,如何更正此等高线正方形:三角形:找到三角形143请更好地解释为什么它不能按预期工作ID我把该函数描述了可能三角形拐角处的曲线被识别为“等高线”?是的,它能感知等高线,如何更正此等高线正方形:三角形:找到三角形143