Handwritten numbers viewer (Developed in Java, version 2)

In this example we can see a small Java-code that loads a corpus of handwritten numbers and draw it on a panel.

public class Graficador extends javax.swing.JFrame{

	private final String RUTA="numeros.data";
	
	private final int KTE_FILAS=16;
	
	private final int KTE_COLS=16;
	
	private java.util.ArrayList numeros;
	
	private Lienzo lienzo;

	public Graficador(){
		super("Graficador Numeros Proyecto Integrador - IA");
		setSize(833,833);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		numeros=new java.util.ArrayList();
		lienzo=new Lienzo();
		setLayout(new java.awt.BorderLayout());
		add(createControls(),java.awt.BorderLayout.NORTH);
		add(lienzo,java.awt.BorderLayout.CENTER);
	}
	
	
	private javax.swing.JPanel createControls(){
		javax.swing.JButton cargar=new javax.swing.JButton("Cargar data");
		final javax.swing.JComboBox lista=new javax.swing.JComboBox();
		javax.swing.JPanel panel=new javax.swing.JPanel(new java.awt.FlowLayout());
		panel.add(cargar);
		panel.add(new javax.swing.JLabel("Entradas de Datos/CORPUS: "));
		panel.add(lista);
		cargar.addActionListener(ae->{
			cargarDataBase(lista);
		});
		lista.addItemListener(ae->{
			int i=lista.getSelectedIndex();
			Numero n=numeros.get(i);
			lienzo.setMatrix(n.matrix);
		});
		return panel;
	}
	
	private class Lienzo extends javax.swing.JPanel{
	
		private int matrix[][];
	
		public Lienzo(){
			setBackground(java.awt.Color.WHITE);
			matrix=null;
		}
		
		public void setMatrix(int m[][]){
			matrix=m;
			repaint();
		}
		
		public void paint(java.awt.Graphics g){
			super.paint(g);
			java.awt.Dimension d=getSize();
			int stepX=((int)d.getWidth())/KTE_COLS;
			int stepY=((int)d.getHeight())/KTE_FILAS;
			g.setColor(java.awt.Color.BLUE);
			for(int i=0;i<(int)d.getWidth();i+=stepX){
				g.drawLine(i,0,i,(int)d.getHeight());
			}
				
			for(int i=0;i<((int)d.getHeight());i+=stepY){
				g.drawLine(0,i,(int)d.getWidth(),i);
			}
			if(matrix!=null){
				g.setColor(java.awt.Color.YELLOW);
				for(int i=0;i<matrix.length;i++){
					for(int j=0;j<matrix[0].length;j++){ if(matrix[j][i]!=0) g.fillRect(i*stepX,j*stepY,stepX,stepY); } } } } } private void cargarDataBase(javax.swing.JComboBox lista){ try{ java.io.BufferedReader buffer=new java.io.BufferedReader(new java.io.FileReader(RUTA)); String linea=""; Numero num=null; int cont=1; while((linea=buffer.readLine())!=null){ if(linea.trim().length()>3){
					num=string2Numero(linea.trim());
					numeros.add(num);
					lista.addItem((cont++)+" => "+num.numero);
				}
			}
			buffer.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private Numero string2Numero(String cad){
		Numero num=new Numero(cad);
		return num;
	}
	
	private class Numero{
		public int matrix[][];
		public String numero;
		
		public Numero(String linea){
			String data[]=linea.trim().split("(\\s+)");
			matrix=new int[KTE_FILAS][KTE_COLS];
			int k=0;
			for (int i=0;i<KTE_FILAS;i++){
				for(int j=0;j<KTE_COLS;j++){
					matrix[i][j]=new Float(data[k++]).intValue();
				}
			}
			
			numero="";
			for (int i=KTE_FILAS*KTE_COLS;i<data.length;i++){
				numero+=data[i]+"   ";
			}
			numero=numero.trim();
		}
	}
	
	public static void main(String[]args){
		Graficador g=new Graficador();
		g.setVisible(true);
	}
	
}

Thresholding in HSV and Trackbars

In this example is explained how to convert a image from RGB color space to HSV color space.

Likewise, the code described below includes a simple example of trackbar use.

 

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <string.h>
#include <time.h>
#include <stdlib.h>
using namespace cv;
using namespace std;

// Variables for minimum and maximun value of Hue
int hueMin=30;
int hueMax=170;

// Variables for minimum and maximun value of Saturation
int satMin=30;
int satMax=170;

// Variables for minimum and maximun value of Value
int valMin=30;
int valMax=170;

// Methods to handle changes values in trackbars
void eventoTrack1(int valor, void* p){
	//hueMin=valor;
	cout << "Valor Hue Min: " << hueMin << endl;
}

void eventoTrack2(int valor, void* p){
	//hueMin=valor;
	cout << "Valor Hue Max: " << hueMax << endl;
}

void eventoTrack3(int valor, void* p){
}
void eventoTrack4(int valor, void* p){
}
void eventoTrack5(int valor, void* p){
}
void eventoTrack6(int valor, void* p){
}


int main(int argc,char *argv[]){
	Mat frame;
	Mat hsv;
	Mat umbralizada;
	VideoCapture video(0);
	namedWindow("original",CV_WINDOW_AUTOSIZE);
	namedWindow("hsv",CV_WINDOW_AUTOSIZE);
	namedWindow("umbral",CV_WINDOW_AUTOSIZE);
	createTrackbar("HueMin","umbral",&hueMin,179,eventoTrack1);
	createTrackbar("HueMax","umbral",&hueMax,179,eventoTrack2);
	createTrackbar("SatMin","umbral",&satMin,255,eventoTrack3);
	createTrackbar("SatMax","umbral",&satMax,255,eventoTrack4);
	createTrackbar("ValMin","umbral",&valMin,255,eventoTrack5);
	createTrackbar("ValMax","umbral",&valMax,255,eventoTrack6);
	if(video.isOpened()){
		while(1==1){
			video >> frame;
			cvtColor(frame,hsv,CV_BGR2HSV);
			inRange(hsv,Scalar(hueMin,satMin,valMin),Scalar(hueMax,valMax,valMax),umbralizada);
			imshow("umbral",umbralizada);
			imshow("original",frame);
			imshow("hsv",hsv);
			if(waitKey(20)>0)
				break;
		}
	}
	return 0;
}

 

Handwritten Java visualizer example (Corpus Semeion)

This post shows a simple Java-code to visualize the handwritten digits of semeion dataset.

 

/**
	@author vlarobbyk
	Clase en Java que permite generar una imagen del digito que se ha escrito.
*/

public class Visualizador extends javax.swing.JFrame{
	
	private String fichero;
	private final int TAM=16;
	private int digito[][]=null;

	public Visualizador(String fichero){
		super("Visualizador de Digitos");
		this.fichero=fichero;
		setSize(510,510);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().setBackground(java.awt.Color.WHITE);
		javax.swing.JMenuBar barra=new javax.swing.JMenuBar();
		javax.swing.JMenu archivo=new javax.swing.JMenu("Archivo");
		javax.swing.JMenuItem sel=new javax.swing.JMenuItem("Seleccionar Fila");
		sel.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				String valor=javax.swing.JOptionPane.showInputDialog(Visualizador.this,"Ingrese la fila");
				try{
					Integer val=new Integer(valor);
					if(!(val>=0 && val<1593)){
						javax.swing.JOptionPane.showMessageDialog(Visualizador.this,"El rango valido de filas es [0,1592]");
					}else{
						digito=visualizar(val);
						System.out.println("Visualizando fila: "+val);
						repaint();
					}
				}catch(Exception ex){
					javax.swing.JOptionPane.showMessageDialog(Visualizador.this,"No es un numero valido...");
				}
			}
		});
		archivo.add(sel);
		barra.add(archivo);
		setJMenuBar(barra);
	}
	
	public void paint(java.awt.Graphics g){
		super.paint(g);
		java.awt.Graphics2D g2d=(java.awt.Graphics2D) g;
		java.awt.Dimension d=getSize();
		if(digito!=null){
			System.out.println(d.getWidth()+"!!"+d.getHeight());
			double dy=d.getWidth()/TAM;
			double dx=d.getHeight()/TAM;
			double paso1=dx;
			double paso2=dy;
			g2d.setColor(java.awt.Color.BLUE);
			java.awt.geom.Line2D.Double linea=null;
			java.awt.geom.Rectangle2D.Double rect=null;
			for(int i=0;i<TAM;i++){
				linea=new java.awt.geom.Line2D.Double(0.d,paso1,d.getWidth(),paso1);
				g2d.draw(linea);
				linea=new java.awt.geom.Line2D.Double(paso2,0.d,paso2,d.getHeight());
				g2d.draw(linea);
				paso1+=dx;
				paso2+=dy;
			}
			paso1=dx;
			paso2=dy;
			for(int i=0;i<TAM;i++){
				for(int j=0;j<TAM;j++){
					if(digito[i][j]==1){
						paso1=dx*j;
						paso2=dy*i;
						rect=new java.awt.geom.Rectangle2D.Double(paso1,paso2,dx,dy);
						g2d.fill(rect);
					}
				}
			}
		}
	}
	
	public int[][] visualizar(int fila){
		try{
			int matriz[][]=new int[TAM][TAM];
			java.io.BufferedReader buffer=new java.io.BufferedReader(new java.io.FileReader(fichero));
			int cont=0;
			String data=null;
			while((cont<fila)){
				data=buffer.readLine();
				cont++;
			}
			data=buffer.readLine();
			String datos[]=data.trim().split("(\\s)+");
			for(int i=0;i<datos.length;i++){
				System.out.print(datos[i]+"|");
			}
			for(int i=0,k=0;i<matriz.length;i++){
				for(int j=0;j<matriz[0].length;j++,k++){
					matriz[i][j]=new Double(datos[k]).intValue();
				}
			}
			return matriz;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[]args){
		new Visualizador("corpus_digitos.data").setVisible(true);
	}

}

 

Loading 3D objects in Java

/**
	Example that shows load 3D models in Java
	@author vlarob
*/
public class LoaderObj extends javax.swing.JFrame{

	private javax.media.j3d.Canvas3D canvas3D;
	private javax.media.j3d.BranchGroup bg;
	private javax.media.j3d.TransformGroup tg;
	private com.sun.j3d.utils.universe.SimpleUniverse simpleUniverse;
	/**
		Class constructor: generates the instance of canvas3D and simple universe.
		@param path - Specifies the name and path of 3D object model
	*/
	public LoaderObj(String path){
		super("Ejemplo de carga de objetos 3D");
		setSize(400,400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		canvas3D=new javax.media.j3d.Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
		add(canvas3D);
		
		bg=new javax.media.j3d.BranchGroup();
		tg=new javax.media.j3d.TransformGroup();
		tg.setCapability(javax.media.j3d.TransformGroup.ALLOW_TRANSFORM_READ);
		tg.setCapability(javax.media.j3d.TransformGroup.ALLOW_TRANSFORM_WRITE);

		
		javax.media.j3d.BoundingSphere bs=new javax.media.j3d.BoundingSphere();
		com.sun.j3d.utils.behaviors.mouse.MouseRotate mr=new com.sun.j3d.utils.behaviors.mouse.MouseRotate(tg);
		mr.setSchedulingBounds(bs);
		com.sun.j3d.utils.behaviors.mouse.MouseZoom mz=new com.sun.j3d.utils.behaviors.mouse.MouseZoom(tg);
		mz.setSchedulingBounds(bs);
		com.sun.j3d.utils.behaviors.mouse.MouseTranslate mt=new com.sun.j3d.utils.behaviors.mouse.MouseTranslate(tg);
		mt.setSchedulingBounds(bs);

		javax.media.j3d.AmbientLight ambientLight=new javax.media.j3d.AmbientLight();
		ambientLight.setColor(new javax.vecmath.Color3f(1.f,0.f,0.0f));
		ambientLight.setInfluencingBounds(bs);
		tg.addChild(ambientLight);
		
		try{
			com.sun.j3d.loaders.objectfile.ObjectFile objectFile=new com.sun.j3d.loaders.objectfile.ObjectFile(com.sun.j3d.loaders.objectfile.ObjectFile.RESIZE);
			com.sun.j3d.loaders.Scene scene=objectFile.load(ClassLoader.getSystemResource("galleon.obj"));
			tg.addChild(scene.getSceneGroup());
			System.out.println(scene.getNamedObjects());
			System.out.println("Cargado...");
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		javax.media.j3d.TransformGroup tgEscala=new javax.media.j3d.TransformGroup();
		javax.media.j3d.Transform3D t3d=new javax.media.j3d.Transform3D();
// 		t3d.setScale(0.2f);
		tgEscala.setTransform(t3d);
		tgEscala.addChild(tg);

		bg.addChild(tgEscala);
		bg.addChild(mr);
		bg.addChild(mz);
		bg.addChild(mt);
		
		bg.compile();
		simpleUniverse=new com.sun.j3d.utils.universe.SimpleUniverse(canvas3D);
		simpleUniverse.addBranchGraph(bg);
		simpleUniverse.getViewingPlatform().setNominalViewingTransform();
	}
	
	public static void main(String []args){
		new LoaderObj("").setVisible(true);
	}
}

Free 3D models: http://tf3dm.com/3d-models/file/1/obj