// // Programa de binarização de imagems pgm, que recebe o nome // dos ficheiros pela linha de comando // int main(int argc, char **argv){ int height, width, i, j, datasize; unsigned char **imptr, *dp, limiar; printf("Lendo imagem '%s'...\n", argv[1]); imptr=readpgm(argv[1],&width,&height); printf("\tOk.\n\n"); datasize=width*height; if(!(dp=(unsigned char *)calloc(sizeof(char),datasize))){ fprintf(stderr,"writepgm: calloc error\n"); exit(0); } for (i=0;i<height;i++) for (j=0;j<width;j++) dp[i*width+j]=imptr[i][j]; #define LIMIAR 128 if (argc<4) { limiar = LIMIAR; printf("Nao foi indicado na linha de comandos o limiar a utilizar.\n"); printf("\tSera usado o valor por defeito %u.\n", limiar); } else limiar = (unsigned char) atoi(argv[3]); printf("Executando a funcao 'bin_img()' com o limiar %u...\n", limiar); bin_img(dp, width, height, limiar); printf("\tFim.\n\n"); for(i=0;i<height;i++) for(j=0;j<width;j++) imptr[i][j]=dp[i*width+j]; printf("Guardando o resultado no ficheiro '%s'...\n", argv[2]); writepgm(imptr,width,height,argv[2]); printf("\tOk.\n\n"); }
int main(int argc, char* argv[]) { if (argc < 2) { std::cout << "USAGE: program [path_to_image] (width) (height)" << std::endl; std::cout << " optional optional" << std::endl; return 0; } cv::Mat img = cv::imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); if (!img.data){ std::cout << "[ERROR]: loading image" << std::endl; return 0; } int rows = 0; int cols = 0; if (argc > 2) { rows = std::stoi(argv[2]); if (argc > 3) { cols = std::stoi(argv[3]); } else { cols = rows; } } else { rows = cols = 50; } /// jam::StopWatch sw; /// sw.start(); // let's get to it img.convertTo(img, CV_32F); cv::resize(img, img, cv::Size(rows, cols), 0, 0, CV_INTER_AREA); // get image's edges cv::Mat edge_angles, edge_strength; calcEdges(img, edge_angles, edge_strength); // remove noisy edges // find a mask of non-noisy edges const float thresh = optimalThresh(edge_strength) * 2; cv::Mat bin_img(rows, cols, CV_8U); cv::threshold(edge_strength, bin_img, thresh, 1, CV_THRESH_BINARY); bin_img.convertTo(bin_img, CV_8U); // set the non-noisy edges, and mark the noisy ones cv::Mat result_angles(rows, cols, CV_32F); result_angles = NO_EDGE; edge_angles.copyTo(result_angles, bin_img); // generate a string out of edges std::string result = img2ascii(result_angles); // print the string puts(result.c_str()); /// sw.measure(); /// std::cout << sw.asMilliseconds() << std::endl; /* cv::namedWindow("debug", CV_WINDOW_NORMAL); show(img); show(edge_strength); show((result_angles + M_PI / 2) * (255 / M_PI)); */ return 0; }