1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
| #define NOMINMAX
#include "CImg.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <limits>
#include <cmath>
#include <string>
#define round(x) (x<0?ceil((x)-0.5):floor((x)+0.5))
#define PI (3.1415926535897932384626433832795028841971693992)
using namespace cimg_library;
using namespace std;
bool is_readable( const std::string & file )
{
std::ifstream fichier( file.c_str() );
return !fichier.fail();
}
int main() {
/** Import of the image of the Workpiece using CImg Library
For more information about this Image Processing Library go to http://cimg.sourceforge.net/ */
CImg<double> image("Workpiece.bmp");
CImg<double> image_w=image;
int nx = image.dimx();
int ny = image.dimy();
cout <<"*******************************************************************************"<<endl;
cout <<"********** CALCULUS OF THE ENGAGEMENT ANGLE USING IMAGE PROCESSING ************"<<endl;
cout <<"*******************************************************************************"<<endl;
cout <<"*******************************************************************************"<<endl;
cout <<"******* LICP- Created by Nicolas Planquet - Master Thesis - June 2009 *********"<<endl;
cout <<"*******************************************************************************"<<endl;
cout <<"****************SWISS FEDERAL INSTITUTE OF TECHNOLOGY**************************"<<endl;
cout <<"*******************************************************************************"<<endl;
cout<<"Information about input picture : "<<endl;
cout << "Number of Pixel in x : " << nx << " and in y : " << ny << " total of pixels : " << nx*ny <<std::endl;
cout <<"*******************************************************************************"<<endl;
cout <<"*******************************************************************************"<<endl;
cout<<"Information about the tool and the workpiece : "<<endl;
double WorkpieceLength = 210;
double ToolDiameter = 20;
double scale = WorkpieceLength/nx ;
int compteur=0;
// Calcul de la taille d'un pixel en mm (représente la résolution de la matrice)
int NbToolPixel = round(ToolDiameter / scale) ;
int NbToolPixelReel = NbToolPixel;
int n = NbToolPixel/2;
if(NbToolPixel%2==0)
NbToolPixel++;
int startX=-1;
int startY=-1;
cout << "Tool Diameter : " << ToolDiameter <<" mm"<<endl;
cout << "Workpiece Length : " << WorkpieceLength <<" mm"<<endl;
cout <<"Scale : "<< scale <<" mm/px"<<endl;
cout <<"*******************************************************************************"<<endl;
cout <<"*******************************************************************************"<<endl;
cout <<"*******************************************************************************"<<endl;
/** Lecture du fichier d'entrée pour construire le tableau 2D représentant l'outil */
cout<<" "<<endl;
int** Tool = new int* [NbToolPixel] ;
for (int i=0 ;i<NbToolPixel ;i++)
{
Tool[i] = new int[NbToolPixel] ;
}
int** ToolDetect = new int* [NbToolPixel] ;
for (int i=0 ;i<NbToolPixel ;i++)
{
ToolDetect[i] = new int[NbToolPixel] ;
}
for(int i=0;i<NbToolPixel;i++){
for(int j=0;j<NbToolPixel;j++)
{
ToolDetect[i][j]=0;
double distance = 0 ;
distance = sqrt((double)(i-n)*(i-n)+(j-n)*(j-n));
if(round(distance)<n)
Tool[i][j]=0;
else
Tool[i][j]=1;
}
}
for(int i=0;i<NbToolPixel;i++){
for(int j=0;j<NbToolPixel;j++)
{
int testvoisin=0;
if(Tool[i][j]==0)
{
if(i>0 && j>0 && Tool[i-1][j-1]==1)
testvoisin++;
if(i>0 && Tool[i-1][j]==1)
testvoisin++;
if(i>0 && j<NbToolPixel-1 && Tool[i-1][j+1]==1)
testvoisin++;
if(j<NbToolPixel-1 && Tool[i][j+1]==1)
testvoisin++;
if(j<NbToolPixel-1 && i<NbToolPixel-1 && Tool[i+1][j+1]==1)
testvoisin++;
if(i<NbToolPixel-1 && Tool[i+1][j]==1)
testvoisin++;
if(i<NbToolPixel-1 && j>0 && Tool[i+1][j-1]==1)
testvoisin++;
if(j>0 && Tool[i][j-1]==1)
testvoisin++;
if(testvoisin>=2)
Tool[i][j]=5;
}
}
}
Tool[0][n]=5;
Tool[n][0]=5;
Tool[NbToolPixel-1][n]=5;
Tool[n][NbToolPixel-1]=5;
/** Recherche du point de depart du toolpath */
for(int i=0;i<nx;i++){
for(int j=0;j<ny;j++){
if(image_w(i,j,0)==255 && image_w(i,j,1)==255 && image_w(i,j,2)==0){
startX=i;
startY=j;
// Blanc
image_w(startX,startY,0)=255;
image_w(startX,startY,1)=255;
image_w(startX,startY,2)=255;
}
}
}
if(startX==-1 || startY==-1){
cout<<"ERROR : Starting point of the toolpath not found, please mark it with this color (yellow in paint) R:255 V:255 B:0"<<endl;
Sleep(20000);
return 0;}
/** Usinage au point de départ du toolpath
On parcourt le tableau contenant l'information sur l'outil et l'image aux coordonnées
correspondantes et on réalise une operation booleenne*/
for(int i =-n ; i<=n;i++){
for(int j =-n ; j<=n;j++){
if(startX+i>=0 && startY+j>=0 && startX+i<nx && startY+j<ny && i+n>=0 && j+n>=0 && i+n<NbToolPixel && j+n<NbToolPixel){
int PieceStart1 = image_w(startX+i,startY+j,0);
int PieceStart2 = image_w(startX+i,startY+j,1);
int PieceStart3 = image_w(startX+i,startY+j,2);
int ToolStart = Tool[i+n][j+n];
if(PieceStart1==0 && PieceStart2==0 && PieceStart3==0 && ToolStart!=1){
if(ToolStart == 5) {
// on active la partie de l'outil en contact avec la pièce pour le calcul de l'angle
ToolDetect[i+n][j+n]=6;
}
image_w(startX+i,startY+j,0)=255;
image_w(startX+i,startY+j,1)=255;
image_w(startX+i,startY+j,2)=255;
}
}
}
}
ofstream resultat;
resultat.open("resultat.txt", ios::out);
if (resultat.bad())
return 1;
cout<<"SIMULATION AND COMPUTATION IN PROGRESS ..."<<endl;
double EngagementAngle=0;
int cas=0;
int CoordAngleStartX1=0;
int CoordAngleStartY1=0;
int CoordAngleStartX2=0;
int CoordAngleStartY2=0;
double distance=0;
double distanceUpdate=0;
double coordinateX=0;
double coordinateY=0;
double distanceStartAngle1=0;
double distanceStartAngle2=0;
int OrigineX=0;
int OrigineY=0;
double StartAngle=-1;
double ExitAngle=-1;
double min=-1;
double longueur=0;
while((image_w(startX-1,startY-1,0)==255 && image_w(startX-1,startY-1,1)==0 && image_w(startX-1,startY-1,2)==0) ||
(image_w(startX-1,startY,0)==255 && image_w(startX-1,startY,1)==0 && image_w(startX-1,startY,2)==0) ||
(image_w(startX-1,startY+1,0)==255 && image_w(startX-1,startY+1,1)==0 && image_w(startX-1,startY+1,2)==0) ||
(image_w(startX,startY+1,0)==255 && image_w(startX,startY+1,1)==0 && image_w(startX,startY+1,2)==0) ||
(image_w(startX+1,startY+1,0)==255 && image_w(startX+1,startY+1,1)==0 && image_w(startX+1,startY+1,2)==0) ||
(image_w(startX+1,startY,0)==255 && image_w(startX+1,startY,1)==0 && image_w(startX+1,startY,2)==0) ||
(image_w(startX+1,startY-1,0)==255 && image_w(startX+1,startY-1,1)==0 && image_w(startX+1,startY-1,2)==0) ||
(image_w(startX,startY-1,0)==255 && image_w(startX,startY-1,1)==0 && image_w(startX,startY-1,2)==0))
{
if(image_w(startX+1,startY,0)==255 && image_w(startX+1,startY,1)==0 && image_w(startX+1,startY,2)==0){
startX=startX+1;
cas=1;
}
else if(image_w(startX-1,startY,0)==255 && image_w(startX-1,startY,1)==0 && image_w(startX-1,startY,2)==0){
startX=startX-1;
cas=2;
}
else if(image_w(startX,startY-1,0)==255 && image_w(startX,startY-1,1)==0 && image_w(startX,startY-1,2)==0){
startY=startY-1;
cas=4;
}
else if(image_w(startX,startY+1,0)==255 && image_w(startX,startY+1,1)==0 && image_w(startX,startY+1,2)==0){
startY=startY+1;
cas=3;
}
else if(image_w(startX-1,startY+1,0)==255 && image_w(startX-1,startY+1,1)==0 && image_w(startX-1,startY+1,2)==0){
startX=startX-1;
startY=startY+1;
}
else if(image_w(startX+1,startY+1,0)==255 && image_w(startX+1,startY+1,1)==0 && image_w(startX+1,startY+1,2)==0){
startX=startX+1;
startY=startY+1;
}
else if(image_w(startX+1,startY-1,0)==255 && image_w(startX+1,startY-1,1)==0 && image_w(startX+1,startY-1,2)==0){
startX=startX+1;
startY=startY-1;
}
else if(image_w(startX-1,startY-1,0)==255 && image_w(startX-1,startY-1,1)==0 && image_w(startX-1,startY-1,2)==0){
startX=startX-1;
startY=startY-1;
}
coordinateX=startX*scale;
coordinateY=(ny-startY)*scale;
//On efface le point rouge du toolpath
image_w(startX,startY,0)=255;
image_w(startX,startY,1)=255;
image_w(startX,startY,2)=255;
//On efface la piece en contact avec l'outil
for(int i =-n ; i<=n;i++){
for(int j =-n ; j<=n;j++){
if(startX+i>=0 && startY+j>=0 && startX+i<nx && startY+j<ny && i+n>=0 && j+n>=0 && i+n<NbToolPixel && j+n<NbToolPixel){
int PieceStart1 = image_w(startX+i,startY+j,0);
int PieceStart2 = image_w(startX+i,startY+j,1);
int PieceStart3 = image_w(startX+i,startY+j,2);
int ToolStart = Tool[i+n][j+n];
if((PieceStart1==0||PieceStart1==255) && PieceStart2==0 && PieceStart3==0 && ToolStart == 5){
// on active la partie de l'outil en contact avec la pièce pour le calcul de l'angle
ToolDetect[i+n][j+n]=6;
}
if(PieceStart1==0 && PieceStart2==0 && PieceStart3==0 && ToolStart!=1){
image_w(startX+i,startY+j,0)=255;
image_w(startX+i,startY+j,1)=255;
image_w(startX+i,startY+j,2)=255;
}
}
}
}
/** Calcul de l'angle en fonction du nombre de pixel relevés */
for(int i=0;i<NbToolPixel;i++){
for(int j=0;j<NbToolPixel;j++){
if(ToolDetect[i][j]==6){
for(int x=0;x<NbToolPixel;x++){
for(int y=0;y<NbToolPixel;y++){
if(ToolDetect[x][y]==6||ToolDetect[x][y]==7){
// Compute the euclidian distance between the current point and the past point
distanceUpdate=sqrt((double)(x-i)*(x-i)+(y-j)*(y-j));
//Check if the distance is the max
if(distanceUpdate>distance){
//if yes save the max and update it with the new value
distance=distanceUpdate;
CoordAngleStartX1=i;
CoordAngleStartY1=j;
CoordAngleStartX2=x;
CoordAngleStartY2=y;
}
}
}
}
}
}
}
if(cas==1)
{
// if it goes right origin is
OrigineX=n;
OrigineY=0;
}
else if(cas==2)
{
// if it goes left origin is
OrigineX=n;
OrigineY=NbToolPixel-1;
}
else if(cas==3)
{
// if it goes down origin is
OrigineX=NbToolPixel-1;
OrigineY=n;
}
else if(cas==4)
{
// if it goes up origin is
OrigineX=0;
OrigineY=n;
}
distanceStartAngle1=sqrt((double)(CoordAngleStartX1-OrigineX)*(CoordAngleStartX1-OrigineX)+(CoordAngleStartY1-OrigineY)*(CoordAngleStartY1-OrigineY));
distanceStartAngle2=sqrt((double)(CoordAngleStartX2-OrigineX)*(CoordAngleStartX2-OrigineX)+(CoordAngleStartY2-OrigineY)*(CoordAngleStartY2-OrigineY));
if(distanceStartAngle1<distanceStartAngle2)
min=distanceStartAngle1;
else
min=distanceStartAngle2;
if(min!=0)
StartAngle=2*asin(min/(NbToolPixel-1))*(180/PI);
else
StartAngle=0;
for(int i=0;i<NbToolPixel;i++){
for(int j=0;j<NbToolPixel;j++){
ToolDetect[i][j]=0;
}
}
/** CF formule Planquet Master Thesis report */
if(distance!=0)
EngagementAngle = 2*asin(distance/(NbToolPixel-1))*(180/PI);
else
EngagementAngle = 0 ;
ExitAngle=StartAngle+EngagementAngle;
compteur++;
longueur=compteur*scale;
//cout<<compteur<<endl;
resultat<<compteur<<" "<<coordinateX<<" "<<coordinateY<<" "<<round(StartAngle)<<" "<<round(ExitAngle)<<endl;
}
cout<<"... COMPUTATION ENDED ! see resultat.txt"<<endl;
//resultat.close(); //on ferme le fichier pour liberer la mémoire
return 0;
} |
Partager