예제 #1
0
파일: crop.go 프로젝트: 9nut/go-opencv
func main() {
	_, currentfile, _, _ := runtime.Caller(0)
	filename := path.Join(path.Dir(currentfile), "../images/lena.jpg")
	if len(os.Args) == 2 {
		filename = os.Args[1]
	}

	image := opencv.LoadImage(filename)
	if image == nil {
		panic("LoadImage fail")
	}
	defer image.Release()

	crop := opencv.Crop(image, 0, 0, 50, 50)
	opencv.SaveImage("/tmp/crop.jpg", crop, 0)
	crop.Release()

	os.Exit(0)
}
예제 #2
0
//detects faces and crops em out
func cropFaces(inputs []string, dirOut string, harrcascade string) {
	err := os.MkdirAll(dirOut, 0777) // makes dir if not exists
	if err != nil {
		fmt.Printf("Could not create directory %s\n", dirOut)
		log.Fatal(err)
	}

	fmt.Printf("Cropping %d images.\n", len(inputs))

	for _, element := range inputs {

		outPath := dirOut + "face_" + filepath.Base(element)

		image := opencv.LoadImage(element)
		if image == nil {
			panic("Loading Image " + element + "failed")
		}
		defer image.Release()

		//detect some faces
		cascade := opencv.LoadHaarClassifierCascade(harrcascade)
		faces := cascade.DetectObjects(image)
		if len(faces) == 0 {
			fmt.Printf("Found no face in %s\n", element)
		}

		for _, value := range faces {
			//Some invalid stuff filtering
			if value.Width() < image.Width() && value.Height() < image.Height() && value.Width() > 0 && value.Height() > 0 {
				//crop out the face
				crop := opencv.Crop(image, value.X(), value.Y(), value.Width(), value.Height())
				//save cropped
				opencv.SaveImage(outPath, crop, 0)
				crop.Release()
			} else {
				fmt.Printf("Couldn't save: %s", value)
			}
		}
	}
}