如何确定感兴趣区域,然后使用 OpenCV 裁剪图像

2024-02-25

我问过类似的问题here https://stackoverflow.com/questions/15690770/extracting-text-from-an-image-with-tesseract但这更多地集中在超立方体上。

我有一个示例图像如下。我想将白色方块作为我的感兴趣区域,然后裁剪掉该部分(方块)并用它创建一个新图像。我将使用不同的图像,因此正方形不会始终位于所有图像中的同一位置。所以我需要以某种方式检测正方形的边缘。

我可以执行哪些预处理方法来获得结果?


使用你的测试图像我能够用一个简单的方法消除所有噪音erosion http://docs.opencv.org/modules/imgproc/doc/filtering.html?highlight=erode#erode手术。

在那之后,一个简单的迭代Mat寻找角点像素这是微不足道的,我在这个答案 https://stackoverflow.com/a/10317919/176769。为了测试目的,我们可以画出green这些点之间的线来显示原始图像中我们感兴趣的区域:

最后,我在原始图像中设置 ROI 并裁剪出那部分。

最终结果如下图所示:

我编写了一个示例代码,使用以下命令执行此任务C++接口OpenCV 的。我对您将此代码翻译为 Python 的能力充满信心。如果你做不到,请忘记代码并坚持使用roadmap http://en.wikipedia.org/wiki/Technology_roadmap我分享了这个答案。

#include <cv.h>
#include <highgui.h>

int main(int argc, char* argv[])
{
    cv::Mat img = cv::imread(argv[1]);
    std::cout << "Original image size: " << img.size() << std::endl;

    // Convert RGB Mat to GRAY
    cv::Mat gray;
    cv::cvtColor(img, gray, CV_BGR2GRAY);
    std::cout << "Gray image size: " << gray.size() << std::endl;

    // Erode image to remove unwanted noises
    int erosion_size = 5;
    cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS,
                                       cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),
                                       cv::Point(erosion_size, erosion_size) );
    cv::erode(gray, gray, element);

    // Scan the image searching for points and store them in a vector
    std::vector<cv::Point> points;
    cv::Mat_<uchar>::iterator it = gray.begin<uchar>();
    cv::Mat_<uchar>::iterator end = gray.end<uchar>();
    for (; it != end; it++)
    {
        if (*it) 
            points.push_back(it.pos()); 
    }

    // From the points, figure out the size of the ROI
    int left, right, top, bottom;
    for (int i = 0; i < points.size(); i++)
    {
        if (i == 0) // initialize corner values
        {
            left = right = points[i].x;
            top = bottom = points[i].y;
        }

        if (points[i].x < left)
            left = points[i].x;

        if (points[i].x > right)
            right = points[i].x;

        if (points[i].y < top)
            top = points[i].y;

        if (points[i].y > bottom)
            bottom = points[i].y;
    }
    std::vector<cv::Point> box_points;
    box_points.push_back(cv::Point(left, top));
    box_points.push_back(cv::Point(left, bottom));
    box_points.push_back(cv::Point(right, bottom));
    box_points.push_back(cv::Point(right, top));

    // Compute minimal bounding box for the ROI
    // Note: for some unknown reason, width/height of the box are switched.
    cv::RotatedRect box = cv::minAreaRect(cv::Mat(box_points));
    std::cout << "box w:" << box.size.width << " h:" << box.size.height << std::endl;

    // Draw bounding box in the original image (debugging purposes)
    //cv::Point2f vertices[4];
    //box.points(vertices);
    //for (int i = 0; i < 4; ++i)
    //{
    //    cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
    //}
    //cv::imshow("Original", img);
    //cv::waitKey(0);

    // Set the ROI to the area defined by the box
    // Note: because the width/height of the box are switched, 
    // they were switched manually in the code below:
    cv::Rect roi;
    roi.x = box.center.x - (box.size.height / 2);
    roi.y = box.center.y - (box.size.width / 2);
    roi.width = box.size.height;
    roi.height = box.size.width;
    std::cout << "roi @ " << roi.x << "," << roi.y << " " << roi.width << "x" << roi.height << std::endl;

    // Crop the original image to the defined ROI
    cv::Mat crop = img(roi);

    // Display cropped ROI
    cv::imshow("Cropped ROI", crop);
    cv::waitKey(0);

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何确定感兴趣区域,然后使用 OpenCV 裁剪图像 的相关文章

  • 多线程 - 比单线程慢

    当我使用多个线程而不是单线程运行程序时 它会变慢 不是应该更快吗 该程序应该遍历从起始目录开始的所有目录 并查找并打印所有名为 X 的文件 代码如下 while done pthread mutex lock lock if list is
  • 在未运行 python 中的函数的情况下检查了非本地语句[重复]

    这个问题在这里已经有答案了 以前我认为当我们定义一个函数时 该函数可能是错误的 但python在执行之前不会检查它 x 100 def f x 1 0 return x print x gt gt gt 100 然而 当我学习的时候nonl
  • 从二进制文件读取字节到 long int

    我有两个问题 我有二进制文件的数据 我想使用 read 函数读取前 8 个字节以签署 long int 但我不能 你知道我该怎么做吗 如何直接读取一块数据到字符串中 我可以像所示那样阅读吗 前任 ifstream is is open te
  • C# 反序列化过程中创建指向父对象的指针

    我有这样的课程 Serializable public class child public Parent parent Serializable public class Parent public List
  • Django 自定义文件存储系统

    我有一个自定义存储 import os from django core files storage import Storage class AlwaysOverwriteFileSystemStorage Storage def get
  • 如何使用 C# 将表格粘贴到 Ms-Word 文档的末尾

    我有一个预制的 Word 模板 其中有一个表格 我想打开它 然后在文档末尾添加 粘贴 另一个表格 问题是它不会转到文档的末尾 而是将新表格粘贴到原始表格的第一个单元格中 任何帮助将不胜感激 previous code copied a ta
  • List 或其他类型上的 string.Join

    我想将整数数组或列表转换为逗号分隔的字符串 如下所示 string myFunction List
  • Django Admin Media 前缀 URL 问题

    我有以下文件夹结构 src BAT templates admin base html src BAT media base css src BAT media admin media base css 设置 py MEDIA ROOT o
  • 如何阻止 Control-I 在 CoreWindow 范围内的 UWP 文本框中插入选项卡?

    当我在 UWP 应用程序中有一个 TextBox 时 对我来说 奇怪的行为 在 Windows 10 中创建通用的空白应用程序 UWP 应用程序 使用以下代码将文本框添加到默认网格
  • 如何在 SQLite 中检查数据库是否存在 C#

    我目前正在用 C 编写一个应用程序 并使用 sqlite 作为嵌入式数据库 我的应用程序在启动时创建一个新数据库 但如何让它检查数据库是否存在 如果它确实存在 我如何让它使用它 如果不存在如何创建一个新数据库 这是我到目前为止所拥有的 pr
  • 从值数组中计算 sympy 表达式

    我正在尝试 sympy 但遇到了一个无法解决的问题 使用 scipy 我可以编写一个表达式并计算 x 值数组 如下所示 import scipy xvals scipy arange 100 100 0 1 f lambda x x 2 f
  • 如何使用“路径”查询 XDocument?

    我想查询一个XDocument给定路径的对象 例如 path to element I want 但我不知道如何继续 您可以使用以下方法System Xml XPath Extensions http msdn microsoft com
  • 在Python中设置Windows命令行终端标题

    我在 Windows 计算机上运行某个 Python 脚本的多个实例 每个实例都来自不同的目录并使用单独的 shell 窗口 不幸的是 Windows 为每个 shell 窗口提供了相同的名称
  • 如何在我的 heroku 应用程序上安装软件包?

    我有一个使用 Shortuuid 的应用程序 https pypi python org pypi shortuuid 0 1 https pypi python org pypi shortuuid 0 1 当我使用 runapp py
  • 子进程调用,它们是并行完成的吗?

    我一直在谷歌搜索这个问题的答案 但似乎没有一个答案 谁能告诉我如果subprocess模块是否并行调用 Python 文档建议它可用于生成新进程 但没有提及它们是否并行 如果它们可以并行完成 您能否给我举一个例子或将我链接到一个例子 这取决
  • 如何获取运行或段落的高度

    我找到了Run or Paragraph in FlowDocument现在我需要知道HEIGHT of it i e while navigator CompareTo flowDocViewer Document ContentEnd
  • 是否可以检测流是否已被客户端关闭?

    简要介绍一下情况 我有一项服务可以通过套接字接收信息并发送回复 连接不安全 我想设置另一个可以为这些连接提供 TLS 的服务 这个新服务将提供单个端口并根据提供的客户端证书分发连接 我不想使用 stunnel 有几个原因 其中之一是每个接收
  • 如何创建实体集或模型而不在数据库中创建相应的表 - 实体框架

    我的 sqlserver 数据库中有一个存储过程 它返回多个结果集 我正在使用 msdn 中的以下链接从实体框架中的 SP 读取多个结果集 https msdn microsoft com en us library jj691402 v
  • gis计算点和多边形/边界之间的距离

    我想使用 python 计算一个点到一个国家边界之间的距离shapely 它应该工作得很好 point distance poly 例如在这里展示查找多边形形状上最近点的坐标 https stackoverflow com question
  • 线程安全的有限大小队列,不使用锁

    我正在尝试编写一个主题队列 但遇到死锁和其他多线程问题 我想用Interlocked CompareExchange避免lock用法 但这段代码并没有按预期工作 它只是擦除整个队列 我在这里做错了什么 public class FixedS

随机推荐