Exemplo n.º 1
0
func LoadMasks(folder string) *MaskIndex {
	scaler := cv.ScaleBinaryImage{Height: SCALE_HEIGHT}
	bounder := cv.BoundBinaryImage{}
	ret := NewMaskIndex()
	n := 0
	err := filepath.Walk(folder, func(root string, f os.FileInfo, err error) error {
		if f == nil {
			return err
		}
		if f.IsDir() {
			label := f.Name()
			if len(label) > 2 || label == "#" {
				return nil
			}
			log.Println(label, root)
			filepath.Walk(root, func(root2 string, f2 os.FileInfo, err2 error) error {
				if f2 == nil {
					return err
				}
				if f2.IsDir() {
					return nil
				}
				img := LoadImage(root2)
				if img != nil {
					mk := Mask{}
					mk.Img = scaler.Process(bounder.Process(cv.Convert2BinaryImage(img)))
					mk.Label = label
					ret.AddMask(&mk)
					n += 1
				}
				return nil
			})
			return nil
		}
		return nil
	})
	if err != nil {
		panic(err)
	}
	log.Println("mask count", n)
	return ret
}
Exemplo n.º 2
0
func (self *FastCutBasedPredictor) Guess(img *cv.BinaryImage, mki *MaskIndex, chType int) []*Result {
	h := self.XHis(img)
	scaler := cv.ScaleBinaryImage{Height: SCALE_HEIGHT}
	bounder := cv.BoundBinaryImage{}
	curX := 0
	ret := NewResult("", 0.0)
	failCount := 0
	for {
		if curX >= img.Width {
			break
		}
		curX, _ = FindBeginPos(img, curX)

		maxSim := 0.0
		var bestCutImg *cv.BinaryImage
		var bestLocalResults []*Result
		nextX := 0
		endXs := self.FindNextSplitPoints(img, curX, h)
		for beginX := -2; beginX < 3; beginX++ {
			for k, endX := range endXs {
				if k > 20 {
					break
				}
				width := endX - curX - beginX + 1
				if width <= 3 {
					continue
				}

				cutImg := self.CutMatrixByRect(img, curX+beginX, 0, endX, img.Height)
				if cutImg == nil || cutImg.FrontSize() < 10 {
					continue
				}
				cutImg = bounder.Process(cutImg)
				if cutImg.Height < 5 {
					continue
				}
				cutImg = scaler.Process(cutImg)
				if cutImg == nil {
					continue
				}
				localResults := mki.FindBestMatchedMasks(cutImg, chType, 1)
				if localResults == nil || len(localResults) == 0 {
					continue
				}
				if maxSim < localResults[0].Weight {
					maxSim = localResults[0].Weight
					bestLocalResults = localResults
					nextX = endX
					bestCutImg = cutImg
				}
			}
			if maxSim > 0.8 {
				break
			}
		}
		if maxSim < 0.6 {
			curX += 1
			failCount += 1
			continue
		}
		curX = nextX
		ret.Label += bestLocalResults[0].Label
		ret.Weight += bestLocalResults[0].Weight
		ret.AddComponent(bestCutImg)
		log.Println(bestLocalResults[0].Label, maxSim)
		if len(ret.Label) > 16 || failCount > 10 {
			return []*Result{}
		}
	}
	return []*Result{ret}
}