我正在尝试使用opencv 4 android sdk检测矩形文档。首先,我试图通过找到轮廓来检测它,但是它不适用于多彩色文档。您可以检查此链接以获得更好的主意:
detecting multi color document with OpenCV4Android
我进行了很多研究,发现可以使用houghline变换来完成它。
原始图像-> cvtColor->高斯模糊滤波器->对其进行扩张以锐化边缘->应用分水岭图像分割算法->具有动态otsu阈值的canny边缘检测->然后应用霍夫线变换
我为霍夫线变换所做的是:
Imgproc.HoughLinesP(watershedMat, lines, 1, Math.PI / 180, 50, 100, 50);
List<Line> horizontals = new ArrayList<>();
List<Line> verticals = new ArrayList<>();
for (int x = 0; x < lines.rows(); x++)
{
double[] vec = lines.get(x, 0);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Line line = new Line(start, end);
if (Math.abs(x1 - x2) > Math.abs(y1-y2)) {
horizontals.add(line);
} else if (Math.abs(x2 - x1) < Math.abs(y2 - y1)){
verticals.add(line);
}
}
从上面的水平线和垂直线列表中,我正在找到交点,如下所示:
protected Point computeIntersection (Line l1, Line l2) {
double x1 = l1._p1.x, x2= l1._p2.x, y1 = l1._p1.y, y2 = l1._p2.y;
double x3 = l2._p1.x, x4 = l2._p2.x, y3 = l2._p1.y, y4 = l2._p2.y;
double d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
// double angle = angleBetween2Lines(l1,l2);
Log.e("houghline","angle between 2 lines = "+angle);
Point pt = new Point();
pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
return pt;
}
从那四个交点我画了线。到目前为止,我已经可以通过它检测文件了。见下图:
但是,当其他对象与文档有关时,它也会尝试检测它们。我要从上到下排,从左到右cols找出最大矩形的交点。我遇到以下问题:
如上图所示,当其他物体出现在屏幕上时,它也将对其进行检测。如何仅检测文件?并忽略其他对象?
这是我的原始图片:
任何帮助将不胜感激!提前致谢
最佳答案
一般信息
但是,如果我们要使用某种类型的边缘检测,则绝对应使用原始图像尺寸。
在这里被惩罚。如果您需要特定领域的帮助,您当然可以在评论中寻求帮助。
对问题的一般观察
您以前的方法不起作用的原因有很多。
在找到解决方案之前,需要考虑以下几点:
实际上,对象由看起来很像背景的部分分割。
解决方案
考虑到上述提到的观点,我认为简单的阈值化或边缘检测不会产生任何可靠的结果,尤其是在查看同一场景的不同图像之间的差异时。
作为解决方案,我建议通过LAB或HSV颜色空间进行前景和/或背景色检测和分类。
最突出颜色的样本图像应用于对各个区域进行分类。
例如。对于前景,该书的颜色为深红色和亮红色以及金色/黄色。背景由相当均匀的灰色组成,可用于检测。
潜在算法:
如果由于亮度变化引起的颜色变化成为问题,则请切换至与亮度无关的方法(例如,仅使用AB分量而忽略L分量)
好处:
缺点:
通用解决方案的难点
当前的方法尽管先进,但有一定的理由不能满足一般应用的要求(书籍种类繁多,背景各异等)。
如果您想要一个可以在不同背景下自动检测不同书籍的通用系统,那么您将遇到麻烦。
这达到了难以解决的困难程度。这让我想起了车牌的检测:
照明,噪声,物体弄脏,背景变化很大,对比度差等。
即使您对此进行管理,也很重要:这样的系统仅适用于特定类型的车牌。
这同样适用于您的书。
测验
由于您发布了一个非常类似的问题(detecting multi color document with OpenCV4Android),因此我自由地采取了以下行动:
使用此处张贴的图像以及您在此处提供的图像。
由于其中一张图像仅具有红色ROI,所以我使用我的Photoshop技能等级> 9000来消除红色ROI :)。
用于背景分类的样本图像
用于前景分类的样本图像
图片
背景分类
前景分类
检测到的对象
更新
快速LAB速成类(class)
由于关于色彩空间的理论非常广泛,因此您应该首先阅读一些基本知识和要点。
我的快速搜索找到了这个站点,该站点很好地解释了一些要点:http://www.learnopencv.com/color-spaces-in-opencv-cpp-python/
-我们将使用OpenCV的float变量,因为它是最简单使用的变量(LAB范围未更改,没有缩放,没有调整等)。
-LAB值范围:
L *轴(亮度)的范围是0到100
a *和b *(颜色属性)轴的范围是-128至+127
资料来源和引用:
What are the ranges of coordinates in the CIELAB color space?
http://www.colourphil.co.uk/lab_lch_colour_space.shtml
色距
https://en.wikipedia.org/wiki/Color_difference
本质上,我们使用两种颜色之间的欧几里得距离。
当然,我们可以从我们比较的两种颜色中省去一些成分,例如亮度分量(L)。
为了获得直观的颜色距离度量,我们可以将颜色距离简单地归一化为0.0到1.0之间的范围。
这样,我们可以将颜色距离设置为百分比偏差。
例子
让我们使用上面发布的教程页面中的图像,并在示例中使用它们。
该示例应用程序显示以下内容:
-BGR到LAB的转换
-(L)AB距离计算
-(L)AB距离归一化
-根据BGR/LAB值和颜色距离阈值进行颜色分类
-在不同的照明条件下物体的颜色如何变化
-与其他颜色的距离如何变大/变暗/变暗(如果您仔细阅读所发布的链接,这也将变得很清楚)。
附加提示:
该示例应表明,在强烈变化的照明条件下,单一颜色通常不足以检测颜色对象。
一种解决方案是通过经验分析为每种颜色使用不同的颜色距离阈值。
一种替代方法是为要查找的每种颜色使用许多分类样本颜色。您必须计算色差
每种颜色的颜色,并通过对结果进行或运算来组合找到的值。
代码和图片
(图片来自http://www.learnopencv.com/color-spaces-in-opencv-cpp-python/
-萨蒂亚·马里克(Satya Mallick)的教程)
#include <opencv2/opencv.hpp>
// Normalization factors for (L)AB distance calculation
// LAB range:
// L: 0.0 - 100.0
// A: -128.0 - 127.0
// B: -128.0 - 127.0
static const float labNormalizationFactor = (float)(1.f / (std::sqrt(std::pow(100, 2) + std::pow(255, 2) + std::pow(255, 2))));
static const float abNormalizationFactor = (float)(1.f / (std::sqrt(std::pow(255, 2) + std::pow(255, 2))));
float labExample_calculateLabDistance(const cv::Vec3f& c1, const cv::Vec3f& c2)
{
return (float)cv::norm(c1, c2) * labNormalizationFactor;
}
float labExample_calculateAbDistance(const cv::Vec3f& c1, const cv::Vec3f& c2)
{
cv::Vec2f c1Temp(c1(1), c1(2));
cv::Vec2f c2Temp(c2(1), c2(2));
return (float)cv::norm(c1Temp, c2Temp) * abNormalizationFactor;
}
void labExample_calculateLabDistance(
cv::Mat& imgLabFloat,
cv::Mat& distances,
const cv::Vec3f labColor,
const bool useOnlyAbDistance
)
{
// Get size for general usage
const auto& size = imgLabFloat.size();
distances = cv::Mat::zeros(size, CV_32F);
distances = 1.f;
for (int y = 0; y < size.height; ++y)
{
for (int x = 0; x < size.width; ++x)
{
// Read LAB value
const auto& value = imgLabFloat.at<cv::Vec3f>(y,x);
// Calculate distance
float distanceValue;
if (useOnlyAbDistance)
{
distanceValue = labExample_calculateAbDistance(value, labColor);
}
else
{
distanceValue = labExample_calculateLabDistance(value, labColor);
}
distances.at<float>(y,x) = distanceValue;
}
}
}
// Small hacky function to convert a single
// BGR color value to LAB float.
// Since the conversion function is not directly available
// we just use a Mat object to do the conversion.
cv::Vec3f labExample_bgrUchar2LabFloat(const cv::Scalar bgr)
{
// Build Mat with single bgr pixel
cv::Mat matWithSinglePixel = cv::Mat::zeros(1, 1, CV_8UC3);
matWithSinglePixel.setTo(bgr);
// Convert to float and scale accordingly
matWithSinglePixel.convertTo(matWithSinglePixel, CV_32FC3, 1.0 / 255.0);
// Convert to LAB and return value
cv::cvtColor(matWithSinglePixel, matWithSinglePixel, CV_BGR2Lab);
auto retval = matWithSinglePixel.at<cv::Vec3f>(0, 0);
return retval;
}
void labExample_convertImageBgrUcharToLabFloat(cv::Mat& src, cv::Mat& dst)
{
src.convertTo(dst, CV_32FC3, 1.0 / 255.0);
cv::cvtColor(dst, dst, CV_BGR2Lab);
}
void labExample()
{
// Load image
std::string path = "./Testdata/Stackoverflow lab example/";
std::string filename1 = "1.jpg";
std::string fqn1 = path + filename1;
cv::Mat img1 = cv::imread(fqn1, cv::IMREAD_COLOR);
std::string filename2 = "2.jpg";
std::string fqn2 = path + filename2;
cv::Mat img2 = cv::imread(fqn2, cv::IMREAD_COLOR);
// Combine images by scaling the second image so both images have the same number of columns and then combining them.
float scalingFactorX = (float)img1.cols / img2.cols;
float scalingFactorY = scalingFactorX;
cv::resize(img2, img2, cv::Size(), scalingFactorX, scalingFactorY);
std::vector<cv::Mat> mats;
mats.push_back(img1);
mats.push_back(img2);
cv::Mat img;
cv::vconcat(mats, img);
// Lets use some reference colors.
// Remember: OpenCV uses BGR as default color space so all colors
// are BGR by default, too.
cv::Scalar bgrColorRed(52, 42, 172);
cv::Scalar bgrColorOrange(3, 111, 219);
cv::Scalar bgrColorYellow(1, 213, 224);
cv::Scalar bgrColorBlue(187, 95, 0);
cv::Scalar bgrColorGray(127, 127, 127);
// Build LAB image
cv::Mat imgLabFloat;
labExample_convertImageBgrUcharToLabFloat(img, imgLabFloat);
// Convert bgr ref color to lab float.
// INSERT color you want to analyze here:
auto colorLabFloat = labExample_bgrUchar2LabFloat(bgrColorRed);
cv::Mat colorDistancesWithL;
cv::Mat colorDistancesWithoutL;
labExample_calculateLabDistance(imgLabFloat, colorDistancesWithL, colorLabFloat, false);
labExample_calculateLabDistance(imgLabFloat, colorDistancesWithoutL, colorLabFloat, true);
// Color distances. They can differ for every color being analyzed.
float maxColorDistanceWithL = 0.07f;
float maxColorDistanceWithoutL = 0.07f;
cv::Mat detectedValuesWithL = colorDistancesWithL <= maxColorDistanceWithL;
cv::Mat detectedValuesWithoutL = colorDistancesWithoutL <= maxColorDistanceWithoutL;
cv::Mat imgWithDetectedValuesWithL = cv::Mat::zeros(img.size(), CV_8UC3);
cv::Mat imgWithDetectedValuesWithoutL = cv::Mat::zeros(img.size(), CV_8UC3);
img.copyTo(imgWithDetectedValuesWithL, detectedValuesWithL);
img.copyTo(imgWithDetectedValuesWithoutL, detectedValuesWithoutL);
cv::imshow("img", img);
cv::imshow("colorDistancesWithL", colorDistancesWithL);
cv::imshow("colorDistancesWithoutL", colorDistancesWithoutL);
cv::imshow("detectedValuesWithL", detectedValuesWithL);
cv::imshow("detectedValuesWithoutL", detectedValuesWithoutL);
cv::imshow("imgWithDetectedValuesWithL", imgWithDetectedValuesWithL);
cv::imshow("imgWithDetectedValuesWithoutL", imgWithDetectedValuesWithoutL);
cv::waitKey();
}
int main(int argc, char** argv)
{
labExample();
}
关于opencv - 使用Hough变换OpenCV Android进行矩形文档检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825180/