Python + 处理中的图像分割

Image segmentation in Python + Processing

我在尝试 运行 这个图像分割代码时遇到了困难。运行
我的想法是拍摄这样的图像:

http://imgur.com/a/AmcKq

并提取所有黑色波浪形并将每个单独的波浪形保存为自己的图像。
代码似乎可以正常工作,但由于某种原因它没有分割我的图像。
我收到的错误是:('segments detected:', 0)

这是我使用的代码:

import os, sys  
import numpy as np  
from scipy import ndimage as ndi  
from scipy.misc import imsave  
import matplotlib.pyplot as plt  

from skimage.filters import sobel, threshold_local  
from skimage.morphology import watershed  
from skimage import io  


def open_image(name):  
    filename = os.path.join(os.getcwd(), name)  
    return io.imread(filename, as_grey=True)  


def adaptive_threshold(image):  
    print(type(image))  
    print(image)  
    block_size = 41  
    binary_adaptive = threshold_local(image, block_size, offset=10)  
    binary_adaptive = np.asarray(binary_adaptive, dtype=int)  
    return np.invert(binary_adaptive) * 1.  


def segmentize(image):  
    # make segmentation using edge-detection and watershed  
    edges = sobel(image)  
    markers = np.zeros_like(image)  
    foreground, background = 1, 2  
    markers[image == 0] = background  
    markers[image == 1] = foreground  

    ws = watershed(edges, markers)  

    return ndi.label(ws == foreground)  


def find_segment(segments, index):  
    segment = np.where(segments == index)  
    shape = segments.shape  

    minx, maxx = max(segment[0].min() - 1, 0), min(segment[0].max() + 1, shape[0])  
    miny, maxy = max(segment[1].min() - 1, 0), min(segment[1].max() + 1, shape[1])  

    im = segments[minx:maxx, miny:maxy] == index  

    return (np.sum(im), np.invert(im))  


def run(f):  
    print('Processing:', f)  

    image = open_image(f)  
    processed = adaptive_threshold(image)  
    segments = segmentize(processed)  

    print('Segments detected:', segments[1])  

    seg = []  
    for s in range(1, segments[1]):  
        seg.append(find_segment(segments[0], s))  

    seg.sort(key=lambda s: -s[0])    

    for i in range(len(seg)):  
        imsave('segments/' + f + '_' + str(i) + '.png', seg[i][1])  

folder = os.path.join(os.getcwd(), 'segments')  
os.path.isfile(folder) and os.remove(folder)  
os.path.isdir(folder) or os.mkdir(folder)  
for f in sys.argv[1:]:  
    run(f)  

我还要提到我正在 运行从 Processing 3.3.5 中使用这个 Python 脚本作为我的草图文件:

import deadpixel.command.Command;  

static final String BASH =  
  platform == WINDOWS? "cmd /C " :  
  platform == MACOSX? "open" : "xdg-open";  

static final String CD = "cd ", PY_APP = "python ";  
static final String AMP = " && ", SPC = " ";  

static final String PY_DIR = "scripts/";  
//static final String PY_FILE = PY_DIR + "abc.py";  
static final String PY_FILE = PY_DIR + "segmenting.py";  

static final String PICS_DIR = "images/";  
static final String PICS_EXTS = "extensions=,png,jpg,jpeg,gif";  

void setup() {  
  final String dp = dataPath(""), py = dataPath(PY_FILE);  
  final String prompt = BASH + CD + dp + AMP + PY_APP + py;  

  final String pd = dataPath(PICS_DIR);  
  final String pics = join(listPaths(pd, PICS_EXTS), SPC);  

  final Command cmd = new Command(prompt + SPC + pics);  
  println(cmd.command, ENTER);  

  println("Successs:", cmd.run(), ENTER);  
  printArray(cmd.getOutput());  

  exit();  
}   

这在处理中的新标签中:
https://github.com/GoToLoop/command/blob/patch-1/src/deadpixel/command/Command.java

快速调查发现问题所在:此函数在这里

def adaptive_threshold(image):  
    print(type(image))  
    print(image)  
    block_size = 41  
    binary_adaptive = threshold_local(image, block_size, offset=10)  
    binary_adaptive = np.asarray(binary_adaptive, dtype=int)  
    return np.invert(binary_adaptive) * 1. 

应该通过自适应阈值创建图像的掩码 - 但这是(非常)错误的。

主要原因似乎是对 threshold_local 工作方式的误解:此代码期望它是 return 输入图像的二值化分割版本,而实际上它是 return s a threshold image,见解释 here

但这不是唯一的问题。对于您示例中的图像,offset=10threshold_local 产生的阈值降低得太远,因此整个图像将高于阈值。

这是函数的工作版本:

def adaptive_threshold(image):

    # Create threshold image
    # Offset is not desirable for these images
    block_size = 41 
    threshold_img = threshold_local(image, block_size)

    # Binarize the image with the threshold image
    binary_adaptive = image < threshold_img

    # Convert the mask (which has dtype bool) to dtype int
    # This is required for the code in `segmentize` (below) to work
    binary_adaptive = binary_adaptive.astype(int)   

    # Return the binarized image
    return binary_adaptive

如果代码是运行有这个功能(有python;据我所知,这个问题与Processing无关),它returns Segments detected: 108 并且它产生了一个很好的分割:

plt.imshow(segments[0],interpolation='none')
plt.show()


旁注: 根据您对问题的措辞,我假设您不是自己编写此代码并且您在该领域的专业知识可能有限是否正确?

如果是,您可能有兴趣进一步了解基于 python 的图像处理和分割。我最近 运行 关于这个主题的短期课程,其中包括一个完全不言自明的管道实践教程,类似于您在此处使用的管道。 The materials 是公开访问的,请随时查看。


编辑:

根据您的评论,这里有一个解决方案应该允许程序 运行 以完整路径作为输入。

首先,删除所有这些:

folder = os.path.join(os.getcwd(), 'segments')  
os.path.isfile(folder) and os.remove(folder)  
os.path.isdir(folder) or os.mkdir(folder)  

所以只剩下这个:

for f in sys.argv[1:]:  
    run(f)

接下来,替换为:

    for i in range(len(seg)):  
        imsave('segments/' + f + '_' + str(i) + '.png', seg[i][1])  

通过这个:

    # Get the directory name (if a full path is given)
    folder = os.path.dirname(f)

    # Get the file name
    filenm = os.path.basename(f)

    # If it doesn't already exist, create a new dir "segments" 
    # to save the PNGs
    segments_folder = os.path.join(folder,"segments")
    os.path.isdir(segments_folder) or os.mkdir(segments_folder)

    # Save the segments to the "segments" directory
    for i in range(len(seg)):
        imsave(os.path.join(segments_folder, filenm + '_' + str(i) + '.png'), seg[i][1]) 

此解决方案可以处理纯文件输入(例如 'test.png')和路径输入(例如 'C:\Users\Me\etc\test.png')。


编辑 2:

对于 t运行sparency,如果数组保存为 RGBA (MxNx4),scipy.misc.imsave 允许 alpha 层,参见 here

替换这个

        imsave(os.path.join(segments_folder, filenm + '_' + str(i) + '.png'), seg[i][1]) 

由此

        # Create an MxNx4 array (RGBA)
        seg_rgba = np.zeros((seg[i][1].shape[0],seg[i][1].shape[1],4),dtype=np.bool)

        # Fill R, G and B with copies of the image
        for c in range(3):
            seg_rgba[:,:,c] = seg[i][1]

        # For A (alpha), use the invert of the image (so background is 0=transparent)
        seg_rgba[:,:,3] = ~seg[i][1]

        # Save image
        imsave(os.path.join(segments_folder, filenm + '_' + str(i) + '.png'), seg_rgba) 

编辑 3:

要保存到不同的目标文件夹,每个分割图像都有单独的子文件夹:

而不是这一行

    folder = os.path.dirname(f)

您可以指定目标文件夹,例如

    folder = r'C:\Users\Dude\Desktop'

(请注意 r'...' 格式,它会生成 raw string literal。)

接下来,替换这个

    segments_folder = os.path.join(folder,"segments")

由此

    segments_folder = os.path.join(folder,filenm[:-4]+"_segments")

为了更加干净,请替换这个

        imsave(os.path.join(segments_folder, filenm + '_' + str(i) + '.png'), seg_rgba) 

由此

        imsave(os.path.join(segments_folder, filenm[:-4] + '_' + str(i) + '.png'), seg_rgba)