如何从直方图获取阈值? [英] How to get threshold value from histogram?

查看:740
本文介绍了如何从直方图获取阈值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OpenCV中编写一个Android应用程序来检测blob。一个任务是阈值图像以区分前景对象与背景(见图像)。

I'm writing an Android app in OpenCV to detect blobs. One task is to threshold the image to differentiate the foreground objects from the background (see image).

只要图像是已知的,我就可以手动传递一个阈值到threshold() - 在这个特定的图像说,200。该图像不知道有唯一的知识,将有一个黑暗的实心背景和更轻的前景对象如何能够动态地找出阈值?

It works fine as long as the image is known and I can manually pass a threshold value to threshold()--in this particular image say, 200. But assuming that the image is not known with the only knowledge that there would be a dark solid background and lighter foreground objects how can I dynamically figure out the threshold value?

我来了跨越直方图,其中我可以计算灰度图像的强度分布。但我找不到一种方法来分析直方图,并选择感兴趣的对象(较轻)所在的值。那是;我想区分显然黑暗的背景尖峰从较轻的前景尖峰 - 在这种情况下200以上,但在另一种情况下可以说,100如果对象是灰色的。

I've come across the histogram where I can compute the intensity distribution of the grayscale image. But I couldn't find a method to analyze the histogram and choose the value where the objects of interest (lighter) lies. That is; I want to differ the obviously dark background spikes from the lighter foreground spikes--in this case above 200, but in another case could be say, 100 if the objects are grayish.

推荐答案

如果所有的图像都是这样的,或者可以带来这种风格,我认为cv2.THRESHOLD_OTSU,即otsu的阈值算法是一个好镜头。

If all your images are like this, or can be brought to this style, i think cv2.THRESHOLD_OTSU, ie otsu's tresholding algorithm is a good shot.

下面是在命令终端中使用Python的示例:

Below is a sample using Python in command terminal :

>>> import cv2
>>> import numpy as np
>>> img2 = cv2.imread('D:\Abid_Rahman_K\work_space\sofeggs.jpg',0)

>>> ret,thresh = cv2.threshold(img2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

>>> ret
122.0

ret 自动计算的阈值。我们只是传递'0'作为阈值。

ret is the threshold value which is automatically calculated. We just pass '0' as threshold value for this.

我在GIMP中得到124(这与我们得到的结果相当)。它也消除了噪音。查看以下结果:

I got 124 in GIMP ( which is comparable to result we got). And it also removes the noise. See result below:

这篇关于如何从直方图获取阈值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆