2025年matlab函数输出多个值(matlab函数输出多个值怎么绘图)

matlab函数输出多个值(matlab函数输出多个值怎么绘图)以前看过 matlab 的 bwlookup 函数 但是总感觉有点神秘 一直没有去仔细分析 最近在分析计算二值图像的欧拉数时 发现自己写的代码和 matlab 的总是对不少 于是又去翻了下 matlab 的源代码 看到了 matlab 里实现欧拉数的代码非常简单 其核心就是借用了 bwlookup 函数 p p p 以前看过 matlab 的 bwlookup 函数 但是总感觉有点神秘 p

大家好,我是讯享网,很高兴认识大家。



 以前看过matlab的bwlookup函数,但是总感觉有点神秘,一直没有去仔细分析,最近在分析计算二值图像的欧拉数时,发现自己写的代码和matlab的总是对不少,于是又去翻了下matlab的源代码,看到了matlab里实现欧拉数的代码非常简单,其核心就是借用了bwlookup函数。 </p><p>  以前看过matlab的bwlookup函数,但是总感觉有点神秘,一直没有去仔细分析,最近在分析计算二值图像的欧拉数时,发现自己写的代码和matlab的总是对不少,于是又去翻了下matlab的源代码,看到了matlab里实现欧拉数的代码非常简单,如下所示:</p><div></div><p>  在内部就是调用了bwlookup函数,那没办法了,就仔细看了M的帮助文档,发现原来这个函数真的非常简单,我们贴下M的帮助文档:</p><p>  A = bwlookup(BW,lut)</p><p>  The&nbsp;&nbsp;function performs these steps to determine the value of each pixel in the processed image&nbsp;A:</p><ul><li>Locate the pixel neighborhood in input image&nbsp;BW&nbsp;based on the coordinates of the target pixel in&nbsp;. The function zero-pads border pixels of image&nbsp;&nbsp;when the neighborhood extends past the edge of&nbsp;.</li><li>Calculate an index,&nbsp;, based on the binary pixel pattern of the neighborhood.</li><li>Set the target pixel in&nbsp;&nbsp;as the value of the lookup table at the index&nbsp;, in other words, the value of&nbsp;.</li></ul><p>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 24&nbsp;and the length of the lookup table&nbsp;lut&nbsp;is 16.</p><p>To find the value of the target output pixel at (row, column) coordinate (<em>r</em>,<em>c</em>),&nbsp;&nbsp;uses the 2-by-2 pixel neighborhood in the input binary image&nbsp;&nbsp;whose top left pixel is at coordinate (<em>r</em>,<em>c</em>). The index&nbsp;&nbsp;into the lookup table is the weighted sum of the four pixels in the neighborhood, plus 1.</p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f460b.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_sed' title="Pixel weights start with a weight of 1 for the top left pixel in the neighborhood, and increase as powers of two along rows then along columns, with a final weight of 8 for the bottom right pixel." style="width: 372px; visibility: visible;"></p><p>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 29&nbsp;and the length of the lookup table&nbsp;&nbsp;is 256.</p><p>To find the value of the target output pixel at (row, column) coordinate (<em>r</em>,<em>c</em>),&nbsp;&nbsp;uses the 3-by-3 pixel neighborhood in the input binary image&nbsp;&nbsp;centered on coordinate (<em>r</em>,<em>c</em>). The index&nbsp;&nbsp;into the lookup table is the weighted sum of the nine pixels in the neighborhood, plus 1.</p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f49af1f29006.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_02' title="Pixel weights start with a weight of 1 for the top left pixel in the neighborhood, and increase as powers of two along rows then along columns, with a final weight of 256 for the bottom right pixel." style="width: 513px; visibility: visible;"></p><p>  简单的说,这个查找表只有2种可能,一个是表的元素是16个,一种是由512个元素,其中16个元素时,查表的依据是以当前点为起点,向右向下各扩展一个像素范围,得到一个2*2领域4个像素,4个像素,每个像素也就只有2种可能的取值,这样就有16种可能的组合,对应16个元素。</p><p>  当表的元素是512个时,查表的依据是以当前点为中心点,向左向右,向上向下各扩展一个像素,得到一个3*3的领域9个像素,同样每个像素也只有2种可能取值,所以共有1&lt;&lt;9即512种取值。</p><p>  因为涉及到领域,所有在原图边缘的地方会有越界的图像,这个时候的原则是填充0,而不是复制最近的像素。</p><p>  这样,当表中有不同的内容时,就可以实现不同的结果。</p><p>  我们以16个元素的表为例,假定 lut[16] ={&nbsp;6 3 16 11 7 14 8 5 15 1 2 4 13 9 10 12},</p><p>  对于下面的一个4*4的二值数据:</p><div></div><p>  如果当前的点位于第二行第一列,则其2*2的领域范围的像素信息为:</p><div></div><div></div><div></div><p>&nbsp;通过对图像中每个像素的面积求和来估计图像中所有&nbsp;&nbsp;像素的面积。单个像素的面积是通过观察其 2×2 邻域来确定的。有六种不同的情形,每种情形表示一个不同面积:</p><ul><li>具有零个&nbsp;&nbsp;像素的情形(面积 = 0)</li><li>具有一个&nbsp;&nbsp;像素的情形(面积 = 1/4)</li><li>具有两个相邻&nbsp;&nbsp;像素的情形(面积 = 1/2)</li><li>具有两个对角&nbsp;&nbsp;像素的情形(面积 = 3/4)</li><li>具有三个&nbsp;&nbsp;像素的情形(面积 = 7/8)</li><li>具有所有四个&nbsp;&nbsp;像素的情形(面积 = 1)</li></ul><p>  用图形化的表达方式上述六种情况对应如下(为了显示,使用绿色表示黑色值,红色表示白色值):</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f4be2e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_03' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f4dcdbe55445.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_04' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f50028d18793.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_05' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_06' style="width: 32px; visibility: visible;"></p><p>&nbsp; &nbsp; &nbsp;&nbsp;<br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f52d3f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_07' style="width: 32px; visibility: visible;"></p><p>&nbsp; &nbsp; &nbsp;<br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f547abd53157.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_08' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f55f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_sed_09' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f579ebf96677.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_10' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_sed_11' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f5a9c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_12' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f5bfc.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_13' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f5d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_sed_14' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f5ed.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_15' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f60b70f75314.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_16' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f623e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_17' style="width: 32px; visibility: visible;"></p><p>  <br></p><p style="text-align:center;"><img src='https://s2.51cto.com/images/blog//_671be8f639e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184' alt='[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_18' style="width: 32px; visibility: visible;"></p><p>索引值&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp;0  &nbsp;  1  &nbsp; &nbsp; 2    3  &nbsp; &nbsp; 4    5    6   &nbsp; &nbsp; 7   &nbsp; 8  &nbsp; &nbsp; &nbsp;9  &nbsp; &nbsp; &nbsp;10  &nbsp; &nbsp;11  &nbsp; &nbsp; 12  &nbsp; &nbsp;13  &nbsp; &nbsp; 14  &nbsp; &nbsp; 15  </p><p>面积值    &nbsp; 0  &nbsp; 1/4  &nbsp; &nbsp;1/4   1/2  &nbsp; 1/4   1/2   3/4   &nbsp; 7/8   1/4  &nbsp; &nbsp;3/4  &nbsp; &nbsp;1/2   &nbsp;7/8  &nbsp; &nbsp;1/2  &nbsp; &nbsp;&nbsp;7/8  &nbsp; &nbsp; 7/8  &nbsp; &nbsp; 1</p><p>把面积值放大八倍得到面积值依次为:&nbsp;&nbsp;0 2 2 4 2 4 6 7 2 6 4 7 4 7 7 8,我们翻看matlab的bwarea函数,可以得到其代码如下:</p><div></div><p>  查找表和这里的一摸一样。</p><p>  使用查找表的优点是把领域所有的组合提前计算出结果来,而不用到程序里进行一些列复杂的判断,这样在很多情况下是可以提高速度的,比如刚刚这个面积计算的东西,如果在循环内部做,则要做N种判断,那如果是涉及到3*3的领域,那就更为复杂了。&nbsp;</p><p>  朴素的来说,有了bwlookup的原理,要把这个算法移植到C++中,就是一个比较简单的过程了,假如是处理16元素的表,也就是涉及到了4个像素,假定他们的值分别为P0P1P2P3,则最基本的C代码如下所示:</p><div></div><p>  处理512个元素的过程也是类似的,只不过索引值多加了几个(注意,这里用255表示白色,而不是1)。</p><p>  如果考虑指令集优化,一个自然的翻译过程如下所示:</p><div></div><p>  一次性处理16个字节,最为关键的一点是,这个查表可以方便的直接使用_mm_shuffle_epi8非常高效的实现,这个在我前期的文章里多次提到(16个字节表的查找,我们假定lut的类型为字节类型,这个在实际的应用中也足够了),</p><p>&nbsp;  这个代码的性能提升相对于C++来说是非常可观的,大概又4倍多的速度提升。</p><p>  仔细上述代码其实还有提高的地方,我们使用_mm_blendv_epi8来进行结果的混合, 而混合的标志是值是否等于255,这里用_mm_cmpeq_epi8做判断,而_mm_cmpeq_epi8的结果就是如果P等于255,则返回255,否则返回0,那这个不就是P本省吗,所以根本就不用做这个判断,这样代码就变为: 

讯享网

  另外,再观察这个C语言的特殊性,我们可以把上述C代码修改为:

  即直接使用位运算替换掉那个判断,这样对应的SSE指令也可以进行修改为如下代码:

  相比于原始的SSE代码,这个改动也约有30%的性能提升的。

  对于512个元素的表,情况会有所不同,因为索引值大于了255,所以已经无法用字节类型来保存累加后得到的索引了,至少的用ushort类型,但是呢,前8个位置的索引相加还是不会超出字节类型的,所以前8个位置还是可以一次性处理16个像素,到最后一个位置时,单独转换为16位,然后再接用2次16数据的处理指令,就可以一次性得到16个字节对应的查找表索引。

  注意,512个元素的查表如果是纯C++代码, 最后一个的 & 操作,一定要注意数据范围,不能写成&nbsp;Index += (256 & P9);, 而是&nbsp;&nbsp;Index += (256 & (65280 + P9));

  但是这个时候,由于表的大小时512,所以已经无法使用SSE去优化这个查表功能了,只能把索引值单个的提取出来,然后用普通的C语言去执行代码。如果CPU能支持AVX2,那么AVX2倒是又有关指令能直接查表,不过也要做很多的改造工程。

  我们测试,对于512个元素的表,优化后的SSE指令处理一副 3000*2000的二值图,大概耗时在2ms不到,速度还是相当的快的。

  搜索matlab的代码,除了bweuler及bwarea内使用了bwlookup,另外就是bwmorph里也大量的使用了bwlookup,而且都是使用的512个元素的表,也就是说使用3*3的领域,我们看bwmorph的帮助文档,有很多相关内容:

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_19
讯享网

&nbsp;  这些对应的查找表在&nbsp;MATLABR2023b oolboximagesimages+images+internal 目录下以lut开头的文件中可以找到,比如说,这个clean的表内容如下:

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_20

&nbsp;  这些表的内容都是提前设计好的,然后可以多次重复的调用,从而得到某种效果。

  一般来说,这种3*3的领域算子,在迭代了一定的次数后效果就不会有变化了。

   我们在morph中也能看到一些常用的算子,比如这个remove就可以得到二值图像的边界,这个majority可以平滑噪音(和我博客里专门讲的那个majority效果还是有差异的)。

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_21

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_帮助文档_22

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_sed_23

        原图                  remove              majority&nbsp;&nbsp;

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_sed_24

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_25

[快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。_二值图像_26

    &nbsp; &nbsp; fatten&nbsp;&nbsp;                 &nbsp; &nbsp; skeleton                 thin

   个人感觉这个方法在处理一些比较复杂的领域信息时还是很有帮助的,特别是不方便用判断条件一个一个的写的,如果能提前把这个表弄出来,那就效率能得到很大的提升的。

   在个人的SSE Demo里,也集成了这个算法,其内容在Binary(二值处理)–》Processing(后处理)–&gt;&gt; Morph(形态学)中,有兴趣的朋友可以看看。

翻译

搜索

复制

小讯
上一篇 2025-05-04 10:55
下一篇 2025-05-25 22:00

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/199978.html