MAJ recadrage ~fonctionnel

This commit is contained in:
ce806665 2019-10-06 18:59:32 +02:00
parent 325f638301
commit 3e44f921d6

View File

@ -1,37 +1,50 @@
package lecturePdf; package lecturePdf;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
public class Recadrage { public class Recadrage {
String dir=""; String dir="";
String filename=""; String filename="";
BufferedImage img; BufferedImage img;
public Recadrage(String directory,String file) { public Recadrage(String directory,String file) {
dir=directory; dir=directory;
filename=file; filename=file;
File f =new File(dir+filename); File f =new File(dir+filename);
try {
img=ImageIO.read(f);
} catch (IOException e) {
//System.out.println(e);
}
}
try { public Recadrage(BufferedImage image) {
img=ImageIO.read(f); img=image;
} catch (IOException e) { }
System.out.println(e);
}
}
public boolean estDroite() { //determine si l'image png/jpg du pdf est droite public boolean estDroite() { //determine si l'image png/jpg du pdf est droite (doit etre en noir et blanc)
int count=0; int count=0;
boolean stop=false; boolean stop=false;
for (int ty=0; ty<100 && !stop;ty++) { for (int ty=0; ty<100 && !stop;ty++) {
for (int tx=0;tx<img.getWidth();tx++) { for (int tx=0;tx<img.getWidth();tx++) {
Color tmp=new Color(img.getRGB(tx, ty)); Color tmp=new Color(img.getRGB(tx, ty));
if (tmp.getGreen()<10) { if (tmp.getGreen()<20) {
count++; count++;
stop=true; stop=true;
} }
@ -40,14 +53,357 @@ public class Recadrage {
if (count>img.getWidth()/5.4 && count <img.getWidth()/5) if (count>img.getWidth()/5.4 && count <img.getWidth()/5)
return true; return true;
return false; return false;
} }
public static void main(String avg[])
{
Recadrage rec=new Recadrage ("P:\\Bureau\\ptut s3\\eclipse\\image\\","sujet0.jpg");
System.out.println(rec.estDroite());
}
public void automation() throws IOException {
if (!this.estDroite()) {
int[][] points=RdB();
double angle=getAngle(points);
System.out.println("l'angle est de = "+angle);
//this.pivot(angle);
/*AffineTransform tx = new AffineTransform();
tx.rotate(angle, img.getWidth() / 2,img.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_BILINEAR);
BufferedImage image = op.filter(img, null);*/
img=rotate(img,angle);
String nomImage="sortie";
File nomfichier = new File("C:\\Users\\Xxsafirex\\Desktop\\Image\\" + nomImage + ".jpg");// ou jpg
ImageIO.write(img, "JPG", nomfichier);//ou JPG
}
}
public int[][] RdB() { // cherche les 4 points noirs
int[][] roar = new int[img.getWidth()*img.getHeight()][2];
int[][] regroupY = new int[img.getWidth()*img.getHeight()][2];
int boucleY=0;int groupYi=0;
int i=0;
int diamMax=0;
int radius=8; // 19= limite de detection de checkCircle
for (int ty=0; ty<img.getHeight();ty++) {
for (int tx=0;tx<img.getWidth();tx++) {
Color tmp=new Color(img.getRGB(tx, ty));
if (tmp.getGreen()<20) { //si le pixel est noir
if (checkCircle(tx,ty,radius) ) { //verifie si un cercle de radius entoure le pixel
roar[i][0]=tx;
roar[i][1]=ty;
i++;
}
}
}
}
//System.out.println("fin");
int tempora=0;
int tmp=1;
int roar2[][]=new int [img.getWidth()*img.getHeight()][2];
roar2[0]=roar[0];
for (int l=0;l<img.getHeight()*img.getWidth() && (roar[l][1]!=0 || roar[l][0]!=0);l++) {
if((roar[l][0]-roar2[tmp-1][0])<5 || roar[l][1]-roar2[tmp-1][1]<5 ){ //x-(x-1)>5 ou y-(y-1)>5
roar2[tmp][0]=roar[l][0]; //efface le precedent roar2 si il était a 1 pixel de diff
roar2[tmp][1]=roar[l][1];
}
else {
tmp++;
roar2[tmp]=roar[l];
}
}
//boucle de determination des points noirs
//System.out.println("roar2debut");
int points[][]=new int [4][2];
int lastRoar[][]=new int [roar2.length][2]; int boucl=0; int lasti=0;
int t=0;
for (int l=1;l<=img.getHeight()*img.getWidth() && (roar2[l-1][1]!=0 || roar2[l-1][0]!=0);l++) {
int diffx=roar2[l][0]-roar2[l-1][0];
int diffy=roar2[l][1]-roar2[l-1][1];
int diff=Math.abs(diffx)+Math.abs(diffy);
if (diff>img.getWidth()*0.85)
{
points[t]=roar2[l];
t++;
}
if (diffx<10 && diffy<10) {
boucl++;
}
else {
lastRoar[lasti][0]=roar2[l-boucl/2][0];
lastRoar[lasti][1]=roar2[l-boucl/2][1];
lasti++;boucl=0;
}
}
//System.out.println("lasrorar");
for (int l=0;l<=lastRoar.length && (lastRoar[l][1]!=0 || lastRoar[l][0]!=0);l++) {
//System.out.println("x: "+lastRoar[l][0]+" y: "+lastRoar[l][1]); //reste a grouper les coord pour avoir le centre des ronds
}
for (int l=0;l<=lastRoar.length && (lastRoar[l][1]!=0 || lastRoar[l][0]!=0);l++) {
boolean test=true;
int maxPoint=0;
for (int li=0;li<=points.length && (points[li][1]!=0 || points[li][0]!=0);li++) {
int diffx= Math.abs(lastRoar[l][0]-points[li][0]);
int diffy= Math.abs(lastRoar[l][1]-points[li][1]);
boolean testx= diffx>img.getWidth()*0.85 || diffx<img.getWidth()*0.2; //diff <0.1 ou >0.8 x la largeur de feuille
boolean testy= diffy>img.getHeight()*0.8 || diffy<img.getWidth()*0.2;
boolean Repeat= diffx+diffy>img.getWidth()*0.2; //si point deja présent
if (!Repeat || (!testx || !testy) ) // si 0.2>diffx>0.8 ou "diffy" et
{
test=false;
}
maxPoint=li;
}
if(test && maxPoint<2) {
//System.out.println(lastRoar[l][0]+" "+lastRoar[l][1]);
points[maxPoint+1][0]=lastRoar[l][0];
points[maxPoint+1][1]=lastRoar[l][1];
}
}
//System.out.println("point");
for (int l=1;l<=points.length;l++) {
System.out.println("x: "+points[l-1][0]+" y: "+points[l-1][1]);
}
return points;
}
public boolean checkCircle(int x,int y,int radius) {
double pi=Math.PI; //3.14
Color tmp;
for (double k=-1;k<=1;k+=0.05) { // de 0 à 2 pi
int px=x+(int)Math.round(radius*Math.cos(k*pi)); //px = pos x du contour du supposé cercle
int py=y+(int)Math.round(radius*Math.sin(k*pi)); //diam calculé +/- 42 pixels
if(py<0)py=0;
if(py>img.getHeight())py=img.getHeight()-1;
if(px<0)px=0;
if (px>img.getWidth())px=img.getWidth()-1;
tmp=new Color(img.getRGB(px, py));
if (tmp.getGreen()>20) { //si pixel == blanc
return false;
}
}
return true;
}
public double getAngle(int[][] tab) {
double[] res= new double[4];int resi=0;
int cmpt=0;
int angle=0;
for (int i=0;i<tab.length;i++) {
if (!(tab[i][0]==0) || !(tab[i][1]==0))
cmpt++;
}
//System.out.println("cmpt = "+cmpt);
if (cmpt<=1) {
return 0; //RIP CODE
}
for (int i=1;i<tab.length && (!(tab[i][0]==0) || !(tab[i][1]==0));i++) {
for(int c=1;i<tab.length && (tab[c][0]!=0 || tab[c][1]!=0);i++)
{
int diffx=tab[i-1][0]-tab[i][0];
int diffy=tab[i-1][1]-tab[i][1];
double yb=tab[i][1];
double xb=tab[i][0];
double ya=tab[i-1][1];
double xa=tab[i-1][0];
double pointy,pointx;
pointy=yb; //pointy/x = coord du 3e point de triangle rectangle
pointx=xa;
//System.out.println("xb"+ xb + " xa "+xa);
double dhypo=Math.sqrt(Math.pow(xb-xa,2)+Math.pow(yb-ya,2));//(yb-ya)/(xb-xa);
double dadj=Math.sqrt(Math.pow(xb-pointx, 2)+Math.pow(yb-pointy, 2)); //adjacent / rapport a xb,yb
System.out.println("dadj "+dadj + " dhypo "+dhypo);
if (dhypo<img.getWidth() && dhypo!=0) { //deux points selectionnés sont des diagonales
double retour=Math.acos(dadj/dhypo)*(180/Math.PI);
if (retour>90/2)
retour=180-90-retour;
if((xa<xb && ya<yb )||( xb<xa && yb<ya)) //point de droite plus haut que celui de gauche
return -retour;
else
return retour;
}
/*else { //deux points sont en diagonnale
double retour=Math.acos(dadj/dhypo)*(180/Math.PI); // ne marche pas
return (Math.abs(45-retour)/2);
}*/
}
}
return 0;
}
private int[] circle() //code de paul
{
int[] coor = new int[2];
for (int y=0; y<img.getHeight();y++) { //y de 0 a ymax
for (int x=0; x<img.getWidth();x++) { // x a xmax
Color tmp=new Color(img.getRGB(x, y));
if(tmp.getBlue() < 10) //si pixel noir
{
int tempX = x;
int tempY = y;
int tempX2 = x;
int tempY2 = y;
int longueur = 0;
int diam = 0;
Color tmp2 = new Color(img.getRGB(tempX, y));
while(tmp2.getBlue()< 10)
{
tempX++;
longueur++;
tmp2 =new Color(img.getRGB(tempX, y));
}
tempX2 = x+(longueur/2);
tmp2 = new Color(img.getRGB(tempX2, tempY));
while(tmp2.getBlue() < 10)
{
tempY++;
diam++;
tmp2 =new Color(img.getRGB(tempX2, tempY));
}
tempY2 = y+(diam/2);
while(tmp2.getBlue() < 10)
{
if(x == tempX2 + (int)(Math.sin(x)) && y == (int)(Math.cos(y))) return new int[] {tempX,tempY};
tmp2 = new Color(img.getRGB((int)(tempX+Math.sin(x)),(int) (tempY+Math.cos(y))));
x++;
y++;
}
x = tempX2;
y = tempY2;
}
}
}
return new int[] {-1,-1};
}
//rotation de https://stackoverflow.com/questions/37758061/rotate-a-buffered-image-in-java
public static BufferedImage rotate(BufferedImage bimg, double angle) {
int w = bimg.getWidth();
int h = bimg.getHeight();
BufferedImage rotated = new BufferedImage(w, h, bimg.getType());
Graphics2D graphic = rotated.createGraphics();
graphic.rotate(Math.toRadians(angle), w/2, h/2);
graphic.drawImage(bimg, null, 0, 0);
graphic.dispose();
return rotated;
}
public static void main(String avg[]) throws IOException
{
//Recadrage3 rec=new Recadrage3 ("P:\\Bureau\\ptut s3\\eclipse\\image\\","sujet1.jpg");
Recadrage rec=new Recadrage ("C:\\Users\\Xxsafirex\\Desktop\\Image\\","sujet1.4.jpg");
/*
int[] listPos=new int[500];
listPos[0]=5+900;
listPos[1]=7+800;
//System.out.println(listPos[0]==905);
*/
//System.out.println(rec.estDroite());
/*
int x=5,y=5,radius=2;
double pi= Math.PI;
double k=-0.75; //k=0 = droit du cercle
int px=x+(int)Math.round(radius*Math.cos(k*pi)); //px = pos x du contour du supposé cercle
int py=y+(int)Math.round(radius*Math.sin(k*pi));
//System.out.println("x : "+px+" y: "+py);
*/
String nomImage="rendu";
File nomfichier = new File("C:\\Users\\Xxsafirex\\Desktop\\Image\\" + nomImage + ".jpg");// ou jpg
ImageIO.write(rec.img, "JPG", nomfichier);//ou JPG
rec.automation();
//rec.circlePixel();
/*int[] tmp= new int[2];
tmp=rec.circle(); code de paul
//System.out.println("x= "+tmp[0]+" y="+tmp[1]);*/
}
} }