3
C++ (Subject) / bv6 (Lesson)
- Implementieren Sie die Filter für die Erosion mit einem gegebenen Strukturelement. Implementieren Sie den Filter jeweils als eigene Funktion der Form void filtername (const SimpleGrayImage &inImg, const SimpleGrayImage &inSE, SimpleGrayImage &outImg). #include <iostream>#include <cmath>#include <vector>#include "SimpleImage/SimpleGrayImage.h"using namespace std; //Gemäß der Aufgabenstellung sind alle Pixel zwischen 0 und 255void erosion (const SimpleGrayImage &in, const SimpleGrayImage &inSE, SimpleGrayImage &out){if (inSE.width() % 2 != 1 || inSE.height() % 2 != 1) throw "SE muss ungerade Groesse haben, sonst ist Referenzpunkt nicht eindeutig";out.resize(in.width(), in.height());out.clear(0);int mxrad = (inSE.width() -1) / 2;int myrad = (inSE.height() - 1 / 2;for(int i = myrad; i < out.height - myrad; i++){ for(int j = mxrad; j < out.width - mxrad; j++){ int val = 255; for(int m = -myrad; m <= myrad; m++) for(int n = -mxrad; n < mxrad; n++) if(SE[m + myrad][n + mxrad] val = val && in [i+m][j + n]; out [i][j] = (val) ? 255 : 0; } }}
- Implementieren Sie Filter für die (binäre) Dilatation mit einem gegebenen Strukturelement. Implementieren Sie den Filter jeweils als eigene Funktion der Form void filtername (const SimpleGrayImage &inImg, const SimpleGrayImage &inSE , SimpleGrayImage &outImg) void dilatation (const SimpleImage &in, const SimpleGrayImage &inSE , SimpleGrayImage &out){ if (inSE.width() % 2 != 1 || inSE.height() % 2 != 1) throw "SE muss ungerade Groesse haben, sonst ist Referenzpunkt nicht eindeutig"; out.resize(in.width(), in.height());out.clear();int mxrad = inSE.width() -1 / 2;in myrad = inSE.height() -1/ 2; for(int i = myrad; i < out.height() - myrad; i++){ for(int j = mxrad; j < out.width() - mxrad; j++){ int val = 0; for(int m = -myrad; m <= myrad; m++) for (int n = -mxrad; n <= mxrad; n ++) if(inSE[m+myrad][n+myrad]) val = val || in[i+m][j+n]; if(val){ out [i][j] = 255;} else {out[i][j] = 0;} } }}
- [4P] Bestimmen Sie den morphologischen Gradienten des (neu hochgeladenen) Bildes sample_sudoku.pgm. #include <iostream>#include <cmath>#include <vector>#include "SimpleImage/SimpleGrayImage.h"using namespace std;void morphGrad(const SimpleGrayImage &in, const SimpleGrayImage &inSE, const SimpleGrayImage &out){const int W = in.width();const int H = in.height();SimpleGrayImage imfI, imfN; erosion(in, inSE, out);dilatation(in, inSE, out);out.resize(W, H);for(int i = 0; i < H; i ++) for(int j = 0; j < W; j++) out[i][j] = clampToByte(imfN[i][j] - imfI[i][j];}