package dokumenty; import java.io.File; import java.util.List; import pl.wroc.pwr.file.ImageFolderScanner; import pl.wroc.pwr.histogram.IHistogram1D; import pl.wroc.pwr.histogram.imagechannel.IntensityHistogramBuilder; import pl.wroc.pwr.image.IGSImage; import pl.wroc.pwr.image.IRGBImage; import pl.wroc.pwr.image.converter.RGBtoGSConverter; import pl.wroc.pwr.image.io.GenericLoader; import pl.wroc.pwr.image.io.GenericSaver; import pl.wroc.pwr.image.model.RGBImage; import pl.wroc.pwr.imagechannel.IImageChannel; import pl.wroc.pwr.imagechannel.basic.Invert; import pl.wroc.pwr.imagechannel.basic.Threshold; import pl.wroc.pwr.imagechannel.basic.ThresholdLower; import pl.wroc.pwr.imagechannel.basic.ThresholdUpper; import pl.wroc.pwr.imagechannel.edges.Sobel; import pl.wroc.pwr.imagechannel.filter.Mask; import pl.wroc.pwr.imagechannel.filter.median.MedianFilter; import pl.wroc.pwr.imagechannel.skeletonization.FastParallelThinning; public class DetektorGrafikiHistogram { private IImageChannel przygotujObraz(IImageChannel dataChannel, float thres) { dataChannel = new Invert(dataChannel).apply(); dataChannel = new Threshold(dataChannel, thres).apply(); dataChannel = new FastParallelThinning(dataChannel).apply(); return dataChannel; } private IRGBImage wczytaj(File plik) { if (!plik.exists()) { System.out.println("Plik o zadanej nazwie nie istnieje."); return null; } IRGBImage image = new GenericLoader(plik).apply(); if (image == null) { System.out.println("Błąd odczytu obrazu (plik istnieje). Proszę zmienić format pliku wejściowego."); return null; } return image; } private void przetworz(File plik) { IRGBImage image = wczytaj(plik); IGSImage grayImage = new RGBtoGSConverter(image).apply(); IImageChannel data = grayImage.getGrayChannel(); IHistogram1D hist = new IntensityHistogramBuilder(data, true).apply(); float minCutoff = 0.2f; float startCount = 0; int startIndex = 0; while (startCount < minCutoff) { startCount += hist.getValue(startIndex++); } float endCount = 0; int endIndex = 255; while (endCount < minCutoff) { endCount += hist.getValue(endIndex--); } data = new ThresholdLower(data, startIndex / 255.0f, 1).apply(); data = new ThresholdUpper(data, endIndex / 255.0f, 1).apply(); data = new MedianFilter(new Mask(5)).apply(data); data = new Sobel(0.3f).apply(data); //IImageChannel dane4 = detektor.przygotujObraz(grayImage.getGrayChannel(), 0.4f); //IImageChannel dane6 = detektor.przygotujObraz(grayImage.getGrayChannel(), 0.6f); //IImageChannel mul = new MultiplyChannel().apply(dane4, dane6); IRGBImage out = new RGBImage(data, data, data); new GenericSaver(out, new File("/home/mariusz/dane/nekst/wyniki/", plik.getName())).apply(); } public static void main(String[] args) { DetektorGrafikiHistogram d = new DetektorGrafikiHistogram(); List obrazy = new ImageFolderScanner().apply(new File("/home/mariusz/dane/nekst/dokumenty")); for (File obraz : obrazy) { if (!obraz.getName().toLowerCase().endsWith(".png")) { continue; } d.przetworz(obraz); } } }