【问题标题】:How to find and draw outer line of image in OpenCV?如何在 OpenCV 中查找和绘制图像的外线?
【发布时间】:2019-10-01 07:15:02
【问题描述】:

当我处理图像时(之前)在图像中给出,然后我需要它的外边框并在 OpenCV android 中绘制红色线(如之后图像)。

【问题讨论】:

  • “高级坦克。”真的让我开心! :) 说真的,是什么阻止您搜索android image outline?我猜你甚至不需要使用 OpenCV。
  • 其实处理完这张图片我需要得到完整的边框坐标。
  • 我会尝试这个算法(至少理论上应该可行):1 - 复制您的位图并对其进行缩放(4?8?16? ) 像素在两个方向上都比原始像素大。 2 - 将其颜色转换为选择的颜色,并可选择使其半透明(我可能会为此使用 ColorMatrix)。 3 - 将原始图像覆盖在这个转换后的副本上。我假设原始位图是透明的 PNG,而不是 JPEG。
  • 我想在获取图像外线坐标后这样,请检查:stackoverflow.com/questions/56006427/…
  • 另一种(更简单的)方法是将坦克图像叠加到坦克剪影上。更简单的是,只有两张图片:有和没有轮廓的坦克。然后根据需要交换图像。

标签: android image opencv counter


【解决方案1】:

这是cv::findContours的经典应用。

documentation 中提供了一个工作示例。

对于您的具体情况,您可以这样做:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

int main(int argc, char** argv)
{
    using namespace cv;
    using namespace std;

    Mat3b image;
    image = imread(argv[1], 1);  
    imshow ("Before", image);

    Mat1b gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    threshold(gray, gray, 250, 255, THRESH_BINARY);
    medianBlur(gray,gray, 3);
    gray = 255-gray;

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(gray, contours, hierarchy, RETR_EXTERNAL , CHAIN_APPROX_NONE);

    Scalar color = Scalar( 0, 0, 255 );
    for (size_t i = 0; i < contours.size(); i++) {
        drawContours(image, contours, i, color, 2, 8, hierarchy, 0, Point());
    }     
    imshow("After", image);
    return 0;
}

这将为您提供以下输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 2014-01-30
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 2014-09-21
    • 2019-02-12
    • 2014-06-01
    相关资源
    最近更新 更多