[db:标题]

摘要:以前看过matlab的bwlookup函数,但是总感觉有点神秘,一直没有去仔细分析,最近在分析计算二值图像的欧拉数时,发现自己写的代码和matlab的总是对不少,于是又去翻了下matlab的源代码,看到了matlab里实现欧拉数的代码非常简
以前看过matlab的bwlookup函数,但是总感觉有点神秘,一直没有去仔细分析,最近在分析计算二值图像的欧拉数时,发现自己写的代码和matlab的总是对不少,于是又去翻了下matlab的源代码,看到了matlab里实现欧拉数的代码非常简单,如下所示: if n==4 lut = 4*[0 0.25 0.25 0 0.25 0 .5 -0.25 0.25 0.5 0 -0.25 0 ... -0.25 -0.25 0] + 2; else lut = 4*[0 0.25 0.25 0 0.25 0 -.5 -0.25 0.25 -0.5 0 -0.25 0 ... -0.25 -0.25 0] + 2; end % Need to zero-pad the input b = padarray(a,[1 1],'both'); weights = bwlookup(b,lut); if coder.isColumnMajor e = (sum(weights(:),'double') - 2*numel(b)) / 4; else e = (sum(sum(weights,2,'double'),1,'double') - 2*numel(b)) / 4; end   在内部就是调用了bwlookup函数,那没办法了,就仔细看了M的帮助文档,发现原来这个函数真的非常简单,我们贴下M的帮助文档:   A = bwlookup(BW,lut)   Thebwlookupfunction performs these steps to determine the value of each pixel in the processed imageA: Locate the pixel neighborhood in input imageBWbased on the coordinates of the target pixel inA. The function zero-pads border pixels of imageBWwhen the neighborhood extends past the edge ofBW. Calculate an index,idx, based on the binary pixel pattern of the neighborhood. Set the target pixel inAas the value of the lookup table at the indexidx, in other words, the value oflut(idx). 2-by-2 Neighborhood Lookup For 2-by-2 neighborhoods, there are four pixels in each neighborhood. Each binary pixel has two possible states, therefore the total number of permutations is 24and the length of the lookup tablelutis 16. To find the value of the target output pixel at (row, column) coordinate (r,c),bwlookupuses the 2-by-2 pixel neighborhood in the input binary imageBWwhose top left pixel is at coordinate (r,c). The indexidxinto the lookup table is the weighted sum of the four pixels in the neighborhood, plus 1. 3-by-3 Neighborhood Lookup For 3-by-3 neighborhoods, there are nine pixels in each neighborhood. Each binary pixel has two possible states, therefore the total number of permutations is 29and the length of the lookup tablelutis 256. To find the value of the target output
阅读全文