画像処理アルゴリズムにOpenCVを使用していて、文字の不規則なエッジを修正しようとしています。形態学的なHit-Miss変換は、このための非常に優れたソリューションであると読みました。これのオープンソース実装はありますか?
または、不規則なエッジを修正するために使用できる他のアルゴリズムはありますか?
画像処理アルゴリズムにOpenCVを使用していて、文字の不規則なエッジを修正しようとしています。形態学的なHit-Miss変換は、このための非常に優れたソリューションであると読みました。これのオープンソース実装はありますか?
または、不規則なエッジを修正するために使用できる他のアルゴリズムはありますか?
ヒットアンドミスの簡単な実装はここにあります:
#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;
}
しかし、このスライドの7ページの例のように、拡張によってのみ問題を解決できると思います(ゴンザレスらの「デジタル画像処理」の本から引用)。
マーヴィンを使用した形態学的拡張と侵食の組み合わせにより、以下の結果が得られました。
ソースコード:
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();
}
}