【问题标题】:crop triangle from blackbackground从黑色背景裁剪三角形
【发布时间】:2017-04-29 06:32:53
【问题描述】:

我是图像处理和开发的新手。我有一个黑色背景的三角形。我想将该三角形保存为没有黑色像素 [0] 的 Mat 对象。为了做到这一点,我尝试如下。

  1. 设置阈值
  2. 寻找轮廓
  3. 将轮廓 [0] 识别为三角形 // 有 2 个轮廓,一个是三角形,另一个是背面像素。
  4. 保存轮廓点
  5. 裁剪图像。

我的代码请在下面找到。

 Mat finalImage = imread("test.png, CV_LOAD_IMAGE_GRAYSCALE);

        img.copyTo(finalImage, mask);

        Mat canny_output;
        vector<vector<Point> > contours;
        vector<Vec4i> hierarchy;
        int thresh = 100;
        int max_thresh = 255;
        RNG rng(12345);

        /// Detect edges using canny
        Canny(finalImage, canny_output, thresh, thresh * 2, 3);
        /// Find contours
        findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //find this method

        /// Draw contours
        Mat drawing = Mat::zeros(canny_output.size(), CV_8UC1);
        for (int i = 0; i< contours.size(); i++)
        {
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); // find this method.
        }

我有轮廓点,但通过使用轮廓点,我不知道如何仅裁剪 输入图像中的三角形

【问题讨论】:

  • 这个问题你已经问过几次了。 1) 图像必须是矩形,因此您不能拥有三角形图像,有什么不清楚的地方? 2)在矩形图像中,可以使用alpha通道作为遮罩,例如设置透明三角形外的所有像素?
  • 三木是对的,图片不能是三角形的。请详细说明您到底需要什么。

标签: c++ opencv


【解决方案1】:

您可以在重新绘制轮廓的同时获得各种轮廓的边界矩形。因此,在您迭代轮廓的 for 循环中,您可以使用 cv::boundingRect() 来获取相应轮廓的边界 Rect:

/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC1);
for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); // find this method.
        cv::Rect boundingRect = cv::boundingRect(contours[i]);
    }

【讨论】:

  • 但是这种方法给出了矩形内的三角形。矩形有黑色像素。我只想隔离三角形
  • 然后请附上预期的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 2016-02-11
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多