答案 0 :(得分:2)
为了消除一些非黑噪声,我建议使用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;
}
和形态学关闭。然后,您可以删除包含(例如)超过5%的非黑色像素的行和列。
我尝试了以下代码,它适用于您的示例:
threshVal
请注意,为了在您的所有样品上获得最佳效果,您可能需要调整一些参数:borderThresh
和{{1}}。
另外,您可能希望阅读有关thresholding和morphology transformations的详细教程。
答案 1 :(得分:2)
来自akarsakov的回答。他将裁剪出输入图像的黑色部分。但是,它会将这个裁剪后的图像写成灰度。如果你正在尝试改变并添加这些行。
select count(*) from
(your query)
where column='value to be checked'
我建议勾选akarsakov的答案,因为它绝对有效。这适用于任何想要输出彩色图像的人:)