double compute_area_for_x(const QVector<XTrapezoid> &traps) { double area = 0; for (int i = 0; i < traps.size(); ++i) { XTrapezoid trap = traps[i]; area += compute_area(&trap); } return area; }
// Fill the MatchData array for lossless compression static void compute_matchdata_lossless(JB2Image *jimg, MatchData *lib) { int i; int nshapes = jimg->get_shape_count(); for (i=0; i<nshapes; i++) { JB2Shape &jshp = jimg->get_shape(i); lib[i].bits = 0; lib[i].area = 0; lib[i].match = -1; if (! jshp.bits) continue; if (jshp.userdata & JB2SHAPE_SPECIAL) continue; lib[i].bits = jshp.bits; lib[i].area = compute_area(jshp.bits); } }
void Bubble:: raw_initialize() { Tracer *p = root; for( size_t i=size;i>0;--i,p=p->next ) { pbc(p->vertex); const Tracer *q = p->next; assert(q!=NULL); Vertex pq(p->vertex,q->vertex); pbc(pq); p->edge = pq; p->s2 = p->edge.norm2(); p->s = Sqrt( p->s2 ); } compute_area(); content = pressure * area; compute_geometry(); }
// Compute MatchData array for lossy compression. static void compute_matchdata_lossy(JB2Image *jimg, MatchData *lib, int dpi, mdjvu_matcher_options_t options) { int i; int nshapes = jimg->get_shape_count(); // Prepare MatchData GTArray<mdjvu_pattern_t> handles(nshapes); for (i=0; i<nshapes; i++) { JB2Shape &jshp = jimg->get_shape(i); lib[i].bits = 0; lib[i].area = 0; lib[i].match = -1; handles[i] = 0; if (! jshp.bits) continue; if (jshp.userdata & JB2SHAPE_SPECIAL) continue; lib[i].bits = jshp.bits; lib[i].area = compute_area(jshp.bits); handles[i] = compute_comparable_image(jshp.bits); } // Run Ilya's pattern matcher. GTArray<int> tags(nshapes); int maxtag = mdjvu_classify_patterns(handles, tags, nshapes, dpi, options); // Extract substitutions GTArray<int> reps(maxtag); for (i=0; i<=maxtag; i++) reps[i] = -1; for (i=0; i<nshapes; i++) if (handles[i]) { int r = reps[tags[i]]; lib[i].match = r; if (r < 0) reps[tags[i]] = i; } // Free Ilya's data structures. for (i=0; i<nshapes; i++) if (handles[i]) mdjvu_pattern_destroy(handles[i]); }
/// constructor /// @param image input image, is considered as a binary image ( all non-zero pixels are 1 ) region_properties::region_properties ( const cv::Mat& image, double minArea ): m_minArea( minArea ) { cv::Mat binary; if( image.channels() == 3 ) { cv::cvtColor( image, binary, CV_RGB2GRAY ); } else if( image.channels() == 1 ) { binary = image; } else { COMMA_THROW( comma::exception, "incorrect number of channels, should be 1 or 3, not " << image.channels() ); } // cv::Mat closed; // cv::morphologyEx( binary, closed, cv::MORPH_CLOSE, cv::Mat::ones( 3, 3, CV_8U) ); std::vector< std::vector<cv::Point> > contours; cv::findContours( binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); for( unsigned int i = 0; i < contours.size(); i++ ) { binary = cv::Scalar(0); cv::drawContours( binary, contours, i, cv::Scalar(0xFF), CV_FILLED ); cv::Rect rect = cv::boundingRect( cv::Mat( contours[i]) ); cv::Moments moments = cv::moments( binary( rect ), true ); double x = moments.m10/moments.m00; double y = moments.m01/moments.m00; double area = moments.m00; // cv::countNonZero( binary( rect ) ) if( area > m_minArea ) { // see wikipedia, image moments double diff = moments.nu20 - moments.nu02; double a = 0.5 * ( moments.nu20 + moments.nu02 ); double b = 0.5 * std::sqrt( 4 * moments.nu11 * moments.nu11 + diff * diff ); double minEigenValue = a - b; double maxEigenValue = a + b; // std::cerr << " min " << minEigenValue << " max " << maxEigenValue << std::endl; double theta = 0.5 * std::atan2( 2 * moments.nu11, diff ); double eccentricity = 1; if( std::fabs( maxEigenValue ) > 1e-15 ) { eccentricity = std::sqrt( 1 - minEigenValue / maxEigenValue ); } double polygonArea; double convexArea; compute_area( contours[i], polygonArea, convexArea ); // std::cerr << " area " << area << " polygon " << polygonArea << " convex " << convexArea << std::endl; blob blob; blob.majorAxis = 2 * std::sqrt( moments.m00 * maxEigenValue ); blob.minorAxis = 2 * std::sqrt( moments.m00 * minEigenValue ); blob.orientation = theta; blob.centroid = cv::Point( x + rect.x, y + rect.y ); blob.area = area; blob.eccentricity = eccentricity; blob.solidity = 0; if( std::fabs( convexArea ) > 1e-15 ) { blob.solidity = polygonArea / convexArea; } m_blobs.push_back( blob ); } } }