简体   繁体   English

opencv.js大津门槛

[英]opencv.js Otsu threshold

I'm using opencv.js. 我正在使用opencv.js。

I'm trying to find the best way to implement a binary filter on images taken from a mobile phone, in order to clearly see text on the image. 我正在尝试找到对从手机拍摄的图像实施二进制过滤器的最佳方法,以便清楚地看到图像上的文字。 Adaptive Thresholds have not been great - too much noise. 自适应阈值并不是很大 - 噪音太大。 I'm trying to get an Otsu threshold to work and I'm failing. 我试图让Otsu门槛起作用而且我失败了。

From tutorials in the other OpenCV docs it looks like you'd ideally do a Gaussian Blur, then do an Otsu threshold (passing 0 in as the threshold value). 其他OpenCV文档中的教程看起来你理想情况下你会做一个高斯模糊,然后做一个Otsu阈值(将0输入作为阈值)。

For threshold value, simply pass zero. 对于阈值,只需传递零。 Then the algorithm finds the optimal threshold value and returns you as the second output, retVal 然后算法找到最佳阈值并返回第二个输出retVal

When I use 0 as the threshold value I get an all white result. 当我使用0作为阈值时,我得到全白结果。 If I pass in something reasonable (eg 128), I get a decent result, but some darker areas of the image are washed out. 如果我传入一些合理的东西(例如128),我会得到一个不错的结果,但是图像的一些较暗区域会被淘汰。 How can I get this "Optimal Threshold" (pass zero) to work for opencv.js? 我怎样才能获得这个“最佳阈值”(传递零)来为opencv.js工作?

let src = cv.imread('canvasInput');
let dst = new cv.Mat();
let ksize = new cv.Size(3, 3);

//Blur & conver to gray
cv.GaussianBlur(src, dst, ksize, 0);
cv.cvtColor(dst, dst, cv.COLOR_BGR2GRAY, 0);


/* PROBLEM HERE: 
*    passing 0 returns all white, passing 128 is a reasonable result
*/
cv.threshold(dst, dst, 0, 255, cv.THRESH_BINARY&cv.THRESH_OTSU);
cv.imshow('canvasOutput', dst);

If the image has more content than just the text, a global thresholding method (such as Otsu) will fail. 如果图像的内容多于文本的内容,则全局阈值方法(如Otsu)将失败。

Keep your adaptive threshold but add a small positive or negative constant to the threshold(s). 保持自适应阈值,但在阈值上添加一个小的正或负常数。

There is no AUTOMATIC WAY to get "Optimal Threshold". 没有自动方式来获得“最佳阈值”。

But you can try to use Adaptive Threshold and I think CLAHE maybe helpful in thresholding issues. 但您可以尝试使用自适应阈值 ,我认为CLAHE可能有助于阈值处理问题。

The problem is this line 问题是这一行

cv.threshold(dst, dst, 0, 255, cv.THRESH_BINARY&cv.THRESH_OTSU);

Since cv.THRESH_BINARY & cv.THRESH_OTSU = 0, 由于cv.THRESH_BINARY&cv.THRESH_OTSU = 0,

you actually doing this 你实际上是这样做的

cv.threshold(dst, dst, 0, 255, 0);   <===== 0 = cv.THRESH_BINARY

To append a flag to another flag should use "OR" operator. 要将标志附加到另一个标志,应使用“OR”运算符。

So this line should change to 所以这一行应该改为

cv.threshold(dst, dst, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU);

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

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