2012-08-13 21 views
4

我正在使用OpenCV處理我的圖像處理算法,並且正在嘗試修復人物中不齊的邊緣。我讀到形態學命中 - 小姐轉換是一個非常好的解決方案。有沒有這個開源的實現?形態學撞擊小姐變形

或者是否有任何其他算法可以用來修復不齊邊?

Some sample letters that need fixing

+0

您可以發佈圖片的例子嗎? – 2012-08-14 16:08:12

+0

@RégisB。張貼了一些樣品 – go4sri 2012-08-16 04:04:19

+0

你在哪裏閱讀?我認爲Hit-Miss本身還不夠,你需要使用從它衍生出來的形態學操作。請參閱http://www.ee.lamar.edu/gleb/dip/10-2%20-%20Morphological%20Image%20Processing.pdf – nkint 2013-01-21 21:42:19

回答

2

的簡單實現的命中和錯過的可以發現here

#include <opencv2/imgproc/imgproc.hpp> 

// Hit-or-miss transform function 
void hitmiss(cv::Mat& src, // Source image, 8 bit single-channel matrix 
      cv::Mat& dst, // Destination image 
      cv::Mat& kernel) // Kernel. 1=foreground, -1=background, 0=don't care 
{ 
    CV_Assert(src.type() == CV_8U && src.channels() == 1); 

    cv::Mat k1 = (kernel == 1)/255; 
    cv::Mat k2 = (kernel == -1)/255; 

    cv::normalize(src, src, 0, 1, cv::NORM_MINMAX); 

    cv::Mat e1, e2; 
    cv::erode(src, e1, k1); 
    cv::erode(1 - src, e2, k2); 

    dst = e1 & e2; 
} 

但我認爲你可以解決該問題僅出現擴張,爲page 7 of this slide的例子(其取自岡薩雷斯等人的「數字圖像處理」書)

0

結合形態學擴張和侵蝕使用Marvin產生的結果如下:

enter image description here

的源代碼:

package characterRestoration; 

import marvin.image.MarvinColorModelConverter; 
import marvin.image.MarvinImage; 
import marvin.io.MarvinImageIO; 
import marvin.plugin.MarvinImagePlugin; 
import marvin.util.MarvinPluginLoader; 

public class CharacterRestoration { 

MarvinImage     image = MarvinImageIO.loadImage("./res/character_in.png"); 
private MarvinImagePlugin dilation = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.dilation"); 
private MarvinImagePlugin erosion = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.morphological.erosion"); 

private boolean[][]   matrixD = new boolean[][]{ 
          {false,false,false,false,false,false,false,false,false}, 
          {false,false,false,false,false,false,false,false,false}, 
          {false,false,false,false,false,false,false,false,false}, 
          {false,false,true,true,true,true,true,true,true}, 
          {false,false,true,true,true,true,true,true,true}, 
          {false,false,true,true,true,true,true,true,true}, 
          {false,false,false,false,false,false,false,false,false}, 
          {false,false,false,false,false,false,false,false,false}, 
          }; 

private boolean[][]   matrixE = new boolean[][]{ 
          {true,true,true}, 
          {true,true,true}, 
          {true,true,true} 
          };   

public CharacterRestoration(){ 
    // Convert image to binary format 
    image = MarvinColorModelConverter.rgbToBinary(image, 125); 

    // Morphological Dilation 
    dilation.setAttribute("matrix", matrixD); 
    dilation.process(image.clone(), image); 

    // Morphological Erosion 
    erosion.setAttribute("matrix", matrixE); 
    erosion.process(image.clone(), image); 

    MarvinImageIO.saveImage(image, "./res/character_out.png"); 
} 

public static void main(String[] args) { 
    new CharacterRestoration(); 
} 

}