我正在使用OpenCV作为我的图像处理算法,我正在尝试修复字符中的参差不齐的边缘。我读到形态Hit-Miss变换是一个非常好的解决方案。有没有这个开源实现?
或者是否有其他算法可用于修复不规则边缘?
答案 0 :(得分: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中的例子(它来自Gonzales等人的“数字图像处理”一书)
答案 1 :(得分:0)
使用Marvin进行组合形态学扩张和侵蚀产生以下结果:
源代码:
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();
}
}