4

C++ (Subject) / bv4 (Lesson)

-

This lesson was created by Missunderstand.

Learn lesson

This lesson is not released for learning.

  • [4 P] Schreiben Sie eine C++-Funktion, die die Helligkeitsverteilung in einem Grauwert-Bild ermittelt (absolutes Histogramm). Erstellen Sie dazu eine Funktion namens histogram, die als Parameter eine Referenz auf ein Eingabebild und eine Referenz auf ein Array mit 256 long-Werten hat: void histogram (const SimpleGrayImage &img, long (&hist)[256]); Die Funktion soll die Häufigkeit aller Helligkeitswerte ermitteln und in das Array eintragen. Nach der dem Aufruf der Funktion soll das Histogramm in der main()-Funktion in Form von Zahlenpaaren (Grauwert, Anzahl) auf cout ausgegeben werden. void histogramm (const SimpleGrayImage &img, long (&tab)[256]){for(int k = 0; k < 256; k ++) tab[k] = 0;for(int i = 0; j < img.height(); i++)   for(int j = 0; j < img.width(); j++){       int val = img[i][j];       tab[val]++;    }}
  • [4 P] Schreiben Sie ferner eine Funktion void apply_lut(const SimpleGrayImage &in, SimpleGrayImage &out, int (&lut)[256]) der ein Originalbild, ein Ergebnisbild und die anzuwendende Look-Up-Tabelle übergeben wird. Erstellen Sie eine LUT, die ein Bild invertiert und wenden Sie sie auf ein Bild ihrer Wahl aus dem ressources-Verzeichnis an. Speichern Sie das Ergebnisbild. (Ergebnisbilder NICHT in das Repository einchecken!) void apply_lut(const SimpleGrayImage &in, SimpleGrayImage &out, const int(&lut)[256]){ if (in.width() != out.width() || in.height() != out.height()) throw "apply_lut: Ein- und Ausgabebild haben unterschiedliche Größe"; int corrlut[256];for(int k = 0; k < 256; k++)   corrlut[k] = clampToByte(lut[k]);for(int i = 0; i < out.height(); i++)   for(int j = 0; j < out.width(); j++)      out[i][j] = corrlut[in[i][j]];}
  • [4 P] Schreiben Sie eine Funktion void hist_spread_lut(const long (&hist)[256], int (&lut)[256]); die in dem übergebenen Histogramm den kleinsten und größten vorkommen- den Grauwert bestimmt und eine dazu passende LUT zur Histogrammspreizung erzeugt. void hist_spread_lut (const long(&hist)[256], int (&lut)[256]){int min = 0;int max = 255;while (hist [min] <= 0 && min < 255)   min++;while (hist[max] <=0 && max > 0)   max--;if(min > max) throw "fehlerhaftes, leeres Histogramm";cout << "MIN = " <<min  << ", MAX = "<< max <<endl;double step = 255.0/(max - min);for(int g = 0; g < min; g++)lut[g] = 0;for(int g = min; g < max; g++) lut[g] = (g - min) * step;for(int g = max; g < 256; g++) lut[g] = 255; } }
  • [8P] Erstellen Sie eine Funktion void sepFilter2 (const SimpleGrayImage &in, SimpleFloatImage &out, const std::vector<float> &maskH, const std::vector<float> &maskV) die zwei eindimensionale Filtermasken maskH (für die hori- zontale Filterung) und maskV (für vertikal) übergeben bekommt. maskH soll als als Zeilenmaske auf das Eingabebild in anwendet werden und anschließend maskV auf das Ergebnis als Spaltenmaske. Implementieren Sie die Randbehandlung durch gespiegelte Fortsetzung. inline int bh_mirror(int i, int max) {if(i < 0) return (-i -1);if (i >= max) return(max + max - i - 1);return i;} void sepFilter2 (const SimpleGrayImage &in, SIMPLEIMAGEOUT & out, const std::vector<float> &maskH, const std::vector <float> &maskV){if (maskH.size() % 2 != 1) throw std::runtime_error("sepFilter2: H-Maske muss eine ungerade größe haben"if (maskV.size() % 2 != 1) throw std::runtime_error("sepFilter2: V-Maske muss eine ungerade größe haben" const int hrad = maskH.size() / 2; // Fensterradius const int vrad = maskV.size() / 2; // Fensterradius const int W = in.width(); // für bessere Lesbarkeit const int H = in.height(); out.resize(W, H);SimpleGrayImage temp(W, H);// Erster durchlaug mir horizontaler Maskefor(int i = 0; i < H; i++){   for(int j = 0; j < W; j++){       double sum = 0;           for(int mj  = -hrad; mj <= hrad; mj++){              int rj = bh_mirror(j+mj, W);              sum += int[i][rj] * maskH[mj+hrad]} temp[i][j] = sum;}} // 2. Durchlauf mit for(inti=0;i<vertikaler Maske H; i++) { <W;j++){ for(int i = 0; i < H; i++){   for(int j = 0; j < W; j++){       double sum = 0;   for(int mi = -vrand; mi <= vrand; mi++)        int ri = bh_mirror(i + mi, H);        sum += temp[ri][j] * maskV[mi + vrand];       }      out[i][j] = sum;    }  }}
Advertisement