简体   繁体   English

如何使用C ++在OpenCV中从框架中删除黑色边框?

[英]How to remove black borders from a frame in OpenCV using C++?

I would like to know how to remove the black border from the following frame in OpenCV using C++ 我想知道如何使用C ++从OpenCV的下一帧中删除黑色边框

Original Image 原始图片

Result 结果

Any help would be really appreciated. 任何帮助将非常感激。

To remove some non-black noise I recommend using cv::threshold and morphology closing. 为了消除一些非黑噪声,我建议使用cv::threshold和形态关闭。 Then you can just remove rows and columns which contains (for example) more than 5% non-black pixels. 然后,您可以删除包含(例如)超过5%的非黑色像素的行和列。

I tried following code and it works for your example: 我尝试了以下代码,它适用于您的示例:

int main()
{
  const int threshVal = 20;
  const float borderThresh = 0.05f; // 5%

  cv::Mat img = cv::imread("img.jpg", cv::IMREAD_GRAYSCALE);
  cv::Mat thresholded;
  cv::threshold(img, thresholded, threshVal, 255, cv::THRESH_BINARY);
  cv::morphologyEx(thresholded, thresholded, cv::MORPH_CLOSE,
    cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)),
    cv::Point(-1, -1), 2, cv::BORDER_CONSTANT, cv::Scalar(0));

  cv::imshow("thresholded", thresholded);

  cv::Point tl, br;

  for (int row = 0; row < thresholded.rows; row++)
  {
    if (cv::countNonZero(thresholded.row(row)) > borderThresh * thresholded.cols)
    {
      tl.y = row;
      break;
    }
  }

  for (int col = 0; col < thresholded.cols; col++)
  {
    if (cv::countNonZero(thresholded.col(col)) > borderThresh * thresholded.rows)
    {
      tl.x = col;
      break;
    }
  }

  for (int row = thresholded.rows - 1; row >= 0; row--)
  {
    if (cv::countNonZero(thresholded.row(row)) > borderThresh * thresholded.cols)
    {
      br.y = row;
      break;
    }
  }

  for (int col = thresholded.cols - 1; col >= 0; col--)
  {
    if (cv::countNonZero(thresholded.col(col)) > borderThresh * thresholded.rows)
    {
      br.x = col;
      break;
    }
  }

  cv::Rect roi(tl, br);
  cv::Mat cropped = img(roi);

  cv::imwrite("cropped.jpg", cropped);

  return 0;
}

Please note that in order to get the best results on all your samples you may need to adjust some parameters: threshVal and borderThresh . 请注意,为了在所有样本上获得最佳结果,您可能需要调整一些参数: threshValborderThresh

Also you may want to read good tutorials about thresholding and morphology transformations . 另外,您可能想阅读有关阈值化形态转换的优秀教程。

From akarsakov's answer. 从阿卡萨科夫的答案。 His will crop out the black parts of the input image. 他将裁剪出输入图像的黑色部分。 But, it will write this cropped image in grayscale. 但是,它将以灰度形式写入此裁剪的图像。 If you are after colour try changing and adding these lines. 如果您追求颜色,请尝试更改并添加这些行。

#include "opencv2/opencv.hpp"

using namespace cv;

// Read your input image
Mat img = imread("img.jpg");
// Prepare new grayscale image
Mat input_img_gray;
// Convert to img to Grayscale
cvtColor (img, input_img_gray, CV_RGB2GRAY);
Mat thresholded;
// Threshold uses grayscale image
threshold(input_img_gray, thresholded, threshVal, 255, cv::THRESH_BINARY);

I'd recommend ticking akarsakov's answer because it definitely works. 我建议勾选akarsakov的答案,因为它确实有效。 This is just for anyone looking to output a coloured image :) 这仅适用于希望输出彩色图像的人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM