int main () { FILE *fpi, *fpo; int size; double area; double x[20]; double y[20]; fpi = fopen ("data.txt", "r"); fpo = fopen ("data2.txt", "a+"); size = get_corners (fpi, x, y, MAX_SIZE); output_corners (fpo, x, y, size); area = polygon_area (x, y, size); printf ("The area is %.1lf\n", area); return 0; }
bool quad_manipulator_t::do_mouse_press_event( const ui::mouse_press_event_t& event) { picked_corner_ = -1; boost::array<Imath::V2f, 4> dst_pts; get_corners( dst_pts, event.aspect_ratio); for( int i = 0; i < 4; ++i) { if( manipulators::inside_pick_distance( event.wpos, dst_pts[i], event.pixel_scale)) { picked_corner_ = i; break; } } event.view->update(); return picked_corner_ != -1; }
void quad_manipulator_t::do_draw_overlay( const ui::paint_event_t& event) const { boost::array<Imath::V2f, 4> dst_pts; get_corners( dst_pts, event.aspect_ratio); gl_line_width( default_line_width()); gl_point_size( default_control_point_size()); // shadow float off = manipulators::shadow_offset( event.pixel_scale); gl_color3ub( 0, 0, 0); gl_begin( GL_LINE_LOOP); for( int i = 0; i < 4; ++i) gl_vertex2f( dst_pts[i].x + off, dst_pts[i].y + off); gl_end(); gl_begin( GL_POINTS); for( int i = 0; i < 4; ++i) gl_vertex2f( dst_pts[i].x + off, dst_pts[i].y + off); gl_end(); // color gl_color( default_color()); gl_begin( GL_LINE_LOOP); for( int i = 0; i < 4; ++i) gl_vertex2f( dst_pts[i].x, dst_pts[i].y); gl_end(); // draw corners gl_begin( GL_POINTS); for( int i = 0; i < 4; ++i) { if( i != picked_corner_) gl_vertex2f( dst_pts[i].x, dst_pts[i].y); } gl_end(); for( int i = 0; i < 4; ++i) { if( i == picked_corner_) manipulators::draw_small_box( dst_pts[i], 3 / event.pixel_scale); } }
static void get_surrounding_frame(int frame[3], SPGCONST int t_mat[3][3]) { int i, j, max, min; int corners[3][8]; get_corners(corners, t_mat); for (i = 0; i < 3; i++) { max = corners[i][0]; min = corners[i][0]; for (j = 1; j < 8; j++) { if (max < corners[i][j]) { max = corners[i][j]; } if (min > corners[i][j]) { min = corners[i][j]; } } frame[i] = max - min; } }
static void determine_extents(char **infiles, int n_inputs, int *size_x, int *size_y, int *n_bands, double *start_x, double *start_y, double *per_x, double *per_y, int extent) { // the first input file is the "reference" -- all other metadata // must match the first (at least as far as projection, etc) meta_parameters *meta0 = meta_read(infiles[0]); if (!meta0) { asfPrintError("Couldn't read metadata for %s!\n", infiles[0]); } if (!meta0->projection) { asfPrintError("%s is not geocoded!\n", infiles[0]); } asfPrintStatus("Reference image is: %s\nGeocoding:\n", infiles[0]); print_proj_info(meta0); // these values must be matched by all images double px, py; px = *per_x = meta0->projection->perX; py = *per_y = meta0->projection->perY; // these don't have to be matched, we will update as we go along double x0, y0, xL, yL; get_corners(meta0, &x0, &y0, &xL, &yL); projection_type_t proj_type = meta0->projection->type; int nBands; nBands = *n_bands = meta0->general->band_count; int i, n_ok = 1, n_bad = 0; for (i=1; i<n_inputs; ++i) { char *file = infiles[i]; //asfPrintStatus(" Processing metadata for %s...\n", file); meta_parameters *meta = meta_read(file); char *why=""; if (!meta) why = "Couldn't read metadata"; else if (!meta->projection) why = "Image is not geocoded"; else if (meta->projection->perX != px) why = "X pixel size doesn't match reference image"; else if (meta->projection->perY != py) why = "Y pixel size doesn't match reference image"; else if (meta->projection->type != proj_type) why = "Image is in a different projection"; else if (!proj_parms_match(meta0, meta)) why = "Projection parameters differ"; else if (meta->general->band_count != nBands) why = "Number of bands differ"; if (strlen(why) > 0) { ++n_bad; asfPrintStatus("Image '%s': NOT OK (%s)\n", file, why); infiles[i] = NULL; // mark for future ignore-ation } else { ++n_ok; asfPrintStatus("Image '%s': ok (%dx%d LxS)\n", file, meta->general->line_count, meta->general->sample_count); double this_x0, this_y0, this_xL, this_yL; get_corners(meta, &this_x0, &this_y0, &this_xL, &this_yL); if (!extent) update_corners(px, py, &x0, &y0, &xL, &yL, this_x0, this_y0, this_xL, this_yL); } meta_free(meta); } if (n_ok < 2) { asfPrintError("Not enough images to mosaic.\n"); } *start_x = x0; *start_y = y0; // calculate number of lines/samples from corner to corner *size_x = (int) ((xL-x0)/px + .5); *size_y = (int) ((yL-y0)/py + .5); meta_free(meta0); }
int main() { cv::Mat src = cv::imread("/Users/Qt/program/blogsCodes/pic/perspective08.jpg"); if (src.empty()){ std::cerr<<"can't open image"<<std::endl; return - 1; } //step 1 : convert the image to binary by otsu threshold cv::Mat binary; cv::cvtColor(src, binary, CV_BGR2GRAY); cv::threshold(binary, binary, 0, 255, cv::THRESH_BINARY + cv::THRESH_OTSU); #ifdef DRAW_OUTPUT cv::imshow("otsu", binary); cv::imwrite("otsu.jpg", binary); #endif //step 2 : find the contours of binary image auto contours = find_contours(binary); #ifdef DRAW_OUTPUT cv::Mat contour_map = cv::Mat::zeros(binary.size(), CV_8U); cv::drawContours(contour_map, contours, -1, cv::Scalar(255)); cv::imshow("contour map", contour_map); cv::imwrite("contour_map.jpg", contour_map); #endif //step 3 : find the corners of the quadrilaterals auto const corners = get_corners(contours); #ifdef DRAW_OUTPUT cv::Mat src_corners = src.clone(); cv::Scalar color[] = { {255, 0, 0}, {0, 255, 0}, {0, 0, 255} }; for(size_t i = 0; i != corners.size(); ++i){ for(auto const &point : corners[i]){ cv::circle(src_corners, point, 10, color[i % 3]); } } cv::imshow("src corners", src_corners); cv::imwrite("corners.jpg", src_corners); #endif //step 4 : correct the perspective distortion of the quadrilateral by warpPerspective cv::Mat target(150, 150, src.type()); std::vector<cv::Point2f> target_points { {0, 0}, {target.cols - 1, 0}, {target.cols - 1, target.rows - 1}, {0, target.rows - 1} }; std::vector<cv::Point2f> points; for(size_t i = 0; i != corners.size(); ++i){ OCV::convert_points(corners[i], points); cv::Mat const trans_mat =cv::getPerspectiveTransform(points, target_points); cv::warpPerspective(src, target, trans_mat, target.size()); #ifdef DRAW_OUTPUT std::stringstream name; name << "target" << std::setw(2) << std::setfill('0') << i <<".jpg"; cv::imshow("target", target); cv::imwrite(name.str(), target); cv::waitKey(); #endif } #ifdef DRAW_OUTPUT cv::waitKey(); #endif return 0; }