草庐IT

Android OpenCV getPerspectiveTransform 和 warpPerspective

coder 2023-12-04 原文

我对 getPerspectiveTransform 的参数有点困惑,因为我看不到正确的图像。这是我的代码。 original_image 变量是包含我要裁剪并创建新图像(类似 Android OpenCV Find Largest Square or Rectangle 的方形对象)的图像。变量 p1、p2、p3 和 p4 是图像中最大正方形/矩形角的坐标。 p1为左上,p2为右上,p3为右下,p4为左下(顺时针分配)。

    Mat src = new Mat(4,1,CvType.CV_32FC2);
    src.put((int)p1.y,(int)p1.x, (int)p2.y,(int)p2.x, (int)p4.y,(int)p4.x, (int)p3.y,(int)p3.x);
    Mat dst = new Mat(4,1,CvType.CV_32FC2);
    dst.put(0,0, 0,original_image.width(), original_image.height(),original_image.width(), original_image.height(),0);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src, dst);
    Mat cropped_image = original_image.clone();
    Imgproc.warpPerspective(untouched, cropped_image, perspectiveTransform, new Size(512,512));

当我尝试显示 cropped_image 时,我得到一张“我不知道它是什么”的图像。我认为我在 getPerspectiveTransform() 中的参数不正确(或者是)。请帮忙。谢谢!

更新:当我调试我的代码时,我发现我的正方形/矩形的边缘不正确,除了 p4 之外有些是正确的。这是我检测图像中正方形或矩形边缘的代码。除了具有白色轮廓的最大正方形/矩形的轮廓外,我的图像全是黑色。

    //we will find the edges of the new_image (corners of the square/rectangle)
    Point p1 = new Point(10000, 10000); //upper left; minX && minY
    Point p2 = new Point(0, 10000); //upper right; maxX && minY
    Point p3 = new Point(0, 0); //lower right; maxX && maxY
    Point p4 = new Point(10000, 0); //lower left; minX && maxY
    double[] temp_pixel_color;
    for (int x=0; x<new_image.rows(); x++) {
        for (int y=0; y<new_image.cols(); y++) {
            temp_pixel_color = new_image.get(x, y); //we have a black and white image so we only have one color channel
            if (temp_pixel_color[0] > 200) { //we found a white pixel
                if (x<=p1.x && y<=p1.y) { //for p1, minX && minY
                    p1.x = x;
                    p1.y = y;
                }
                else if (x>=p2.x && y<=p2.y) { //for p2, maxX && minY
                    p2.x = x;
                    p2.y = y;
                }
                else if (x>=p3.x && y>=p3.y) { //for p3, maxX && maxY
                    p3.x = x;
                    p3.y = y;
                }
                else if (x<=(int)p4.x && y>=(int)p4.y) { //for p4, minX && maxY
                    p4.x = x;
                    p4.y = y;
                }
            }
        }
    }

这是我的示例图片。忽略检测到边缘后绘制的彩色圆圈:

更新:2013 年 7 月 16 日 我现在可以仅使用最大 4 点轮廓的 approxCurve 来检测角点。这是我的代码:

private Mat 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

    double temp_double[] = maxCurve.get(0, 0);
    Point p1 = new Point(temp_double[0], temp_double[1]);
    Core.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]);
    Core.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]);
    Core.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]);
    Core.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);

    return new_image;
}

这是示例结果图片:

我为正方形/矩形的角绘制了圆圈,我还添加了一个 TextView 来显示所有四个点。

最佳答案

这对我有用。在 src_mat.put 中,您首先应该有 0,0,然后是坐标的浮点值。

    Mat mat=Highgui.imread("inputImage.jpg");
    Mat src_mat=new Mat(4,1,CvType.CV_32FC2);
    Mat dst_mat=new Mat(4,1,CvType.CV_32FC2);


    src_mat.put(0,0,407.0,74.0,1606.0,74.0,420.0,2589.0,1698.0,2589.0);
    dst_mat.put(0,0,0.0,0.0,1600.0,0.0, 0.0,2500.0,1600.0,2500.0);
    Mat perspectiveTransform=Imgproc.getPerspectiveTransform(src_mat, dst_mat);

    Mat dst=mat.clone();

    Imgproc.warpPerspective(mat, dst, perspectiveTransform, new Size(1600,2500));
    Highgui.imwrite("resultImage.jpg", dst);

关于Android OpenCV getPerspectiveTransform 和 warpPerspective,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17637730/

有关Android OpenCV getPerspectiveTransform 和 warpPerspective的更多相关文章

  1. ruby - 在一个进程多个数据库连接 sinatra 应用程序中使用什么 ORM? - 2

    已检查ActiveRecord、DataMapper、Sequel:有些使用全局变量(静态变量)有些需要在使用模型加载源文件之前打开数据库连接。在使用不同数据库的sinatra应用程序中使用哪种ORM更好。 最佳答案 DataMapper专为多数据库使用而设计。你可以通过像DataMapper.setup(:repository_one,"mysql://localhost/my_db_name")这样的方式设置多个存储库。DataMapper随后会跟踪所有已在哈希中设置的存储库,您可以引用该哈希并将其用于范围界定:DataMapp

  2. ruby - 如何在 Sequel ORM 中将行作为数组(而不是哈希)获取? - 2

    在SequelRuby的ORM,Dataset类有一个all方法,它生成一个行散列数组:每一行都是一个以列名作为键的散列。例如,给定一个表T:abc--------------022"Abe"135"Betty"258"Chris"然后:ds=DB['selecta,b,cfromT']ah=ds.all#ArrayofrowHashes应该产生:[{"a":0,"b":22,"c":"Abe"},{"a":1,"b":35,"c":"Betty"},{"a":2,"b":58,"c":"Chris"}]Sequel中是否有一种方法可以代替生成行数组的数组,其中每一行都是一个仅包含每一

  3. ruby - memcached 是否有类似 ORM 的包装器 - 2

    我正在寻找一个ruby​​gem(或rails插件),它以与ActiveRecord抽象SQL细节相同的方式抽象出memcached的细节。我不是正在寻找有助于在memcached中缓存ActiveRecord模型的东西。我确信大约有4215个gem可以帮助解决这个问题。理想情况下,我希望能够执行以下操作:classApple然后能够做类似的事情:my_apple=Apple.find('somememcachedkey')这将在memcached中查找此类的JSON表示并将其反序列化。我也许还能做类似的事情:my_apple.color="red"#persistchangesbac

  4. ruby - 将 ORM 添加到 Sinatra 应用程序;有没有问题少性能好的理想的? - 2

    我希望将ORM添加到我现有的Sinatra应用程序中。尽管我还没有尝试过ActiveRecord,但我了解了Datamapper、Sequel和ActiveRecord。Datamapper看起来很简单,但我一直面临“WhatORMtouseinoneprocessmultipledbconnectionssinatraapplication?”中讨论的问题,但无法理解解决方案和根本原因。对于选择合适的、以性能为导向的ORM有什么建议吗? 最佳答案 Sequel足够快,但功能较少,而ActiveRecord有许多很酷的功能,导致一些

  5. ruby - 在独立的 ruby​​ 应用程序中使用哪个 ruby​​ ORM 框架? - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于StackOverflow来说是偏离主题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,describetheproblem以及迄今为止为解决该问题所做的工作。关闭9年前。Improvethisquestion我想使用带有外键的postgresql来定义数据中的关系,以便其他平台/应用程序也能够轻松地使用相同的数据库。拥有某种ruby​​DSL来定义具有迁移支持的数据库模式也很棒。您会为我推荐哪个框架?是否有某种框架仅用于处理独立于ORM的数

  6. ruby - 用于 Ruby 的 HBase ORM - 2

    存在哪些适用于Ruby的HBaseORM/适配器?哪些是最好的?为什么? 最佳答案 Rhino和Bigrecord似乎有几个月没有事件了。这是一个更活跃的hbaseRubyORM:https://github.com/CompanyBook/massive_record 关于ruby-用于Ruby的HBaseORM,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/3645866/

  7. ruby-on-rails - `autodetect' : No known ORM was detected - 2

    无法使用database_cleaner.rb清理数据;在运行测试时抛出以下问题。/Users/prashanth_sams/.rvm/gems/ruby-2.0.0-p598/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:147:in`autodetect':NoknownORMwasdetected!IsActiveRecord,DataMapper,Sequel,MongoMapper,Mongoid,Moped,orCouchPotato,RedisorOhmloaded?(DatabaseCleaner::N

  8. ruby - 是否有任何使用游标或智能提取的 Ruby ORM? - 2

    我正在寻找一个RubyORM来替代ActiveRecord。我一直在研究Sequel和DataMapper。它们看起来很不错,但是它们似乎都没有做基本的事情:在不需要时不将所有内容加载到内存中。我的意思是我已经在ActiveRecord和Sequel上尝试了以下(或等效的)在有很多行的表上:posts.each{|p|putsp}他们俩都为内存疯狂。他们似乎将所有内容都加载到内存中,而不是在需要时获取内容。我在ActiveRecord中使用了find_in_batches,但这不是一个可接受的解决方案:ActiveRecord不是一个可以接受的解决方案,因为我们在使用它时遇到了太多问题

  9. sql - 何时使用 ORM(Sequel、Datamapper、AR 等)与纯 SQL 进行查询 - 2

    我的一位同事目前正在设计如下所示的SQL查询以生成报告,这些报告通过外部数据查询显示在excel文件中。目前只需要DB上的上报流程(无CRUD操作)。我试图说服他最好使用ruby​​ORM以便能够在rails/sinatra应用程序中显示数据。尽管在显示数据方面有明显的优势,但学习使用像Sequel或Datamapper这样的ORM对他有什么优势?他正在编写的SQL查询显然相当复杂,并且对SQL比较陌生,他经常提示它非常耗时且令人困惑。是否可以使用ORM编写极其复​​杂的查询?如果是这样,哪个最合适(我听说Sequel对遗留数据库有好处)?在进行复杂的数据库查询时,学习Ruby和使用O

  10. javascript - DAO 与 ORM - 在 Sequelize.js 的上下文中解释的概念 - 2

    我最近一直在使用Sequelize.js,并且经常遇到术语“DAO”。来自ActiveRecord(在Rails中),ORM的想法似乎非常简单。谁能给我解释一下DAO是什么?它与ORM有何不同?它如何导致更多模块化代码/防止抽象泄漏?编辑:阅读以下内容后:https://www.reddit.com/r/learnprogramming/comments/32a1fr/what_is_the_general_difference_between_dao_and_orm/感觉/似乎DAO可以被认为是一个单一的“模型”——在ActiveRecord的上下文中,我的用户实例将被认为是一个DA

随机推荐