Matlab:如何在图像中弯曲线条

标签 matlab image-processing line

我手头有一张图片(png 格式)。限制椭圆(代表原子核)的线太直了,这是不切实际的。如何从图像中提取线条并使它们弯曲,前提是它们仍然包围着原子核。

图片如下:
enter image description here

弯曲后

enter image description here

编辑:如何将answer2 中的Dilation And Filter 部分翻译成Matlab 语言?我想不通。

最佳答案

好的,这是一种涉及获得“自然”非对称外观所需的多个随机化步骤的方法。

我在 Mathematica 中发布实际代码,以防万一有人关心将它翻译成 Matlab。

(* A preparatory step: get your image and clean it*)
i = Import@"http://i.stack.imgur.com/YENhB.png";
i1 = Image@Replace[ImageData[i], {0., 0., 0.} -> {1, 1, 1}, {2}];
i2 = ImageSubtract[i1, i];
i3 = Inpaint[i, i2]

enter image description here

(*Now reduce to a skeleton to get a somewhat random starting point.  
The actual algorithm for this dilation does not matter, as far as we 
get a random area slightly larger than the original elipses *)
id = Dilation[SkeletonTransform[
             Dilation[SkeletonTransform@ColorNegate@Binarize@i3, 3]], 1] 

enter image description here

(*Now the real random dilation loop*)
(*Init vars*)
p = Array[1 &, 70]; j = 1;
(*Store in w an image with a different color for each cluster, so we 
can find edges between them*)
w = (w1 = 
      WatershedComponents[
       GradientFilter[Binarize[id, .1], 1]]) /. {4 -> 0} // Colorize;
(*and loop ...*)
For[i = 1, i < 70, i++,
 (*Select edges in w and dilate them with a random 3x3 kernel*)
 ed = Dilation[EdgeDetect[w, 1], RandomInteger[{0, 1}, {3, 3}]];
 (*The following is the core*)
 p[[j++]] = w =
   ImageFilter[  (* We apply a filter to the edges*)
    (Switch[
          Length[#1],  (*Count the colors in a 3x3 neighborhood of each pixel*)
          0, {{{0, 0, 0}, 0}},          (*If no colors, return bkg*)
          1, #1,                        (*If one color, return it*)
          _, {{{0, 0, 0}, 0}}])[[1, 1]] (*If more than one color, return bkg*)&@
      Cases[Tally[Flatten[#1, 1]], 
       Except[{{0.`, 0.`, 0.`}, _}]] & (*But Don't count bkg pixels*),
    w, 1,
    Masking -> ed,       (*apply only to edges*)
    Interleaving -> True (*apply to all color chanels at once*)]
 ]

结果是:

enter image description here

编辑

对于面向 Mathematica 的读者来说,最后一个循环的功能代码可能更容易(也更短):

NestList[
 ImageFilter[  
   If[Length[#1] ==  1, #1[[1, 1]], {0, 0, 0}] &@
     Cases[Tally[Flatten[#1, 1]], Except[{0.` {1, 1, 1}, _}]] & , #, 1,
   Masking      -> Dilation[EdgeDetect[#, 1], RandomInteger[{0, 1}, {3, 3}]],  
   Interleaving -> True ] &,
 WatershedComponents@GradientFilter[Binarize[id,.1],1]/.{4-> 0}//Colorize, 
5]

关于Matlab:如何在图像中弯曲线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7963459/

相关文章:

matlab - 更好的(非线性)分箱

mysql - 在 MySQL 中存储 matlab 数组。再次

image - 定位系统中相机倾斜的程序校正

python - 在包含图像的数组中创建批处理

batch-file - For循环批处理中的多个条件?

matlab - 训练神经网络技巧

matlab - 在 matlab 绘图的标记内插入字符串

c++ - opencv中的轮廓选择

textarea - 换行符在 Textarea 输出中不起作用

android - 去掉 TabWidget 下的那一行