Esempio n. 1
0
File: pyr.go Progetto: jvlmdr/go-cv
func MultiScale(im image.Image, tmpls map[string]*detect.FeatTmpl, opts detect.MultiScaleOpts) ([]Det, error) {
	if len(tmpls) == 0 {
		return nil, nil
	}
	scales := imgpyr.Scales(im.Bounds().Size(), minDims(tmpls), opts.MaxScale, opts.PyrStep).Elems()
	ims := imgpyr.NewGenerator(im, scales, opts.Interp)
	pyr := featpyr.NewGenerator(ims, opts.Transform, opts.Pad)
	var dets []Det
	l, err := pyr.First()
	if err != nil {
		return nil, err
	}
	for l != nil {
		for key, tmpl := range tmpls {
			pts := detect.Points(l.Feat, tmpl.Image, tmpl.Bias, opts.DetFilter.LocalMax, opts.DetFilter.MinScore)
			// Convert to scored rectangles in the image.
			for _, pt := range pts {
				rect := pyr.ToImageRect(l.Image.Index, pt.Point, tmpl.Interior)
				dets = append(dets, Det{detect.Det{pt.Score + tmpl.Bias, rect}, key})
			}
		}
		var err error
		l, err = pyr.Next(l)
		if err != nil {
			return nil, err
		}
	}
	Sort(dets)
	inds := detect.SuppressIndex(DetSlice(dets), opts.SupprFilter.MaxNum, opts.SupprFilter.Overlap)
	dets = detsSubset(dets, inds)
	return dets, nil
}
Esempio n. 2
0
File: pyr.go Progetto: jvlmdr/go-cv
// MultiScale searches an image at multiple scales and performs non-max suppression.
//
// At each level, the image is rescaled using Interp,
// then padded using Pad before calling Transform.Apply().
// The levels are geometrically spaced at intervals of PyrStep.
// Detections are filtered using DetFilter and then non-max suppression
// is performed using the OverlapFunc test.
func MultiScale(im image.Image, scorer slide.Scorer, shape PadRect, opts MultiScaleOpts) ([]Det, MultiScaleDuration, error) {
	scales := imgpyr.Scales(im.Bounds().Size(), scorer.Size(), opts.MaxScale, opts.PyrStep).Elems()
	ims := imgpyr.NewGenerator(im, scales, opts.Interp)
	pyr := featpyr.NewGenerator(ims, opts.Transform, opts.Pad)
	var dets []Det
	l, err := pyr.First()
	if err != nil {
		return nil, MultiScaleDuration{}, err
	}
	var dur MultiScaleDuration
	for l != nil {
		t := time.Now()
		pts, err := Points(l.Feat, scorer, opts.DetFilter.LocalMax, opts.DetFilter.MinScore)
		if err != nil {
			return nil, MultiScaleDuration{}, err
		}
		dur.Slide += time.Since(t)
		// Convert to scored rectangles in the image.
		for _, pt := range pts {
			rect := pyr.ToImageRect(l.Image.Index, pt.Point, shape.Int)
			dets = append(dets, Det{pt.Score, rect})
		}
		l, err = pyr.Next(l)
		if err != nil {
			return nil, MultiScaleDuration{}, err
		}
	}
	dur.Resize = pyr.DurResize
	dur.Feat = pyr.DurFeat
	t := time.Now()
	Sort(dets)
	dets = Suppress(dets, opts.SupprFilter.MaxNum, opts.SupprFilter.Overlap)
	dur.Suppr = time.Since(t)
	return dets, dur, nil
}
Esempio n. 3
0
File: main.go Progetto: jvlmdr/go-cv
func evalImage(tmpl *detect.FeatTmpl, im image.Image, pyrStep float64, hogBin int, opts featpyr.DetectOpts) []detect.Det {
	// Construct image pyramid.
	scales := imgpyr.Scales(im.Bounds().Size(), tmpl.Size, pyrStep)
	pixpyr := imgpyr.New(im, scales)
	// Construct HOG pyramid.
	fn := func(rgb *rimg64.Multi) *rimg64.Multi { return hog.HOG(rgb, hog.FGMRConfig(hogBin)) }
	pyr := featpyr.New(pixpyr, fn, hogBin)
	// Search feature pyramid.
	dets := featpyr.Detect(pyr, tmpl, opts)
	return dets
}
Esempio n. 4
0
File: test.go Progetto: jvlmdr/go-cv
// Runs a single detector across a single image and returns results.
func detectImage(tmpl *detect.FeatTmpl, im image.Image, margin int, step float64, sbin int, localmax bool, maxiou float64) []detect.Det {
	// Construct pyramid.
	// Get range of scales.
	scales := imgpyr.Scales(im.Bounds().Size(), tmpl.Size, step)
	// Define feature transform.
	phi := hog.Transform{hog.FGMRConfig(sbin)}
	// Define amount and type of padding.
	pad := feat.Pad{feat.Margin{margin, margin, margin, margin}, imsamp.Continue}
	pyr := featpyr.NewPad(imgpyr.New(im, scales), phi, pad)

	// Search feature pyramid.
	// Options for running detector on each level.
	detopts := detect.DetFilter{LocalMax: localmax, MinScore: math.Inf(-1)}
	// Use intersection-over-union criteria for non-max suppression.
	overlap := func(a, b image.Rectangle) bool {
		return detect.IOU(a, b) > maxiou
	}
	// Options for non-max suppression.
	suppropts := detect.SupprFilter{MaxNum: 0, Overlap: overlap}
	dets := detect.Pyramid(pyr, tmpl, detopts, suppropts)
	return dets
}
Esempio n. 5
0
File: main.go Progetto: jvlmdr/go-cv
func main() {
	var (
		sbin     = flag.Int("sbin", 4, "Spatial binning parameter to HOG")
		margin   = flag.Int("margin", 0, "Margin to add around images before computing features")
		step     = flag.Float64("pyr-step", 1.1, "Geometric step to use in image pyramid")
		maxinter = flag.Float64("max-intersect", 0.5, "Maximum overlap of detections. Zero means detections can't overlap at all, one means they can overlap entirely.")
		localmax = flag.Bool("local-max", true, "Detections cannot score less than a neighbor")
	)

	flag.Usage = usage
	flag.Parse()

	if flag.NArg() != 3 {
		flag.Usage()
		os.Exit(1)
	}
	var (
		tmplFile = flag.Arg(0)
		imFile   = flag.Arg(1)
		detsFile = flag.Arg(2)
	)

	// Load image.
	im, err := loadImage(imFile)
	if err != nil {
		log.Fatal(err)
	}
	// Construct pyramid.
	scales := imgpyr.Scales(im.Bounds().Size(), image.Pt(24, 24), *step)
	phi := hog.Transform{hog.FGMRConfig(*sbin)}
	pad := feat.Pad{feat.Margin{*margin, *margin, *margin, *margin}, imsamp.Continue}
	pyr := featpyr.NewPad(imgpyr.New(im, scales), phi, pad)

	// Load template.
	var tmpl *detect.FeatTmpl
	if err := loadGob(tmplFile, &tmpl); err != nil {
		log.Fatal(err)
	}

	detopts := detect.DetFilter{
		LocalMax: *localmax,
		MinScore: math.Inf(-1),
	}
	// Use intersection-over-union criteria for non-max suppression.
	overlap := func(a, b image.Rectangle) bool {
		return detect.IOU(a, b) > *maxinter
	}
	suppropts := detect.SupprFilter{
		MaxNum:  0,
		Overlap: overlap,
	}
	dets := detect.Pyramid(pyr, tmpl, detopts, suppropts)

	if err := saveJSON(detsFile, dets); err != nil {
		log.Fatal(err)
	}

	for i, det := range dets {
		r := det.Rect
		cmd := fmt.Sprintf("rectangle %d,%d %d,%d", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
		fmt.Printf("convert %s -fill none -stroke white -draw '%s' det_%06d.jpg\n", imFile, cmd, i)
	}
}