获取图像黑色边框内的区域

时间:2017-05-07 08:03:33

标签: matlab image-processing computer-vision

我从下面的图片开始:

Initial image

我通过将像素与其邻域平均值进行比较来执行局部阈值处理。我还使用以下代码在本地阈值处理后裁剪出图像周围的白色边框:

I = imread('img_path');
N = 21;
localMean = conv2(double(I), double(1/(N^2) * ones(N)), 'same');
BW = I > localMean;
borderSize = ceil(double(N) / 2);
R = [borderSize, borderSize, size(BW, 2) - 2*borderSize, size(BW, 1) - 2*borderSize];
BW = imcrop(BW, R);

这给我留下了以下图片:

Segmented Cells

正如您所看到的,每个分段细胞周围都有一个清晰的黑色边框。如何删除此图像中的所有内容除了黑色边框内的区域外?

如果这是不可能的,那么我是否还有其他技术可以让我的本地阈值降低噪音?

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

你可以使用Otsu的阈值(graythresh)和形态操作:

#!python3
#coding=utf-8
""" Line break demo 2 """

text = "lorem ipsum dolor sit amet blablah"

for wmax in [10,25,55,80,100,120]:

    print(wmax)

    base_width, base_height = (wmax,None)#base_img.size
    #font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
    line_width = 0
    line_count = 1
    lines = []
    string = ""
    for c in text:
        line_width += 5#font.getsize(c)[0]
        string += c
        if line_width > base_width:

            print("text  ", text)
            print("string", string)

            s = string.rsplit(" ", 1)
            print("split ", s)

            string = s[0]
            lines.append(string)

            try:
                string = s[1]
                line_width = len(string) * 5
            except:
                string = ""
                line_width = 0

            print("lines ", lines)
            print("string", string)

            line_count += 1
            print()

    if string:
        lines.append(string)


    print(lines)
    print()

enter image description here