Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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
在Android中使用openCV从图像中的最大矩形提取信息_Android_Opencv_Image Processing_Contour_Rectangles - Fatal编程技术网

在Android中使用openCV从图像中的最大矩形提取信息

在Android中使用openCV从图像中的最大矩形提取信息,android,opencv,image-processing,contour,rectangles,Android,Opencv,Image Processing,Contour,Rectangles,我使用以下代码查找android摄像头拍摄图像的最大矩形: private static Mat findLargestRectangle(Mat original_image) { Mat imgSource = original_image; Bitmap bmpOriOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);

我使用以下代码查找android摄像头拍摄图像的最大矩形:

private static Mat findLargestRectangle(Mat original_image) {
        Mat imgSource = original_image;

        Bitmap bmpOriOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);

        Utils.matToBitmap(imgSource, bmpOriOut);

        try {
            bmpOriOut.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/original.jpg"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //convert the image to black and white, commenting this wont crash
        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2RGB);

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

        //apply gaussian blur to smoothen lines of dots, commenting this crashes
        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();
        Mat largest_contour = contours.get(0);
        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) {
                    maxArea = contourarea;
                    maxAreaIdx = idx;
                    largest_contours.add(temp_contour);
                    largest_contour = temp_contour;
                }
            }
        }
        MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
        largest_contours = new ArrayList<MatOfPoint>();
        largest_contours.add(temp_largest);

        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
        Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1);

        //create the new image here using the largest detected square

        //Toast.makeText(getApplicationContext(), "Largest Contour: ", Toast.LENGTH_LONG).show();
        Bitmap bmpOut = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);

        Utils.matToBitmap(imgSource, bmpOut);

        try {
            bmpOut.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/bigrect.jpg"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return imgSource;
    }
专用静态Mat findLargestRectangle(Mat原始图像){
Mat imgSource=原始图像;
位图bmpOriOut=Bitmap.createBitmap(imgSource.cols(),imgSource.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgSource,bmpOriOut);
试一试{
compress(CompressFormat.JPEG,100,新文件输出流(“/sdcard/mediaAppPhotos/original.jpg”);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//将图像转换为黑白,并评论这不会崩溃
Imgproc.cvtColor(imgSource,imgSource,Imgproc.COLOR_BGR2RGB);
//将图像转换为黑白图像(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();
Mat最大_等高线=等高线。获取(0);
列出最大的_等高线=新的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){
最大面积=轮廓面积;
maxAreaIdx=idx;
最大等高线。添加(温度等高线);
最大等高线=温度等高线;
}
}
}
MatOfPoint temp_Maximum=最大等高线.get(最大等高线.size()-1);
最大_等高线=新阵列列表();
最大等高线。添加(最大温度);
Imgproc.cvtColor(imgSource,imgSource,Imgproc.COLOR_BayerBG2RGB);
Imgproc.drawContours(imgSource,最大的_等高线,-1,新标量(0,255,0),1);
//使用检测到的最大正方形在此处创建新图像
//Toast.makeText(getApplicationContext(),“最大轮廓:”,Toast.LENGTH_LONG.show();
位图bmpOut=Bitmap.createBitmap(imgSource.cols(),imgSource.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(imgSource,bmpOut);
试一试{
compress(CompressFormat.JPEG,100,新文件输出流(“/sdcard/mediaAppPhotos/bigrect.jpg”);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回imgSource;
}
成功检测到图像中最大的矩形,如下所示:

但我的问题是,如何将检测到的最大矩形保存为位图(没有黑白)
非常感谢您的帮助。

不要将其绘制在
imgSource
矩阵上:

Imgproc.drawContours(imgSource, largest_contours, -1, new Scalar(0, 255, 0), 1);
创建一个空的黑/白
Mat
并在其上绘制。类似以下内容(黑色初始矩阵):


当然,您可以使用
Mat
构造函数来创建所需的初始背景矩阵。

我现在正在尝试,您能否告诉我如何将其保存为RGB图像而不是黑白图像。您的代码中已经有了它。您可以将
cvtColor
Imgproc.COLOR\u GRAY2RGB
一起使用;或者在初始化时,使用3个通道,而不是
imgSource.channels()
,后者有1个通道。openCV SDK提供的名为“openCV示例-颜色斑点检测”的示例项目根据颜色识别区域。但不可能提取该区域。你能告诉我吗?我记不清那个例子了。你能告诉我它是如何“识别”这个地区的吗?如果它与该区域的轮廓一起工作,那么它已经在进行提取,剩下的唯一工作就是如上所述在空矩阵上绘制轮廓。请运行openCV SDK提供的名为“openCV采样-颜色斑点检测”的项目……我没有经验说明如何检测该区域。我只想提取检测到的区域..请帮助!
emptyMat = Mat.zeros(imgSource.size(), imgSource.channels());
...
Imgproc.drawContours(emptyMat, largest_contours, -1, new Scalar(0, 255, 0), 1);