咖啡豆分离算法 [英] Coffee beans separation algorithm

查看:785
本文介绍了咖啡豆分离算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是分离(计数)二进制图像上的咖啡豆的正确算法?豆类可以触摸并部分重叠。

What is proper algorithm for separating (counting) coffee beans on binary image? Beans can touch and partially overlap.

我工作实际上不是用咖啡豆,而是用咖啡豆更容易描述。这是我的任务计数所有现有的人和计数人超过一些想象线从超市监视视频的子问题。我已经提取移动对象到二进制掩码,现在我需要分开这些不知何故。

I am working actually not with coffee beans, but with coffee beans it is easier to describe. This is sub-problem in my task of counting all present people and counting people crossing some imaginary line from supermarket surveillance video. I've extracted moving objects into binary mask, and now I need to separate these somehow.

有人在评论中提及的两个有希望的算法:

Two promising algo that someone mentioned in comments:


  • Wathershed + distancetransofrm +标签。这可能是这个问题的答案,我把它(豆分离)。

  • 从视频序列跟踪移动对象(此算法的名称是什么?)。它可以跟踪重叠的对象。这是更有前途的算法,可能正是我需要解决我的任务(移动人分离)。

推荐答案

这种方法是从mmgp的回答分离,详细解释分水岭算法如何工作。因此,如果你需要对代码做什么的一些解释,请检查他的答案。

This approach is a spin-off from mmgp's answer that explains in detail how the watershed algorithm works. Therefore, if you need some explanation on what the code does, please check his answer.

代码可以播放,以提高检测率。这是:

The code can be played with in order to improve the rate of detection. Here it is:

import sys
import cv2
import numpy
from scipy.ndimage import label

def segment_on_dt(a, img):
    border = cv2.dilate(img, None, iterations=3)
    border = border - cv2.erode(border, None)
    cv2.imwrite("border.png", border)

    dt = cv2.distanceTransform(img, 2, 5)    
    dt = ((dt - dt.min()) / (dt.max() - dt.min()) * 255).astype(numpy.uint8)
    _, dt = cv2.threshold(dt, 135, 255, cv2.THRESH_BINARY)
    cv2.imwrite("dt_thres.png", dt)    

border (左), dt (右):




    lbl, ncc = label(dt)
    lbl = lbl * (255/ncc)      
    # Completing the markers now. 
    lbl[border == 255] = 255

    lbl = lbl.astype(numpy.int32)
    cv2.imwrite("label.png", lbl)

lbl

    cv2.watershed(a, lbl)

    lbl[lbl == -1] = 0
    lbl = lbl.astype(numpy.uint8)
    return 255 - lbl

# Application entry point
img = cv2.imread("beans.png")
if img == None:
    print("!!! Failed to open input image")
    sys.exit(0)

# Pre-processing.
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    
_, img_bin = cv2.threshold(img_gray, 128, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
cv2.imwrite("img_bin.png", img_bin)

img_bin = cv2.morphologyEx(img_bin, cv2.MORPH_OPEN, numpy.ones((3, 3), dtype=int))
cv2.imwrite("img_bin_morphoEx.png", img_bin)

img_bin 和后(右)形态操作:

img_bin (left) before and after (right) a morphology operation:


result = segment_on_dt(img, img_bin)
cv2.imwrite("result.png", result)

result[result != 255] = 0
result = cv2.dilate(result, None)
img[result == 255] = (0, 0, 255)
cv2.imwrite("output.png", img)

结果 >输出(右):

result (left) of watershed segmentation, followed by output (right):


这篇关于咖啡豆分离算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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