简体   繁体   English

图像二值化,自适应阈值参数麻烦

[英]image binarization, adaptive threshold parameters trouble

Im in a computer vision project here in my work (i'm an engineering practitioner) and i have to compare two images of a circuit board to found a mistake (if any) before a machine do the assembly.我在我的工作中参与了一个计算机视觉项目(我是一名工程从业者),我必须在机器组装之前比较电路板的两个图像以发现错误(如果有的话)。

i dont know if my method is the good one but i found that i can compare two binary images and then apply an XOR operation to found the differences between the two images (this is what im doing: http://www.electroschematics.com/10482/pcb-defects-detection-opencv/ )我不知道我的方法是否好,但我发现我可以比较两个二进制图像,然后应用 XOR 运算来找到两个图像之间的差异(这就是我正在做的: http : //www.electroschematics.com /10482/pcb-缺陷-检测-opencv/ )

but when i apply in my code the function of adaptive threshold the resulting image is not the result that i want (it has a lot of black spaces or the circuit lines are missing or bold) i try a lot of parameters but i cant find the good one.但是当我在我的代码中应用自适应阈值的功能时,结果图像不是我想要的结果(它有很多黑色空间或电路线丢失或加粗)我尝试了很多参数,但我找不到好一个。

Here is the code of the threshold:这是阈值的代码:

[Imgproc.adaptiveThreshold(img1,src, 255,   Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 13,10);]

and this is the result这就是结果

circuit board电路板

i just want to know if there are some parameters to make the lines more clear (or maybe another binary mehotd that i just missed)我只是想知道是否有一些参数可以使线条更清晰(或者可能是我刚刚错过的另一个二进制 mehotd)

i just want to know if there are some parameters to make the lines more clear (or maybe another binary mehotd that i just missed)我只是想知道是否有一些参数可以使线条更清晰(或者可能是我刚刚错过的另一个二进制 mehotd)

You can make the lines more clear by further filtering.您可以通过进一步过滤使线条更清晰 For example, in the guide you referenced, the threshold plays an important part because it identifies the missing objects on the two circuit boards.例如,在您参考的指南中,阈值起着重要作用,因为它可以识别两个电路板上丢失的物体。 If the threshold was set to 0, then each tiny little white dot identified (which is likely due to different lighting - as suggested from the guide ) would be considered a detection of a 'difference' between the two boards.如果阈值设置为 0,那么识别的每个微小的小白点(这可能是由于不同的照明 - 正如指南中所建议的)将被视为检测到两块板之间的“差异”。 Upping the threshold is important to detect segments that meet a minimum threshold of pixels.提高阈值对于检测满足最小像素阈值的片段很重要。

but when i apply in my code the function of adaptive threshold the resulting image is not the result that i want (it has a lot of black spaces or the circuit lines are missing or bold) i try a lot of parameters but i cant find the good one.但是当我在我的代码中应用自适应阈值的功能时,结果图像不是我想要的结果(它有很多黑色空间或电路线丢失或加粗)我尝试了很多参数,但我找不到好一个。

There's a lot to consider for 'accurate' detection and getting what result you want because clusters of different pixels (as shown in an output 'xor' image) can vary in threshold throughout the board.要进行“准确”检测并获得您想要的结果,需要考虑很多因素,因为不同像素的集群(如输出“异或”图像中所示)在整个电路板上的阈值可能会有所不同。

For your particular sitation, looking at the doc's for Imgproc.adaptiveThreshold对于您的特定站点,请查看Imgproc.adaptiveThreshold文档

public static void adaptiveThreshold(Mat src,
                 Mat dst,
                 double maxValue,
                 int adaptiveMethod,
                 int thresholdType,
                 int blockSize,
                 double C)

I found the following parameters will likely help you configure a more acceptable threshold.我发现以下参数可能会帮助您配置一个更容易接受的阈值。 Those parameters being (although all the parameters play an important role):这些参数是(尽管所有参数都起着重要作用):

maxValue - Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
blockSize - Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
C - Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.

Let's say you have a maxValue of 255 (as per your original question state).假设您的 maxValue 为 255(根据您的原始问题状态)。 Then a blockSize of 13, and a C of 10.然后块大小为 13,C 为 10。

From my understanding, and please if an onlooker catches something off here, I'd benefit from the learning too, the 13 * 13 block is approx 169 total pixels and the threshold is 255 with a variation of a constant factor C or 10. So I don't think these numbers don't maximize their potential.根据我的理解,如果旁观者在这里发现了什么,我也会从学习中受益,13 * 13 块大约有 169 个总像素,阈值为 255,变化为常数因子 C 或 10。所以我不认为这些数字不会最大限度地发挥其潜力。

My suggested solutions:我建议的解决方案:

  • Take note and try to improve on the 5.0 Limitations as suggested in the guide/article you linked to following.请注意并尝试按照您链接到的以下指南/文章中的建议改进5.0 限制
  • Instead of searching the entire image, searching by parts of an image may increase accuracy as you can have variable thresholds for different parts of the image (eg if there's small differences with less noise in one corner of the image vs. large differences with more noise in another corner of the image - then you may potentially eliminate potential loss of legitimate clusters of missing parts when filtering)不是搜索整个图像,而是按图像的一部分进行搜索可能会提高准确性,因为您可以为图像的不同部分设置可变的阈值(例如,如果图像的一个角存在噪声较小的小差异与噪声较多的大差异在图像的另一个角落 - 那么你可能会在过滤时消除合法的缺失部分簇的潜在损失)

Note: by posting the source images you are using and the code you are using to better help replicate the setup you have (see how to ask a good question )注意:通过发布您正在使用的源图像和您正在使用的代码来更好地帮助复制您的设置(请参阅如何提出一个好问题

Edit: See related for an example of adaptive threshold and openCV (related).编辑:有关自适应阈值和 openCV(相关)的示例,请参阅相关内容。

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

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