Beispiel #1
1
//ProjectGray returns a greyscale sinogram (or radon projection) of img
// N(default: 360): number of image rotation on which a projection will be done
// A naive simplistic approach
// Sinograms looks like this :
// θ1 θ2 θ3...θN
// |  |  |    |
// |  |  |    |
func ProjectGray(src image.Image, N int) (*image.Gray, error) {
	if N == 0 {
		N = 360
	}
	step := 180.0 / float64(N)

	size := src.Bounds().Size()
	overX := int(float64(size.X) * 1.1)
	overY := int(float64(size.Y) * 1.1)
	var img image.Image = image.NewGray(image.Rect(0, 0, size.X+overX, size.Y+overY))
	img = imaging.Overlay(img, src, image.Pt(overX/2, overY/2), 1)
	size = img.Bounds().Size()

	D := max(size.X, size.Y)
	out := image.NewGray(image.Rect(0, 0, N, D))

	// for each given angle θ
	for n := 0; n < N; n++ {
		θ := float64(n) * step
		draw := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dy(), img.Bounds().Dx()))
		//have a duplicate img rotated by θ
		err := graphics.Rotate(draw, img, &graphics.RotateOptions{Angle: manipulator.Rad(θ)})
		if err != nil {
			return out, err
		}

		sinogram := make([]float64, size.X)
		// get column average profile
		for y := 0; y < size.Y; y++ {
			for x := 0; x < size.X; x++ {
				greyColor, _ := color.GrayModel.Convert(draw.At(x, y)).(color.Gray)
				sinogram[x] = sinogram[x] + float64(greyColor.Y)
			}
		}

		//Set out line with sinogram
		for d := 0; d < D; d++ {
			out.Set(n, d, color.Gray{uint8(sinogram[d] / float64(size.Y))})
		}
	}

	return out, nil
}
func Overlay(images []*ingest.Image) (*ingest.Image, error) {
	if len(images) == 0 {
		return nil, fmt.Errorf("empty slice was passed in")
	} else if len(images) == 1 {
		return images[0], nil
	}
	meta := CommonMeta(images)
	path := OverlayName(images)
	fi, err := os.Stat(path)
	if err != nil && os.IsNotExist(err) {
		// ok we will make it below
	} else if err != nil {
		return nil, err
	} else if fi.Size() > 0 {
		return &ingest.Image{path, meta}, nil
	}
	imgs := make([]image.Image, 0, len(images))
	for _, i := range images {
		img, err := ingest.LoadImage(i.Path)
		if err != nil {
			return nil, err
		}
		imgs = append(imgs, img)
	}
	overlay := imgs[0]
	for i := 1; i < len(imgs); i++ {
		overlay = imaging.Overlay(overlay, imgs[i], image.Pt(0, 0), .50)
	}
	err = ingest.WriteJpeg(path, overlay)
	if err != nil {
		return nil, err
	}
	return &ingest.Image{path, meta}, nil
}