Änderungen am TBitmap



  • Ich lade ein Bitmap und will daran Änderungen vornehmen:
    Der erste Teil läd und zeigt das Bild:

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    Graphics::TBitmap *Bitmap = new Graphics::TBitmap();
    OpenPictureDialog1->Filter="Bitmap Files (*.bmp)|*.bmp";
            if (OpenPictureDialog1->Execute() )
              Graphics::TBitmap *Bitmap = new Graphics::TBitmap();
      try
      {
        Bitmap->LoadFromFile(OpenPictureDialog1->FileName );
        Canvas->Draw(150,10,Bitmap);
          }
      catch (...)
      {
        MessageBeep(0);
      }
      //delete Bitmap;
      //delete Bitmap2;
    
    }
    

    Der zweite Teil des Programm soll es es in Schwarz Weis umwandeln aber das haut noch nicht hin:

    void binarize(Graphics::TBitmap *bitmap)
    {
    
    int THRESHOLD,WHITE,BLACK;
    THRESHOLD=130;
    WHITE=1;
    BLACK=200;                                              // Binaraize every row in the image
      for (int row = 0; row < bitmap->Height ; row++) {
        RGBQUAD * pixel = (RGBQUAD *) bitmap->ScanLine [row];
                                                  // Binarize every pixel in each row
        for (int col = 0; col < bitmap->Width; col++, pixel++) {
    
          if ((int)pixel->rgbRed > THRESHOLD && (int)pixel->rgbGreen > THRESHOLD &&
                (int)pixel->rgbBlue > THRESHOLD){ // If ablove the threshold
            pixel->rgbRed = WHITE;                // Set the pixel to be white
            pixel->rgbGreen = WHITE;
            pixel->rgbBlue = WHITE;
          }
          else{                                   // Otherwise the pixel is set to
            pixel->rgbRed = BLACK;                // black
            pixel->rgbGreen = BLACK;
            pixel->rgbBlue = BLACK;
          }
        }
      }
    
    }
    

    Was muss ich am zweiten Teil ändern??



  • du kannst kein Farbbild in ein Binärbild umwandeln. Mach dazu nen Umweg über ein Graubild und berechne den Grauwert eines jeden Pixels, z.b nach Intensität G = (R + G + 😎 / 3. Kannst natürlich auch eine gewichtete Berechnung (entspricht mehr dem menschlichen Sehen) machen, z.b: G = ((77 * R) + (151 * G) + (28 * B)) >> 8;



  • Und wie soll das Codemaessig ungefaehr aussehen?
    Ich bin mir naemlich nicht sicher ob ich das TBitmap richtig anspreche. Warum kann ich das Bildeigentlich nicht direkt in ein SchwarzWeis Bild umwandeln ? Ich frage jeden Bildpunkt ab und wenn er hoher als den Threshold Wert ist setzt ich den Pixel auf schwarz und ansonsten auf weiss?



  • hab doch geschrieben wie das aussehen muss! der grauwert ist der wert, den du gegen den schwellwert testen musst.

    if (GrayValue > Threshold)
    {
      R = 255; // weiß
      G = 255; // weiß
      B = 255; // weiß
    }
    else
    {
      R = 0; // schwarz
      G = 0; // schwarz
      B = 0; // schwarz
    }
    


  • Das war mir schonklar ich meinte eigentlich wie du das mit dem Grauwert machst?



  • typedef unsigned char u_char;
    
    for (int row = 0; row < bitmap->Height ; row++) 
    { 
      // RGBQUAD geht natürlich nur bei 32Bit Bildern!
      RGBQUAD *pixel = (RGBQUAD*) bitmap->ScanLine[row];
    
      // Binarize every pixel in each row
      for (int col = 0; col < bitmap->Width; col++, pixel++) 
      {
        u_char Gray = ((77 * pixel->rgbRed) + (151 * pixel->rgbGreen) + (28 * pixel->rgbBlue)) >> 8;
    
        // If ablove the threshold
        if (Gray > THRESHOLD)
        { 
            pixel->rgbRed   = 255;                // Set the pixel to be white
            pixel->rgbGreen = 255;
            pixel->rgbBlue  = 255;
          }
          else{                                   // Otherwise the pixel is set to
            pixel->rgbRed   = 0;                // black
            pixel->rgbGreen = 0;
            pixel->rgbBlue  = 0;
          }
        }
      }
    

Anmelden zum Antworten