void VilDraw::draw_cross(vil_image_view<vxl_byte> &image, const vcl_vector< vgl_point_2d<double>> &pts, int crossWidth, const vcl_vector< vxl_byte >& colour, int lineWidth) { assert(image.nplanes() == 3 || image.nplanes() == 1); assert(colour.size() == 3); vil_image_view<vxl_byte> rgb_image = image; if (image.nplanes() == 1) { image = VilUtil::gray_2_rgb(image); } for (unsigned int i = 0; i<pts.size(); i++) { //center point double px = pts[i].x(); double py = pts[i].y(); vgl_point_2d<double> p1, p2, p3, p4; double h_l = crossWidth; p1.set(px - h_l, py); p2.set(px + h_l, py); p3.set(px, py - h_l); p4.set(px, py + h_l); VilAlgoPlus::fill_line(image, p1, p2, colour); VilAlgoPlus::fill_line(image, p3, p4, colour); } }
void VilBaplSIFT::getSiftPositions(const vil_image_view<vxl_byte> &image, vcl_vector<vgl_point_2d<double> > &points, double curve_ratio) { assert(image.nplanes() == 1 || image.nplanes() == 3); assert(points.size() == 0); vil_image_view<vxl_byte> grey_img; if (image.nplanes() == 3) { vil_convert_planes_to_grey(image, grey_img); } else { grey_img.deep_copy(image); } vcl_vector<bapl_keypoint_sptr> sift_keypoints; vil_image_resource_sptr image_sptr = vil_new_image_resource_of_view(grey_img); // float curve_ratio = 2.0f; bapl_keypoint_extractor(image_sptr, sift_keypoints, curve_ratio); vcl_vector<bapl_keypoint_sptr>::iterator keypoint_itr, keypoint_end; keypoint_end = sift_keypoints.end(); for (keypoint_itr = sift_keypoints.begin(); keypoint_itr != keypoint_end; ++keypoint_itr) { bapl_lowe_keypoint_sptr sift_lowe_keypoint = dynamic_cast<bapl_lowe_keypoint*>((*keypoint_itr).as_pointer()); double x = sift_lowe_keypoint->location_i(); double y = sift_lowe_keypoint->location_j(); points.push_back(vgl_point_2d<double>(x, y)); } }
void VilBaplSIFT::getSiftDescription(const vil_image_view<vxl_byte> &image, vcl_vector<bapl_lowe_keypoint_sptr> &descriptions) { assert(image.nplanes() == 1 || image.nplanes() == 3); vil_image_view<vxl_byte> grey_img; if (image.nplanes() == 3) { vil_convert_planes_to_grey(image, grey_img); } else { grey_img.deep_copy(image); } vcl_vector<bapl_keypoint_sptr> sift_keypoints; vil_image_resource_sptr image_sptr = vil_new_image_resource_of_view(grey_img); bapl_keypoint_extractor(image_sptr, sift_keypoints); for (vcl_vector<bapl_keypoint_sptr>::iterator itr = sift_keypoints.begin(); itr != sift_keypoints.end(); itr++) { bapl_lowe_keypoint_sptr sift_lowe_keypoint = dynamic_cast<bapl_lowe_keypoint*>((*itr).as_pointer()); assert(sift_lowe_keypoint); descriptions.push_back(sift_lowe_keypoint); } }
void CvxLSD::detect_lines(const vil_image_view<vxl_byte> & image, vcl_vector<LSDLineSegment> & line_segments) { assert(image.nplanes() == 3 || image.nplanes() == 1); vil_image_view<vxl_byte> grayImage; if (image.nplanes() == 3) { vil_convert_planes_to_grey(image, grayImage); } else { grayImage = image; } double * imageData = NULL; double * out = NULL; int width = grayImage.ni(); int height = grayImage.nj(); int n = 0; imageData = (double *) malloc( width * height * sizeof(double) ); assert(imageData); for(int j=0; j<height; j++) { for(int i=0; i<width; i++) { imageData[i + j * width] = grayImage(i, j); } } /* LSD call */ out = lsd(&n, imageData, width, height); /* print output */ // printf("%d line segments found:\n",n); line_segments.resize(n); for(int i=0; i<n; i++) { double x1 = out[7*i + 0]; double y1 = out[7*i + 1]; double x2 = out[7*i + 2]; double y2 = out[7*i + 3]; double line_width = out[7*i + 4]; double p = out[7*i + 5]; double tmp = out[7*i + 6]; line_segments[i].seg_ = vgl_line_segment_2d<double>(vgl_point_2d<double>(x1, y1), vgl_point_2d<double>(x2, y2)); line_segments[i].width_ = line_width; line_segments[i].angle_precision_ = p; line_segments[i].NFA_ = tmp; } free( (void *) imageData ); free( (void *) out ); }
void VilGMM::probabilty(const vil_image_view<vxl_byte> & image, const vil_image_view<vxl_byte> & maskImage, int mask, vil_image_view<double> & outProb, int patchSize) { assert(image.ni() == maskImage.ni()); assert(image.nj() == maskImage.nj()); assert(image.nplanes() == 3); assert(gmm_); assert(patchSize%2 == 1); vil_image_view<double> pixelProb(image.ni(), image.nj(), 1); pixelProb.fill(0.0); // probability in each pixel position int w = image.ni(); int h = image.nj(); for (int j = 0; j<h; j++) { for (int i = 0; i<w; i++) { if (maskImage(i, j) == mask) { vnl_vector<double> color(3); for (int k = 0; k<3; k++) { color[k] = image(i, j, k); } pixelProb(i, j) = (*gmm_)(color); } } } outProb = vil_image_view<double>(w, h, 1); outProb.fill(0.0); // average probility for (int j = patchSize/2; j<h-patchSize/2; j++) { for (int i = patchSize/2; i<w-patchSize/w; i++) { if (maskImage(i, j) == mask) { // average the value inside the patch double val = 0; int num = 0; for (int k = -patchSize/2; k <= patchSize/2; k++) { for (int m = -patchSize/2; m<= patchSize/2; m++) { if (maskImage(i + m, j+ k) == mask) { val += pixelProb(i+m, j+k); num++; } } } assert(num != 0); val /= num; outProb(i, j) = val; } } } }
void VilDraw::draw_segment(vil_image_view<vxl_byte> &image, const vgl_line_segment_2d<double> & seg, const vcl_vector<vxl_byte> & colour, int lineWidth) { assert(image.nplanes() == 3 || image.nplanes() == 1); assert(colour.size() == 3); vil_image_view<vxl_byte> rgb_image = image; if (image.nplanes() == 1) { image = VilUtil::gray_2_rgb(image); } VilAlgoPlus::fill_line(image, seg.point1(), seg.point2(), colour); }
void VilBaplSIFT::getSIFT(const vil_image_view<vxl_byte> &image, vcl_vector<bapl_keypoint_sptr> & features) { assert(image.nplanes() == 1 || image.nplanes() == 3); assert(features.size() == 0); vil_image_view<vxl_byte> grey_img; if (image.nplanes() == 3) { vil_convert_planes_to_grey(image, grey_img); } else { grey_img.deep_copy(image); } bapl_keypoint_extractor(vil_new_image_resource_of_view(grey_img), features); }
void VilDraw::draw_match_dense(const vil_image_view<vxl_byte> &image1, const vil_image_view<vxl_byte> &image2, const vcl_vector< vgl_point_2d<double> > & pts1, const vcl_vector< vgl_point_2d<double> > & pts2, const vcl_vector<bool> & inliers, vil_image_view<vxl_byte> &matches, const int thickness) { assert(image1.nplanes() == 1 || image1.nplanes() == 3); assert(image2.nplanes() == 1 || image1.nplanes() == 3); assert(pts1.size() == pts2.size()); assert(inliers.size() == pts1.size()); vcl_vector<vgl_point_2d<double> > inlier1; vcl_vector<vgl_point_2d<double> > inlier2; for (int i = 0; i<inliers.size(); i++) { if (inliers[i]) { inlier1.push_back(pts1[i]); inlier2.push_back(pts2[i]); } } printf("inlier number is %lu", inlier1.size()); VilDraw::draw_match(image1, image2, inlier1, inlier2, matches, thickness); }
void VilBaplSIFT::getSiftDescription(const vil_image_view<vxl_byte> & image, const vcl_vector<vgl_point_2d<double> > & pts, vcl_vector<bapl_lowe_keypoint_sptr> & sifts) { assert(image.nplanes() == 3); assert(sifts.size() == 0); vil_image_view<vxl_byte> grey_img; vil_convert_planes_to_grey(image, grey_img); bapl_lowe_pyramid_set_sptr pyramid_set = new bapl_lowe_pyramid_set(vil_new_image_resource_of_view(grey_img)); for (unsigned int i = 0; i<pts.size(); i++) { // bapl_lowe_keypoint keypoint(pyramid_set, pts[i].x(), pts[i].y()); bapl_lowe_keypoint_sptr keypoint = new bapl_lowe_keypoint(pyramid_set, pts[i].x(), pts[i].y()); sifts.push_back(keypoint); } }
bool VilGMM::train(const vil_image_view<vxl_byte> & image, const vil_image_view<vxl_byte> & maskImage, int mask) { assert(image.ni() == maskImage.ni()); assert(image.nj() == maskImage.nj()); assert(image.nplanes() == 3); vpdfl_gaussian_builder g_builder; vpdfl_mixture_builder builder; builder.init(g_builder,comp_n_); builder.set_weights_fixed(false); vcl_vector<vnl_vector<double> > data; for (int j = 0; j<image.nj(); j++) { for (int i = 0; i<image.ni(); i++) { if (maskImage(i, j) == mask) { vnl_vector<double> color(3); for (int k = 0; k<3; k++) { color[k] = image(i, j, k); } data.push_back(color); } } } if (data.size() <= comp_n_ * 20) { return false; } if (gmm_) { delete gmm_; gmm_ = NULL; } gmm_ = builder.new_model(); mbl_data_array_wrapper<vnl_vector<double> > data_array(data); builder.build(*gmm_, data_array); if (verbose_) { vcl_cout<<"training sample number is "<<data.size()<<vcl_endl; vcl_cout<<"Probability distribution function is "<<gmm_<<vcl_endl; vcl_cout<<"Mean: "<<gmm_->mean()<<vcl_endl; vcl_cout<<"Var: "<<gmm_->variance()<<vcl_endl; } return true; }
bool VilGMM::addOneImage(const vil_image_view<vxl_byte> & image, const vil_image_view<vxl_byte> & maskImage, int mask) { assert(image.ni() == maskImage.ni()); assert(image.nj() == maskImage.nj()); assert(image.nplanes() == 3); for (int j = 0; j<image.nj(); j++) { for (int i = 0; i<image.ni(); i++) { if (maskImage(i, j) == mask) { vnl_vector<double> color(3); for (int k = 0; k<3; k++) { color[k] = image(i, j, k); } data_.push_back(color); } } } return true; }
void VilBaplSIFT::getSiftFromDesignatedPositions(const vil_image_view<vxl_byte> & image, vcl_vector<vnl_vector_fixed<double, 4> > & loc_sca_ori, vcl_vector<bapl_keypoint_sptr> & sifts) { // assert(image.nplanes() == 3); assert(sifts.size() == 0); vil_image_view<vxl_byte> source_grey; if (image.nplanes() == 3) { vil_convert_planes_to_grey(image, source_grey); } else { source_grey = image; } bapl_lowe_pyramid_set_sptr source_pyramid_set = new bapl_lowe_pyramid_set(vil_new_image_resource_of_view(source_grey)); for (int i = 0; i<loc_sca_ori.size(); i++) { bapl_lowe_keypoint *pKeypoint = new bapl_lowe_keypoint(source_pyramid_set, loc_sca_ori[i][0], loc_sca_ori[i][1], loc_sca_ori[i][2], loc_sca_ori[i][3]); sifts.push_back(bapl_keypoint_sptr(pKeypoint)); } assert(loc_sca_ori.size() == sifts.size()); }
void VilDraw::draw_match_vertical(const vil_image_view<vxl_byte> &image1, const vil_image_view<vxl_byte> &image2, const vcl_vector< vgl_point_2d<double> > & pts1, const vcl_vector< vgl_point_2d<double> > & pts2, vil_image_view<vxl_byte> &matches, const int sample_num) { assert(image1.nplanes() == 3 || image1.nplanes() == 1); assert(image2.nplanes() == 3 || image2.nplanes() == 1); assert(pts1.size() == pts2.size()); vil_image_view<vxl_byte> rgb_image1 = image1; vil_image_view<vxl_byte> rgb_image2 = image2; if (image1.nplanes() == 1) { rgb_image1 = VilUtil::gray_2_rgb(image1); } if (image2.nplanes() == 1) { rgb_image2 = VilUtil::gray_2_rgb(image2); } int gap_width = 10; int y_shift = gap_width + image1.nj(); int width = vcl_max(image1.ni(), image2.ni()); int height = image1.nj() + gap_width + image2.nj(); matches = vil_image_view<vxl_byte>(width, height, 3); matches.fill(0); //copy image 1 to left vil_copy_to_window(rgb_image1, matches, 0, 0); //copy image 2 to right vil_copy_to_window(rgb_image2, matches, 0, y_shift); for (int i = 0; i<pts1.size(); i += sample_num) { int r = rand()%255; int g = rand()%255; int b = rand()%255; //int r = 0; //int g = 255; //int b = 0; vcl_vector< vxl_byte> colour; colour.push_back(r); colour.push_back(g); colour.push_back(b); vgl_point_2d<double> p1 = pts1[i]; vgl_point_2d<double> p2(vgl_point_2d<double>(pts2[i].x(), pts2[i].y() + y_shift)); VilAlgoPlus::fill_line(matches, p1, p2, colour); // cross in left vgl_point_2d<double> q1, q2, q3, q4; double h_l = 5; q1.set(p1.x() - h_l, p1.y()); q2.set(p1.x() + h_l, p1.y()); q3.set(p1.x(), p1.y() - h_l); q4.set(p1.x(), p1.y() + h_l); VilAlgoPlus::fill_line(matches, q1, q2, colour); VilAlgoPlus::fill_line(matches, q3, q4, colour); // cross in right q1.set(p2.x() - h_l, p2.y()); q2.set(p2.x() + h_l, p2.y()); q3.set(p2.x(), p2.y() - h_l); q4.set(p2.x(), p2.y() + h_l); VilAlgoPlus::fill_line(matches, q1, q2, colour); VilAlgoPlus::fill_line(matches, q3, q4, colour); } }
bool VlSIFTFeature::vl_keypoint_extractor(const vil_image_view<vxl_byte> & image, const vl_feat_sift_parameter ¶m, vcl_vector<bapl_keypoint_sptr> & keypoints, bool verbose) { vil_image_view<vxl_byte> grey; if (image.nplanes() == 1) { grey.deep_copy(image); } else { vil_convert_planes_to_grey(image, grey); } int width = grey.ni(); int height = grey.nj(); int noctaves = param.noctaves; // maximum octatve possible int nlevels = 3; int o_min = 0; //first octave index // create a filter to process the image VlSiftFilt *filt = vl_sift_new (width, height, noctaves, nlevels, o_min) ; double edge_thresh = param.edge_thresh; double peak_thresh = param.peak_thresh; double magnif = param.magnif ; double norm_thresh = param.norm_thresh; double window_size = param.window_size; if (peak_thresh >= 0) vl_sift_set_peak_thresh (filt, peak_thresh) ; if (edge_thresh >= 0) vl_sift_set_edge_thresh (filt, edge_thresh) ; if (norm_thresh >= 0) vl_sift_set_norm_thresh (filt, norm_thresh) ; if (magnif >= 0) vl_sift_set_magnif (filt, magnif) ; if (window_size >= 0) vl_sift_set_window_size (filt, window_size) ; // data from image vl_sift_pix *fdata = (vl_sift_pix *)malloc(width * height * sizeof (vl_sift_pix)); for (int y = 0; y<grey.nj(); y++) { for (int x = 0; x<grey.ni(); x++) { int idx = y * width + x; fdata[idx] = grey(x, y, 0); } } // Process each octave bool isFirst = true ; vl_bool err = VL_ERR_OK; int nangles = 0; double angles[4]; vnl_vector_fixed<float, 128> descriptor; while (1) { if (isFirst) { isFirst = false; err = vl_sift_process_first_octave (filt, fdata) ; } else { err = vl_sift_process_next_octave (filt) ; } if(err == VL_ERR_EOF) { break; } vl_sift_detect (filt); VlSiftKeypoint const * keys = vl_sift_get_keypoints(filt) ; int nkeys = vl_sift_get_nkeypoints (filt) ; for (int i = 0; i<nkeys; i++) { VlSiftKeypoint const * curKey = keys + i; // Depending on the symmetry of the keypoint appearance, determining the orientation can be ambiguous. SIFT detectors have up to four possible orientations nangles = vl_sift_calc_keypoint_orientations(filt, angles, curKey) ; for (int q = 0 ; q < nangles ; q++) { vl_sift_calc_keypoint_descriptor(filt, &descriptor[0], curKey, angles[q]); bapl_lowe_keypoint *pKeypoint = new bapl_lowe_keypoint(); double x = curKey->x; double y = curKey->y; double s = curKey->sigma; double o = angles[q]; vnl_vector_fixed<double, 128> des; for (int j = 0; j<128; j++) { des[j] = descriptor[j]; } pKeypoint->set_location_i(x); pKeypoint->set_location_j(y); pKeypoint->set_scale(s); pKeypoint->set_orientation(o); pKeypoint->set_descriptor(des); keypoints.push_back(bapl_keypoint_sptr(pKeypoint)); } } } vl_sift_delete(filt); delete fdata; if(verbose){ vcl_cout<<"Found "<<keypoints.size()<<" keypoints."<<vcl_endl; } return true; }