python - 在 python 和 openCV 中实现 LoG blob 检测器

标签 python opencv computer-vision

我正在尝试根据维基百科中的算法实现 LoG blob 检测器: https://en.wikipedia.org/wiki/Blob_detection#The_Laplacian_of_Gaussian

我正在使用 Python 和 openCV,我正在使用我获得的用于制作滤镜的代码,我的代码创建了 n 级滤镜,在图像上使用滤镜并将所有级别保存在 hw n 数组。 之后我寻找局部最大值,如果找到一个,我将其标记为 Blob 的中心并在其周围画一个圆圈。 我设法让它工作,但我得到了奇怪的结果,我不确定我做错了什么。

myImage = cv2.imread('fishes.jpg')
n = 15
height, width = myImage.shape[:2]
empty = np.empty((height+2,width+2)).astype(np.uint8)
imgArray = np.empty((n+2,height+2,width+2)).astype(np.uint8)
radArray = []
imgArray[0] = empty
imgArray[n+1] = empty
gray_image = cv2.cvtColor(myImage, cv2.COLOR_BGR2GRAY)
grayAllChannels = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
sigma = 2
k = 2**(0.25)
std2 = float(sigma**2)
for i in range(n):
    filt_size =  2*np.ceil(3*sigma)+1
    radArray.append(filt_size / 2)
    H = log_filt( filt_size, sigma)
    H *= sigma**2
    dst = cv2.filter2D(gray_image,-1,H)
    dst = cv2.copyMakeBorder(dst,1,1,1,1,cv2.BORDER_CONSTANT, value=BLACK)
    imgArray[i+1] = dst      
    sigma = sigma * k
    std2 = float(sigma**2)

i = 0
for imgIndex in range(1,n+1):
    for hIndex in range(1, height+1):
        for wIndex in range(1, width+1):
            tSlice = imgArray[imgIndex - 1:imgIndex + 2,hIndex - 1:hIndex + 2,wIndex - 1:wIndex + 2]
            tNum = imgArray[imgIndex,hIndex,wIndex]
            if (tNum > imgArray[imgIndex - 1,hIndex - 1,wIndex - 1] and
                tNum > imgArray[imgIndex - 1,hIndex - 1,wIndex] and
                tNum > imgArray[imgIndex - 1,hIndex - 1,wIndex + 1] and
                tNum > imgArray[imgIndex - 1,hIndex,wIndex - 1] and
                tNum > imgArray[imgIndex - 1,hIndex,wIndex] and
                tNum > imgArray[imgIndex - 1,hIndex,wIndex + 1] and
                tNum > imgArray[imgIndex - 1,hIndex + 1,wIndex - 1] and
                tNum > imgArray[imgIndex - 1,hIndex + 1,wIndex] and
                tNum > imgArray[imgIndex - 1,hIndex + 1,wIndex + 1] and

                tNum > imgArray[imgIndex,hIndex - 1,wIndex - 1] and
                tNum > imgArray[imgIndex,hIndex - 1,wIndex] and
                tNum > imgArray[imgIndex,hIndex - 1,wIndex + 1] and
                tNum > imgArray[imgIndex,hIndex,wIndex - 1] and
                tNum > imgArray[imgIndex,hIndex ,wIndex + 1] and
                tNum > imgArray[imgIndex,hIndex + 1,wIndex - 1] and
                tNum > imgArray[imgIndex,hIndex + 1,wIndex] and
                tNum > imgArray[imgIndex,hIndex + 1,wIndex + 1] and

                tNum > imgArray[imgIndex + 1,hIndex - 1,wIndex - 1] and
                tNum > imgArray[imgIndex + 1,hIndex - 1,wIndex] and
                tNum > imgArray[imgIndex + 1,hIndex - 1,wIndex + 1] and
                tNum > imgArray[imgIndex + 1,hIndex,wIndex - 1] and
                tNum > imgArray[imgIndex + 1,hIndex,wIndex] and
                tNum > imgArray[imgIndex + 1,hIndex,wIndex + 1] and
                tNum > imgArray[imgIndex + 1,hIndex + 1,wIndex - 1] and
                tNum > imgArray[imgIndex + 1,hIndex + 1,wIndex] and
                tNum > imgArray[imgIndex + 1,hIndex + 1,wIndex + 1]):

                cv2.circle(grayAllChannels,(hIndex - 1, wIndex - 1),np.int16(radArray[imgIndex - 1]),(0,30,230),2)

我得到这个结果:

butterfly

正确的结果应该是这样的:

correct butterfly

知道我在这里做错了什么吗?

最佳答案

您是否可能在某处混淆了列/行分别是 y/x(图像的右半部分没有 Blob )? 如果我猜的话,我会在画圆圈的时候说。我认为你必须以 (x, y) 的格式传递点数,这意味着你必须交换你的值。

关于python - 在 python 和 openCV 中实现 LoG blob 检测器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588654/

相关文章:

python - 如何在狮身人面像中启用数学?

python - 我如何在 Python 中执行 strtok() 在 C 中执行的操作?

opencv - 卡尔曼滤波器能否预测被跟踪物体与墙壁碰撞后的位置?

c++ - OpenCV : How to display webcam capture in windows form application?

python - 将黑白图像完全转换为一组线条(也称为仅使用线条矢量化)

c++ - 将视频文件插入vector时vector内存不足?

python - 尝试使用 python 连接到 Google 云存储(GCS)

python - 建议在没有 switch case 的情况下实现文本菜单

python - 将矩形图像调整为正方形,保持比例并用黑色填充背景

opencv - 使用 filter2D (OpenCV) 进行 Sobel 运算符时,平方会引入大量噪声