Skip to content
Snippets Groups Projects
Commit a479c164 authored by Yonatan's avatar Yonatan
Browse files

Color identification works

parent 69223ae9
No related branches found
No related tags found
1 merge request!1Final merge
......@@ -18,8 +18,6 @@ public class Main {
public static void main(String[] args) throws IOException {
var file = new File("C:/Users/Yonatan/Desktop/04.jpg");
new ColorRecognizer().getColorAt(file, 0, 0, 100, 100);
var time = System.currentTimeMillis();
//TODO: 10/04/2023 transform originalImage for best Tesseract OCR results: grayscale, binarize, noise reduction, etc.
var originalImage = ImageIO.read(file);
......@@ -51,6 +49,9 @@ public class Main {
// basic GUI to render the bounding boxes resulting of the OCR recognition as well as testing the word merge algorithm
var colors = new ColorRecognizer();
colors.load(file);
var scale = 1.5;
var window = new JFrame();
window.setSize(new Dimension((int) (originalImage.getWidth() / scale), (int) (originalImage.getHeight() / scale)));
......@@ -63,6 +64,8 @@ public class Main {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
var time = System.currentTimeMillis();
g.drawImage(originalImage.getScaledInstance((int) (originalImage.getWidth() / scale), (int) (originalImage.getHeight() / scale), BufferedImage.SCALE_SMOOTH), 0, 0, null);
var g2d = ((Graphics2D) g);
g2d.setStroke(new BasicStroke(1.5f));
......@@ -73,15 +76,25 @@ public class Main {
var boundingBox = group.bounds();
g2d.setColor(new Color(255, 0, 255, 127));
g2d.drawRect((int) (boundingBox.x() / scale), (int) (boundingBox.y() / scale), (int) (boundingBox.width() / scale), (int) (boundingBox.height() / scale));
var color = colors.getColorAt(boundingBox.x(), boundingBox.y(), boundingBox.width(), boundingBox.height());
System.out.println(color);
boundingBox = boundingBox.withTolerance(1, 1);
g2d.setColor(color.getBgColor());
g2d.fillRect((int) (boundingBox.x() / scale), (int) (boundingBox.y() / scale), (int) (boundingBox.width() / scale), (int) (boundingBox.height() / scale));
g2d.setColor(Color.BLACK);
//g2d.drawString(group.toString(), (int) (boundingBox.x() / scale), (int) ((boundingBox.y() + boundingBox.height()) / scale));
g2d.drawString(group.toString(), (int) (boundingBox.x() / scale), (int) ((boundingBox.y() + boundingBox.height()) / scale));
}
//colors.free();
System.out.println("Draw time: " + (System.currentTimeMillis() - time));
}
};
window.add(imagePanel);
window.setVisible(true);
}
......
package es.yonatan.tfg.recognizer.color;
import es.yonatan.tfg.model.ColorData;
import net.sourceforge.lept4j.Box;
import net.sourceforge.lept4j.Leptonica1;
import net.sourceforge.lept4j.Pix;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.io.File;
import java.nio.IntBuffer;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class ColorRecognizer implements IColorRecognizer {
private @Nullable Pix pix;
private @Nullable Pix pix32;
@Override
public ColorData getColorAt(File image, int u, int v, int width, int height) {
var pix = Leptonica1.pixRead(image.getPath());
pix = Leptonica1.pixClipRectangle(pix, new Box(u, v, width, height, 0), null);
public ColorData getColorAt(int u, int v, int width, int height) {
if(pix == null || pix32 == null) throw new NullPointerException("Image not loaded");
var rBuffer = IntBuffer.allocate(32);
var gBuffer = IntBuffer.allocate(32);
var bBuffer = IntBuffer.allocate(32);
var pix32 = Leptonica1.pixConvertTo32(pix);
var occurrences = new HashMap<Color, Integer>();
var bufferr = IntBuffer.allocate(8);
var bufferg = IntBuffer.allocate(8);
var bufferb = IntBuffer.allocate(8);
for (int i = 0; i < width; i++) {
Leptonica1.pixGetRGBPixel(pix32, u + i, v, rBuffer, gBuffer, bBuffer);
var color = new Color(rBuffer.get(), gBuffer.get(), bBuffer.get());
rBuffer.clear();
gBuffer.clear();
bBuffer.clear();
occurrences.put(color, occurrences.getOrDefault(color, 0) + 1);
}
var sorted = occurrences.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue)).collect(Collectors.toList());
Collections.reverse(sorted);
Leptonica1.pixGetRGBPixel(pix, 1, 1, bufferr, bufferg, bufferb);
if (sorted.size() == 1)
sorted.add(sorted.get(0));
return new ColorData(sorted.get(0).getKey(), sorted.get(0).getKey());
}
System.out.println(bufferr.get() + " " + bufferg.get() + " " + bufferb.get());
@Override
public @NotNull IColorRecognizer load(@NotNull File image) {
pix = Leptonica1.pixRead(image.getPath());
pix32 = Leptonica1.pixConvertTo32(pix);
return this;
}
@Override
public void free() {
if (pix != null)
Leptonica1.pixFreeData(pix);
if (pix32 != null)
Leptonica1.pixFreeData(pix32);
return new ColorData(Color.BLACK, Color.WHITE);
//pix = pix32 = null;
}
}
package es.yonatan.tfg.recognizer.color;
import es.yonatan.tfg.model.ColorData;
import org.jetbrains.annotations.NotNull;
import java.io.File;
public interface IColorRecognizer {
ColorData getColorAt(File image, int u, int v, int width, int height);
ColorData getColorAt(int u, int v, int width, int height);
@NotNull IColorRecognizer load(@NotNull File image);
void free();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment