diff --git a/cs677/pa3/edge-detect.cc b/cs677/pa3/edge-detect.cc index 98c7706..b8cb494 100644 --- a/cs677/pa3/edge-detect.cc +++ b/cs677/pa3/edge-detect.cc @@ -8,12 +8,14 @@ using namespace std; void usage(const char * progName); void squashAndPadData(unsigned char * raw_data, unsigned char * padded_data, int width, int height); +void applyEdgeDetection(unsigned char * padded_data, unsigned char * edge_data, + int width, int height, int threshold); void usage(const char * progName) { cout << "Usage: " << progName << " [options] " << endl; cout << " Options:" << endl; - cout << " -l threshold_level : set threshold level" << endl; + cout << " -l level : apply threshold at level" << endl; exit(42); } @@ -22,7 +24,7 @@ int main(int argc, char * argv[]) int argi; string inputFileName; string outputFileName; - unsigned int threshold_level = 60; + int threshold_level = -1; for (argi = 0; argi < argc; argi++) { @@ -57,7 +59,15 @@ int main(int argc, char * argv[]) squashAndPadData(bmp_data, padded_data, width, height); delete[] bmp_data; + unsigned char * edge_data = new unsigned char[width * height * 3]; + applyEdgeDetection(padded_data, edge_data, width, height, threshold_level); + BMP outputImage(outputFileName.c_str(), + width, + height, + edge_data); + delete[] padded_data; + delete[] edge_data; } void squashAndPadData(unsigned char * raw_data, unsigned char * padded_data, @@ -79,3 +89,33 @@ void squashAndPadData(unsigned char * raw_data, unsigned char * padded_data, *padded_data++ = 0; } +void applyEdgeDetection(unsigned char * padded_data, unsigned char * edge_data, + int width, int height, int threshold) +{ + unsigned char (*in)[height+2][width+2] = + (unsigned char (*)[height+2][width+2]) padded_data; + unsigned char (*out)[height][width][3] = + (unsigned char (*)[height][width][3]) edge_data; + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + /* Apply the Sobel operator on the in to + * produce an out value. */ + int gradY = (*in)[y][x] + 2 * (*in)[y][x+1] + (*in)[y][x+2] + - (*in)[y+2][x] - 2 * (*in)[y+2][x+1] - (*in)[y+2][x+2]; + int gradX = (*in)[y][x+2] + 2 * (*in)[y+1][x+2] + (*in)[y+2][x+2] + - (*in)[y][x] - 2 * (*in)[y+1][x] - (*in)[y+2][x]; + int grad = gradX + gradY; + if (grad < 0) + grad = 0; + else if (grad > 255) + grad = 255; + if (threshold >= 0) + grad = grad < threshold ? 0 : 255; + (*out)[y][x][0] = grad; + (*out)[y][x][1] = grad; + (*out)[y][x][2] = grad; + } + } +}