C++ 如何使用C+从OpenCV中的框架中删除黑色边框+;?
我想知道如何使用C从OpenCV的以下框架中删除黑色边框++C++ 如何使用C+从OpenCV中的框架中删除黑色边框+;?,c++,image,opencv,crop,C++,Image,Opencv,Crop,我想知道如何使用C从OpenCV的以下框架中删除黑色边框++ 非常感谢您的帮助。要去除一些非黑噪声,我建议使用cv::threshold和形态学关闭。然后,您可以删除包含(例如)超过5%非黑色像素的行和列 我尝试了以下代码,它适用于您的示例: int main() { const int threshVal = 20; const float borderThresh = 0.05f; // 5% cv::Mat img = cv::imread("img.jpg", cv::
非常感谢您的帮助。要去除一些非黑噪声,我建议使用
cv::threshold
和形态学关闭。然后,您可以删除包含(例如)超过5%非黑色像素的行和列
我尝试了以下代码,它适用于您的示例:
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;
}
intmain()
{
常数int threshVal=20;
常量浮点边界阈值=0.05f;//5%
cv::Mat img=cv::imread(“img.jpg”,cv::imread\u灰度);
cv::设定阈值的Mat;
cv::threshold(img,阈值化,ThresholdVal,255,cv::threshold_二进制);
cv::morphologyEx(阈值化、阈值化、cv::变形关闭、,
cv::getStructuringElement(cv::MORPH_RECT,cv::Size(3,3)),
cv::Point(-1,-1),2,cv::BORDER_常量,cv::Scalar(0));
cv::imshow(“阈值化”,阈值化);
cv::tl点,br;
对于(int row=0;rowborderthreshold*threshold.cols)
{
tl.y=行;
打破
}
}
for(int col=0;colborderthreshold*threshold.rows)
{
tl.x=col;
打破
}
}
对于(int row=threshold.rows-1;row>=0;row--)
{
if(cv::countNonZero(threshold.row(row))>borderthreshold*threshold.cols)
{
br.y=行;
打破
}
}
对于(int col=threshold.cols-1;col>=0;col--)
{
if(cv::countNonZero(threshold.col(col))>borderthreshold*threshold.rows)
{
br.x=col;
打破
}
}
cv:Rect roi(tl,br);
cv::Mat裁剪=img(投资回报率);
cv::imwrite(“crapped.jpg”,crapped);
返回0;
}
请注意,为了在所有样本上获得最佳结果,您可能需要调整一些参数:threshVal
和borderThresh
此外,您可能还想阅读有关和的优秀教程。来自akarsakov的答案。His将裁剪出输入图像的黑色部分。但是,它将以灰度方式写入此裁剪图像。如果你想改变颜色,试着添加这些线条
#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);
我建议勾选阿卡萨科夫的答案,因为它确实有效。这仅适用于任何希望输出彩色图像的人:)正如您所知,任何尝试此操作的人,图像顶部都有一条浅灰色的线,这在一定程度上扰乱了自动方法。这是故意的吗?这帧是红外视频的一部分。灰线不是故意的。图像和黑色边框的大小是恒定的吗?如果是的话,你可以用一个。