From: <bl...@us...> - 2009-08-06 15:00:20
|
Revision: 4177 http://hugin.svn.sourceforge.net/hugin/?rev=4177&view=rev Author: blimbo Date: 2009-08-06 15:00:12 +0000 (Thu, 06 Aug 2009) Log Message: ----------- Added ShortStraw Modified Paths: -------------- hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.cpp hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.h hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Main.cpp hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/ProcessImage.cpp hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Straighten.cpp Modified: hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.cpp =================================================================== --- hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.cpp 2009-08-06 14:48:02 UTC (rev 4176) +++ hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.cpp 2009-08-06 15:00:12 UTC (rev 4177) @@ -30,6 +30,7 @@ unsigned int current_line = 0; unsigned int optimise_centre = 1; unsigned int poly_type = 1; +unsigned int ss = 0; unsigned int straighten_verts = 0; double sensor_width = 0; double sensor_height = 0; Modified: hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.h =================================================================== --- hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.h 2009-08-06 14:48:02 UTC (rev 4176) +++ hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Globals.h 2009-08-06 15:00:12 UTC (rev 4177) @@ -14,6 +14,7 @@ extern unsigned int optimise_centre; extern unsigned int poly_type; extern unsigned int straighten_verts; +extern unsigned int ss; extern double sensor_width; extern double sensor_height; extern double original_width; Modified: hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Main.cpp =================================================================== --- hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Main.cpp 2009-08-06 14:48:02 UTC (rev 4176) +++ hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Main.cpp 2009-08-06 15:00:12 UTC (rev 4177) @@ -424,7 +424,8 @@ cout << " -e <int> Edge detector. 1 = Canny (default)" << endl; cout << " 2 = Shen-Castan" << endl; cout << " 3 = Boundary tensor" << endl; - cout << " -d <int> Maximum dimension for re-sized image prior to processing. Default 1600." << endl; + cout << " -a <int> Use ShortStraw algorithm instead of boundary tensor. Default 0" << endl; + cout << " -d <int> Maximum dimension for re-sized image prior to processing. Default 1600" << endl; //cout << " -f <string> Output file format. Default .JPG" << endl; cout << " -o <path> Output path. Default output/" << endl; //cout << " -s <float> Edge detector scale. Default 2" << endl; @@ -437,7 +438,7 @@ cout << " -y <float> Corner threshold. Default 150" << endl; //cout << " -i <float> Pixel density in pixels/inch. Default 4000" << endl; cout << " -f <float> Focal length (mm). Read from EXIF data." << endl; - cout << " -c <int> Crop factor. Read/calculated from EXIF data." << endl; + cout << " -c <float Crop factor. Read/calculated from EXIF data." << endl; cout << " -s <float> Shortest dimension of sensor (mm), used to calculate crop factor." << endl; cout << " -l <int> Lens model 1 = Rectilinear (default)" << endl; cout << " 2 = Equal-area fisheye" << endl; @@ -503,7 +504,8 @@ //case 's' : {scale = atof(argv[i]); break;} case 's' : {cropFactor = 24.0/atof(argv[i]); break;} //case 'b' : {tscale = atof(argv[i]); break;} - case 'a' : {a = atof(argv[i]); break;} + //case 'a' : {a = atof(argv[i]); break;} + case 'a' : {ss = atoi(argv[i]); break;} case 't' : {threshold = atof(argv[i]); break;} case 'b' : {bt_threshold = atof(argv[i]); break;} case 'y' : {corner_threshold = atof(argv[i]); break;} @@ -580,10 +582,7 @@ cout << "Found "<< lines.size() <<" lines."<< endl << endl; int n_params = 2; if (lens_type == 2) ++n_params; if (optimise_centre) n_params += 2; - /* for (int i = 0; i < lines.size(); i++){ - for (int j = 0; j < lines[i].size(); j++){ - cout << "_" << i << "," << lines[i][j]->x << "," << lines[i][j]->y << endl; } } cout << endl; */ - + if(!straighten_verts){ if (lines.size() >= n_params ){ @@ -610,8 +609,7 @@ write_pto(output_pto,pto_file_top,pto_file_cps,pto_file_end,pto_image,cps_per_line,lines_to_image_number); cout << endl << "Written file " << output_pto << endl; } - - cout << endl; + //cout << endl; return(1); } Modified: hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/ProcessImage.cpp =================================================================== --- hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/ProcessImage.cpp 2009-08-06 14:48:02 UTC (rev 4176) +++ hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/ProcessImage.cpp 2009-08-06 15:00:12 UTC (rev 4177) @@ -1081,11 +1081,10 @@ } double point_line_length_squared(Point2D& a, Point2D& b){ + //cout << a->x << " - " << b->x << " = " << a->x - b->x << endl; + //cout << a->y<< " - " << b->y <<" = " << a->y - b->y << endl; int d1 = a->x - b->x; int d2 = a->y - b->y; - //cout << a->x << " - " << b->x << " = " << d1 << endl; - //cout << a->y<< " - " << b->y <<" = " << d2 << endl; - //cout << "Dist^2:\t" << (d1*d1+d2*d2) << endl << endl; return(d1*d1+d2*d2); } @@ -1351,6 +1350,236 @@ } } + +double path_distance(vector<Point2D>& coords, int& a, int& b){ + double d = 0; + for (unsigned int i = a; i < b; i++){ + //cout <<"From path_dist - point_line_length_squared " << i << "-" << i+1 << endl; + //if(a < coords.size() && b < coords.size()){ + + d += point_line_length_squared(coords[i],coords[i+1]); + //}else{ + // cout << a << "-" << b << " path_dist, size = " << coords.size() << " " << i << " " << i+1<< " : " << coords[i]->x << "-" << coords[i]->y << "--->" << coords[i+1]->x << "," << coords[i+1]->y << endl; + // d += point_line_length_squared(coords[i],coords[i+1]); + //} + } + return(d); +} + + +bool is_line(vector<Point2D>& coords, int& a, int& b){ + double threshold = 0.95; + //cout <<"From is_line - point_line_length_squared " << a << "-" << b << endl; + double distance = sqrt(point_line_length_squared(coords[a],coords[b])); + double path_dist = path_distance(coords,a,b); + if(distance/path_dist > threshold){ + return(true); + }else{ + return(false); + } +} + +int halfway_corners(vector<double>& straws, int& a, int& b){ + int quarter = (b-a)/4; + int MinIndex; + double MinValue = 100000000; + for (unsigned int i = a+quarter; i < b-quarter; i++){ + if(straws[i] < MinValue){ + MinValue = straws[i]; + MinIndex = i; + } + } + return(MinIndex); +} + +double sample_spacing(vector<Point2D>& coords){ + int x_max = max_x_index(coords); + int x_min = min_x_index(coords); + int y_max = max_y_index(coords); + int y_min = min_y_index(coords); + Point2D top_left(coords[x_min]->x,coords[y_min]->y); + Point2D bottom_right(coords[x_max]->x,coords[y_max]->y); + return(sqrt(point_line_length_squared(top_left,bottom_right)/40.0)); +} + +void resample(vector<Point2D>& coords, vector<Point2D>& resampled, double& s){ + double D = 0; + resampled.push_back(coords[0]); + for (unsigned int i = 1; i < coords.size(); i++){ + double d = sqrt(point_line_length_squared(coords[i-1],coords[i])); + if (d+D >= s){ + Point2D q; + // Need to cast to int + q.x = (int)(coords[i-1]->x + ((s-D)/d) * (coords[i]->x - coords[i-1]->x)); + q.y = (int)(coords[i-1]->y + ((s-D)/d) * (coords[i]->y - coords[i-1]->y)); + resampled.push_back(q); + vector<Point2D>::iterator it = coords.begin();; + coords.insert(it+i,q); + D = 0; + }else{ + D += d; + } + } +} + +void short_straw(BImage& image, vector<Point2D>& coords, string& filename){ + + vector<Point2D> coords_original = coords;; + vector<Point2D> points; + + // Resample points? + if(0){ + cout << "Resampling points.." << endl; + double s = sample_spacing(coords); + vector<Point2D> resampled; + resample(coords,resampled,s); + points = resampled; + }else{ + points = coords; + } + + vector<int> corners,processed_corners; + corners.push_back(0); + int w = 3; + vector<double> straws, sorted_straws; + + for (unsigned int i = w; i < points.size() - w; i++){ + straws.push_back(sqrt(point_line_length_squared(points[i-w],points[i+w]))); + } + sorted_straws = straws; + sort (sorted_straws.begin(),sorted_straws.end()); + + double median_t; + int middle_one,middle_two; + + if ((sorted_straws.size() % 2) == 0){ + middle_one = (sorted_straws.size()/2); + middle_two = (sorted_straws.size()/2)+1; + median_t = (sorted_straws[middle_one-1] + sorted_straws[middle_two-1])/2; + }else{ + middle_one = (sorted_straws.size()/2)+1; + median_t = sorted_straws[middle_one-1]; + } + median_t *= 0.95; + + double localMin; + int localMinIndex; + // Get corners + for (unsigned int i = w; i < points.size() - w; i++){ + if(straws[i] < median_t){ + localMin = 100000000; + localMinIndex = i; + while(i < straws.size() && straws[i] < median_t){ + if(straws[i] < localMin){ + localMin = straws[i]; + localMinIndex = i; + } + i++; + } + corners.push_back(localMinIndex); + } + } + //corners.push_back(points.size()); + + if(0){ + + // Post-process corners + cout << "Post-processing corners..." << endl; + bool cont = false; + while(!cont){ + cont = true; + for (unsigned int i = 1; i < corners.size(); i++){ + cout << i << "/" << corners.size() << endl; + int c1 = corners[i-1]; + int c2 = corners[i]; + if(!is_line(points,c1,c2)){ + int newcorner = halfway_corners(straws,c1,c2); + vector<int>::iterator it = corners.begin(); + corners.insert(it+i,newcorner); + cont = false; + } + } + } + cout << " Retaining good corners..." << endl; + for (unsigned int i = 1; i <corners.size()-1; i++){ + int c1 = corners[i-1]; + int c2 = corners[i+1]; + if(!is_line(points,c1,c2)){ + processed_corners.push_back(i); + } + } + corners = processed_corners; + } + + // Write files/remove corners + BImage ss_corners(image.width(), image.height()); + ss_corners = 255; + + for (unsigned int i = w; i < corners.size() - w; i++){ + + int w = coords_original[corners[i]]->x; + int h = coords_original[corners[i]]->y; + + // Change the size of the box to draw around corner pixel + int size = 3; + int left = w-size; + int top = h-size; + int right = w+size; + int bottom = h+size; + + if(left<0) left = 0; + if(right>image.width()) right = image.width(); + if(top<0) top = 0; + if(bottom > image.height()) bottom = image.height(); + + // Draw a white box around the corner pixel + // in the edge image + vigra::initImage(srcIterRange(ss_corners.upperLeft() + + vigra::Diff2D(left, top), + ss_corners.upperLeft() + vigra::Diff2D(right, bottom)), + 0 ); + + vigra::initImage(srcIterRange(image.upperLeft() + + vigra::Diff2D(left, top), + image.upperLeft() + vigra::Diff2D(right, bottom)), + 255 ); + + } + + if (generate_images){ + + string output = path; + vector<string> tokens; + + // *nix file paths + if (filename.find("/") < filename.length() || filename.substr(0, 1) == ("/")){ + tokenize(filename, tokens, "/"); + output.append(tokens[tokens.size()-1].substr(0,tokens[tokens.size()-1].length()-4)); + // Windows file paths + }else{ + tokenize(filename, tokens, "\\"); + output.append(tokens[tokens.size()-1].substr(0,tokens[tokens.size()-1].length()-4)); + } + string output_minus_ss_corners = output; + output.append("_short_straw_corners"); + output.append(format); + + //if(!fileexists(output)){ + cout << "Writing " << output << endl; + exportImage(srcImageRange(ss_corners), ImageExportInfo(output.c_str()).setPixelType("UINT8")); + //} + + + output_minus_ss_corners.append("_minus_short_straw_corners"); + output_minus_ss_corners.append(format); + + //if(!fileexists(output)){ + cout << "Writing " << output_minus_ss_corners << endl << endl; + exportImage(srcImageRange(image), ImageExportInfo(output_minus_ss_corners.c_str()).setPixelType("UINT8")); + //} + } +} + void process_image(string& filename, int& plotted){ ImageImportInfo info(filename.c_str()); @@ -1405,15 +1634,22 @@ image = grey; } - // Generate boundary tensor - generate_boundary_tensor(image, edgeness, cornerness, filename); + // Run short straw corner finding algorithm + if(ss){ + cout << "Running ShortStraw corner finder..." << endl; + extract_coords(image, coords); + short_straw(image, coords, filename); + coords.clear(); + }else{ + // Generate boundary tensor + generate_boundary_tensor(image, edgeness, cornerness, filename); + // Remove corners + nuke_corners(image, cornerness, filename); + } - // Remove corners - nuke_corners(image, cornerness, filename); - // Get coordinates extract_coords(image, coords); - + unsigned int intital_coords_size = coords.size(); if(verbose) cout << "Initial 'edge' pixel count:\t" << coords.size() << endl << endl; Modified: hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Straighten.cpp =================================================================== --- hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Straighten.cpp 2009-08-06 14:48:02 UTC (rev 4176) +++ hugin/branches/gsoc2009_lenscalibration/src/lens_calibrate/Straighten.cpp 2009-08-06 15:00:12 UTC (rev 4177) @@ -28,7 +28,7 @@ double invert_size_factor = 1.0/sizefactor; double best_angle = 0, best_score = 10000000; - map<double,double> best_rotation; + //map<double,double> best_rotation; cout << "Finding optimal rotation angle..." << endl; for(double angle = -90; angle < 90; angle += 0.25){ @@ -77,17 +77,18 @@ } //cout << angle << "\tTotal:\t" << total_x_diff << endl; - - best_rotation[angle] = total_x_diff; + + if(total_x_diff < best_score){ + best_score = total_x_diff; + best_angle = angle; + } + //best_rotation[angle] = total_x_diff; total_x_diff = 0; } - for(double angle = -90; angle < 90; angle += 0.25){ - //cout << angle << "\tdegrees - Score:\t" << best_rotation[angle] << endl; - if(best_rotation[angle] < best_score){ - best_score = best_rotation[angle]; - best_angle = angle; - } - } - cout << endl; + //for(double angle = -90; angle < 90; angle += 0.25){ + // cout << angle << "\tdegrees - Score:\t" << best_rotation[angle] << endl; + + //} + //cout << endl; return(-best_angle); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |