תכנות וזיהוי/קובץ פונקציות לזיהוי

מתוך ויקיספר, אוסף הספרים והמדריכים החופשי

הקובץ Conclude.cpp[עריכה]

/* Conclude.cpp
פענוח האות
reportfile קרא קובץ תעודות זהות העצמים
. והשוה אותו עם הנתונים  שהסקת מדגימת התמונה
: דווח לנו משהוא כזה
האות שצירת בעזרת העכבר דומה
לאות פלונית בתמונה
חשב את ההסתברות לנכונות הקביעה
 */
#include <windows.h>
#include <stdio.h>
#include "MyProcs.h"

/* מצביע לפונקציה כדי לעבד קובץ תמליל */
FILE *fopen();
/* שמור מפתח התכונות */
int KeyOf3Lines[KeysPerObject][ObjectsMax];
/* מערך השומר תמונה כמספרים דגומים */
int TheImage[RowsPerPic][ColsPerPic];
/* מערך של מלבני מרכיבי התכונות */
int Rects[AllComps][ComponDefSet];
/* מספר העצמים */
int NObjects;




/* קח את החלק הגרפי של המסך ומיקום של נקודה
פרק את מבנה הנתונים של הנקודה
: לארבעה חלקים
שני הביטים הראשונים  קובעים את הצבע האדום
שני הביטים הבאים קובעים את הצבע הירוק
שני הביטים הבאים קובעים את הצבע הכחול
שני הביטים הבאים קובעים אם דגמנו את התמונה
או דגמנו נקודה מחוץ לתמונה
החזר את הצבע האדום בשמונה רמות
כי סך הכול מעניין אותו לזהות אותיות
רק שמונה רמות של בהירות */
int MyPixelColorInC(HDC Myhdc,int ScreenLocateX,
                           int ScreenLocateY)
{COLORREF myColorf;
 int rColor,gColor,bColor,InImeg,output=-3;

 myColorf=GetPixel(Myhdc,ScreenLocateX,ScreenLocateY);
 rColor=  myColorf & RedMask;
 gColor=( myColorf & GreenMask )/(DuBites+1) ;
 bColor=( myColorf & BlueMask )/(QuBites+1) ;
 InImeg=( myColorf & InMask) /(SeBites+1) ;
 if(InImeg>0) output=-2;  /* מחוץ לתמונה */
 if(rColor>-1 && rColor<257  ) /* 256 צבעים*/
    /* הופכים ל - 8 רמות בהירות */
  //  output = rColor / 32;
   output = (rColor+gColor+bColor) / 96;
 return output;
}

void InitiateMyMatInC(int MyMatriz[RowsPerPic][ColsPerPic])
{
 int X,Y;
 int W = BrightLevels -1; // בהירות רקע
  for(Y=0;Y<RowsPerPic;Y++)
     for(X=0;X<ColsPerPic;X++)
               MyMatriz[X][Y]=W;
}


void  FillingGapsInC(
       int GapsMat[RowsPerPic][ColsPerPic])

{int X,Y,X1,X2,Y1,Y2;
 int W = BrightLevels -1; // בהירות רקע

 for(Y=0;Y<RowsPerPic-1;Y++)
  for(X=0;X<ColsPerPic-1;X++)
  {X1 = GapsMat[X][Y];
   X2 = GapsMat[X+1][Y];
   Y1 = GapsMat[X][Y+1];
   Y2 = GapsMat[X+1][Y+1];
    if (X1==W&&Y2==W&&Y1!=W&&X2!=W)
      GapsMat[X][Y]=0;
    if (X1!=W&&Y2!=W&&Y1==W&&X2==W)
      GapsMat[X+1][Y]=0;
  }
}

void EraseBackground(
    int InOutMat[RowsPerPic][ColsPerPic],
    int TheLimit)
{
  int X,Y;
  int W = BrightLevels -1; // בהירות רקע
  for(Y=0;Y<RowsPerPic;Y++)
     for(X=0;X<ColsPerPic;X++)
        {if (InOutMat[X][Y]>  TheLimit)
                 InOutMat[X][Y]=W;
                else  InOutMat[X][Y]=0;
         }
}

/* דגום חלק מהאות
שמור את ערכי הבהירות במערך */
void SelectPicInC(HDC hdcMe,
     float X1,float Y1,float X2,float Y2,
     int TheImage1[RowsPerPic][ColsPerPic],
     LineBrightness FilterType,int FilterN )
{int PixPicCol,PixPicRow=0;
 int xcordi,ycordi,TheColor;
 float MyPixPerRowDot,MyPixPerColDot;
 InitiateMyMatInC(TheImage1);
 MyPixPerRowDot=(X2-X1)/fXSections;
 MyPixPerColDot=(Y2-Y1)/fYSections;
 for(PixPicRow=0;PixPicRow<RowsPerPic;PixPicRow++)
  for(PixPicCol=0;PixPicCol<ColsPerPic;PixPicCol++)
   {xcordi=int(X1)+
           int(MyPixPerColDot*PixPicCol);
   ycordi=int(Y1)+
           int(MyPixPerRowDot*PixPicRow);
   TheColor=MyPixelColorInC(hdcMe,xcordi,ycordi);
   TheImage1[PixPicCol][PixPicRow] =TheColor;
   }                       // אל תתן לוקטור לחדור
  FillingGapsInC(TheImage1); //  באלכסון דרך האות
   if (FilterType !=AnyChange) // נקה את השינויים ברקע
    EraseBackground(TheImage1,FilterN);
}

/* מצא ערך של מרכיב התכונה */
int MyFindMeanInC(int MyFeature)
{ POINT From ,To ;
  int Out=0;

  From.x = Rects[MyFeature][0];
  From.y = Rects[MyFeature][1];
  To.x   = Rects[MyFeature][2];
  To.y   = Rects[MyFeature][3];
  if (MyFeature<RecComps )
     Out=   FindBarInL(From,To,TheImage);
  if (MyFeature>= RecComps &&
     MyFeature< RecComps+MaxComps)
     Out=AddMaxVecsInL(MyFeature,TheImage);
  if (MyFeature>= RecComps+MaxComps)
    Out =AddSumVecsInL(MyFeature,TheImage);
 return Out;
}


/* לך לשורה מסוימת על ידי קריאת כל
: משורות שלפני השורה בצורה זו
אורך כל שורה מתאים לכמות מסןימת של עצמים
לכל עצם מוקצבות מספר שורות */
void NewLinesInC(FILE *TheFilePointer,
      int NumLines, int Objects)
{int TheLine,TheAtt;
 int Stam[1];
 for(TheLine=0;TheLine<NumLines;TheLine++)
  for(TheAtt=0;TheAtt<Objects;TheAtt++)
   fscanf(TheFilePointer,"%d",&Stam[0]);
}

/* : מצא את מספר המפתחות
כל עצם יכול לקבל שלושה מפתחות
כל מפתח מכיל מרכיבים המסודרים בשורה
לפי אותו מספר סודר המתאים לעצם
שהם נותנים לו ציון אופיני
ביחס לאוביקט של נושא השורה
אחד מציוני המרכיבים הוא שלילי
אם יש בשורה שני
מספרים שליליים ומעלה
השורה אינה מפתח
לכן בדוק כמה שורות מתוך 3 שורות
של מפתחות האוביקט
מכילים רק מספר שלילי אחד
  */
int CkeckNumKeysInC(int TheSign)
{int TheAtt,KeyCounter=0,Stam[1];
 int Mone3Lines,MoneFailure=0;
 char Title[45];
 FILE *Refp;

 Refp=fopen(ReportFile,"r");
 /* קרא את הקובץ עד השורה המתאימה */
 NewLinesInC(Refp,NumLinesPerObject*TheSign+1,NObjects);
 for(Mone3Lines=0;Mone3Lines<KeysPerObject;Mone3Lines++)
 {for(TheAtt=0;TheAtt<NObjects;TheAtt++)
  {fscanf(Refp,"%d",&Stam[0]);
   if(Stam[0] == -1) MoneFailure++;
  }
  if(MoneFailure <2) KeyCounter++;
  /* בעיות במבנה הקובץ */
 // אות אחת לא מזוהה וזה הורס הכל
  if ((Mone3Lines==0)&&(MoneFailure >1) )
    {wsprintf(Title,"בעיות בזיהוי אוביקט  %d  ",
               TheSign+1    );
     MessageBox(NULL,Title ,"בעיות",0);
     fclose(Refp);
     return -1;}
  MoneFailure =0;
  NewLinesInC(Refp,2,NObjects);
 }
 fclose(Refp);
 return KeyCounter;
}

/* טען מקובץ לתוך מערך את המפתחות */
bool LoadArrayWithKeyInC(int TheSign,int Numkey,
         int Key3Line[LinesPerKey][ObjectsMax])
{FILE *Refp2;
 Refp2=fopen(ReportFile,"r");
 if (IsFileNotExistInF(Refp2)) return false;
 NewLinesInC(Refp2,
  TheSign*NumLinesPerObject+Numkey*KeysPerObject+1,
   NObjects);
 for(int LineNum=0;LineNum<LinesPerKey;LineNum++)
  for(int AttR=0;AttR<NObjects;AttR++)
   fscanf(Refp2,"%d",&Key3Line[LineNum][AttR]);
 fclose(Refp2);
 return true;
}

/* אם תכונה מסוימת מתאימה
לעצם שלנו ולא לעצם אחר היא תכונה טובה
הפונקציה מחזירה לנו את ההפרש
בין שני הציונים לתכונות
כדי שנוכל לדעת אם
להשתמש בתכונה הזאת */
int BdokTchunaInC(int TheSign,
               int AttribCounter)
{int Prob;
 int Attrib   =int(KeyOf3Lines[0][AttribCounter]);
 if (Attrib == -1) return 0;
 int ItsMean  =KeyOf3Lines[1][AttribCounter];
 int FailMean =KeyOf3Lines[2][AttribCounter];
 int MyMean   =MyFindMeanInC(Attrib);
 int Mark     =AbsDelta(MyMean,ItsMean);
 int FailMark =AbsDelta(FailMean,ItsMean);
 Prob=ProbIfFail[Mark]-ProbIfFail[FailMark];
 if (Prob<0) Prob=0;
 return Prob;
}


/* תן ציונים לתכונות כלומר
החזר את הציון הממוצע של תכונות האוביקט
עגל למעלה
כך שאם יהין לך מתוך 10 אפשרויות
תשעה מופעים  עם התכונה
אל תחזיר ממוצע 0 אלא 1 */
int CheckKeyInC(int TheSign,int NumKey)
{int Attrib;
 int Mark=0;
 if(!LoadArrayWithKeyInC(TheSign,NumKey,KeyOf3Lines))
     return -1;
 for(Attrib=0;Attrib<NObjects;Attrib++)
   Mark=BdokTchunaInC(TheSign,Attrib)+Mark;
   /* תכונות בשורה NObjects יש
    תכונות NObjects - 1 : אנו משתמשים רק ב */
 Mark = Mark /(NObjects+1);
  /* אם יש שארית גדולה עגל כלפי מעלה */
 if ((Mark % (NObjects+1))*2 > NObjects+1)
    Mark++ ;
 return Mark;
}

/* תן ציון לכל מפתחות האוביקט כלומר
תן את הציון הממוצע של המפתחות עבור אוביקט מסויים */
int CheckSignInC(int TheSign,int NumKeys)
{int Key;
 int Mark=0;
 if(NumKeys==0) return 0;
 for(Key=0;Key<NumKeys;Key++)
   Mark =CheckKeyInC(TheSign,Key)+Mark ;
 Mark=Mark/NumKeys;
   /* אם יש שארית גדולה עגל כלפי מעלה */
 if ((Mark % NumKeys)*2 > NumKeys)
    Mark++ ;
 return Mark;
}

/* הפונציה הראשית */
int PerformenceInC(HDC TheHdc,int X1,int Y1,
           int X2,int Y2,int Numb_Objects,
            LineBrightness FilterKind,int FilterNum)
{int MyLetter,NumKeys;
 int TheLetter= -1;
 int MyProb,TheProb=0; // בסופו של דבר חשב את הציון
 FILE *OutFp;
 //הוסף אחד שהאות לא תקוצץ
 int ImageW=X2-X1+1 ,ImageL=Y2-Y1+1;
 NObjects=Numb_Objects;
 GetCupAttsInL(Rects); /* טען מערך במרכיבי התכונות */
 EmptyFileInF(OutFile);

 // תמונה לדגימה
 HDC DeskHDC = CreateCompatibleDC(TheHdc);
 HBITMAP outHB = CreateCompatibleBitmap(
       TheHdc, PicW, PicL);
 SelectObject(DeskHDC, outHB);  /* קשר  */
// תמונה לשמירה
 HDC SaveHDC = CreateCompatibleDC(TheHdc);
 HBITMAP SaveHB = CreateCompatibleBitmap(
         TheHdc, ImageW, ImageL);
 SelectObject(SaveHDC, SaveHB);  /* קשר  */

 /* שמור את הציור כקובץ מפת סיביות */
 BitBlt( SaveHDC,0,0,ImageW ,ImageL,
             TheHdc ,X1,Y1,
             SRCCOPY);
 /* הפוך את התמונה */
 BitBlt(SaveHDC,0,0,
        X1,Y1,SaveHDC,X2,Y2,SRCCOPY);
 // שמור
 Win2HBFileInF(SaveHDC ,ImageW ,ImageL, SaveBMF);

 // מתח לגודל המותאם לדגימה
 StretchBlt( DeskHDC,0,0,PicW, PicL,
             TheHdc ,X1,Y1,ImageW ,ImageL,
             SRCCOPY); /* התאם לגודל יצוגי */
 SetStretchBltMode(DeskHDC,STRETCH_DELETESCANS);
 /* טען מערך בערכי דגימת התמונה */
 SelectPicInC(DeskHDC,float(0),float(0),
    float(PicL),float(PicW),TheImage,  // קודם גובה
    FilterKind,FilterNum);
  for(MyLetter=0;MyLetter<Numb_Objects;MyLetter++)
 {
  NumKeys=CkeckNumKeysInC(MyLetter);
  if(NumKeys == -1) return -1 ;/* בעיה*/
  if(NumKeys == 0) MyProb = 0 ;
  if(NumKeys>0) MyProb=CheckSignInC(MyLetter,NumKeys);
  if(MyProb>TheProb){TheProb=MyProb;TheLetter=MyLetter;}
 }
 /* כתוב את תוצאת הפיענוח בקובץ */
 OutFp=fopen(OutFile,"w");
  fprintf(OutFp,"%5d %5d",TheLetter,TheProb);
 fclose(OutFp);
 DeleteDC (DeskHDC);
 DeleteObject(outHB);
 DeleteDC (SaveHDC);
 DeleteObject(SaveHB);
 return TheLetter;
}