MATLAB中的游程长度平滑算法(RLSA)

时间:2016-08-27 20:26:09

标签: algorithm matlab image-processing

我在matlab中有以下RLSA代码。该算法的要点是它尝试连接具有指定间距(阈值)的区域,例如识别文本区域(因为文本大部分时间都固定在图像上的特定间距)它试图加入它们,以便在进行某些形态学操作时,它们可以很好地识别这些区域。

1.function result=RLSA(image,hor_thresh)
2.        zeros_count=0;
3.        one_flag=0;
4.        hor_image=image;
5.        [m,n]=size(image);
6.        for i=1:m
7.            for j=1:n
8.                if(image(i,j)==1)
9.                    if(one_flag==1)
10.                        if(zeros_count<=hor_thresh)
11.                            hor_image(i,j-zeros_count:j-1)=1;
12.                        else
13.                           one_flag=0;
14.                        end
15.                        zeros_count=0;
16.                   end
17.                    one_flag=1;
18.                else 
19.                    if(one_flag==1)
20.                        zeros_count=zeros_count+1;
21.                    end
22.                end
23.            end
24.        end
25.        result= hor_image;
26.        end

上述MATLAB代码来自以下网站

answers.opencv.org

Attempt to implementation Running Length Smoothing Algorithm in C++

更详细的描述在这里

Working of RLSA

以上代码不适用于所有阈值。例如,在设置阈值= 20时,我遇到以下错误

Subscript indices must either be real positive integers or logicals.


Error in RLSA (line 11)
                    hor_image(i,j-zeros_count:j-1)=1;

有人可以解释我在带有文本的图像上的算法工作,以及为什么这段代码不能用于所有阈值? 此外,我是否需要对此代码进行两次传递(如给出的链接中所述),一次用于水平,另一次用于垂直和逻辑,或者它们或已提及的代码已经处理过了?

1 个答案:

答案 0 :(得分:1)

这一切都取决于你传递的图像和Hor_Thresh。我猜想问题是j-zeros_count可能是负数。如果图像的一列具有1但在另一列的20个像素内具有1,那么这将是可能的。然后,设置为1的范围将跨越2列。

我真的不知道该算法正在尝试做什么,但它似乎是处理二进制图像(但没有明确)。它设置的零点(以书为单位)计数&lt; Hor_Threshold,都是1.所以如果有“短”的零运行,它们将被设置为1.

从参数名称猜测,我希望在2 for循环之间看到一些重置代码。 Hor_Threshold意味着水平阈值,即它被重置每一列。 所以在i和j循环之间,我认为你需要重置你的2个标志:     zeros_count = 0;     one_flag = 0;

自从我使用Matlab已经有很长一段时间了,我一直在使用类似的语言,Igor Pro,所以我在Igor中测试了一些东西。这是翻译的功能:

Function/Wave RLSA(image,hor_thresh)
    Wave Image
    Variable Hor_Thresh

    Variable zeros_count = 0;
    Variable one_flag = 0;
    Duplicate/FREE Image, hor_image
    Variable m = DimSize(Image,0)
    Variable n = DimSize(Image,1)
    Variable i, J
    for (i = 0;i < m;i += 1)
        for (j = 0;j < n;j += 1)
            if (image[i][j] == 1)
                if (one_flag == 1)
                    if (zeros_count <= hor_thresh)
                        hor_image[i][j-zeros_count,j-1] = 1;
                    else
                        one_flag = 0;
                    endif
                    zeros_count = 0;
                endif 
                one_flag = 1;
            else 
                if (one_flag == 1)
                    zeros_count += 1;
                endif
            endif
        endfor
    endfor
    return hor_image;
end //RLSA
与I ++一样,Igor是基于零的索引。 因此,我可以使用我创建的图像获得相同的错误:

Make/N=(24,24) Image
Image = 0
image[0][22] = 1
image[1][2] = 1
RLSA(image,20)

引发与您相同的错误。 所以我在图像中有2个,用3个零(&lt;第二个参数)分隔,但是在不同的列中。

相关问题