func (l LabelMap) EncodeImageToImageSegment(bmpImage image.Image) (image.Image, error) {

	// Because the HTML5 canvas can be imprecise, we need to map nearby colors
	colorPalette := color.Palette{}
	for _, v := range l.Sorted() {

		// Create this label's color.Color based on its hex representation, and
		// add it to our permitted color palette
		col, err := nrgbaFromColorCode(v.Color)
		if err != nil {
			continue
		}
		colorPalette = append(colorPalette, col)

		// Double check that our palette colors map back to themselves
		r, g, b, _ := col.RGBA()
		cl := fmt.Sprintf("#%02x%02x%02x", uint8(r), uint8(g), uint8(b))

		// Background is special cased to be allowed to mismatch
		if cl != "#000000" && cl != v.Color {
			return nil, fmt.Errorf("Label ID %d (color %s) mapped to %s instead of its own color", v.ID, v.Color, cl)
		}
	}

	// Map from the color code to the Label, with all of its attached
	// information
	colorLabels := make(map[color.Color]Label)

	outputImage := image.NewRGBA(bmpImage.Bounds())

	for y := 0; y < bmpImage.Bounds().Max.Y; y++ {
		for x := 0; x < bmpImage.Bounds().Max.X; x++ {

			// Find the color at this pixel
			rawC := bmpImage.At(x, y)

			// If we haven't yet mapped this point's color to a Label
			// identifier, do so now:
			if _, exists := colorLabels[rawC]; !exists {

				// The exact color chosen by HTML5 may or may not exist, so we
				// will choose the nearest one from our palette:
				c := colorPalette[colorPalette.Index(rawC)]

				// Create the hex string representation
				r, g, b, _ := c.RGBA()
				cl := fmt.Sprintf("#%02x%02x%02x", uint8(r), uint8(g), uint8(b))

				// Look up the hex string representation and map it
				for _, v := range l.Sorted() {
					if v.Color == cl {
						colorLabels[rawC] = v
						break
					} else if cl == "#000000" && v.ID == 0 {
						// Background, special case
						colorLabels[rawC] = v
						break
					}
				}
			}

			// Make sure that all labels are known
			lab, exists := colorLabels[rawC]
			if !exists {
				return nil, fmt.Errorf("Saw color %v at {%d, %d} but could not find a neighbor for this color in the label map palette", rawC, x, y)
			}

			// If
			// var opacity uint8 = 255
			// if lab.ID == 0 {
			// 	opacity = 0
			// }
			outputImage.Set(x, y, color.RGBA{
				R: uint8(lab.ID),
				G: uint8(lab.ID),
				B: uint8(lab.ID),
				A: 255,
			})
		}
	}

	return outputImage, nil
}
// EncodeImageToImageSegment consumes a multi-color human-visible image into an
// image where each pixel has the same R, G, and B value mapped to the integer
// ID of the Label. For example, if background is transparent (ID 0) and the
// left atrium is red (ID 1), it will produce an image that is all black, with
// values #000000 for the background and #010101 for the left atrium.
EncodeImageToImageSegment consumes a multi-color human-visible image into an
image where each pixel has the same R, G, and B value mapped to the integer
ID of the Label. For example, if background is transparent (ID 0) and the
left atrium is red (ID 1), it will produce an image that is all black, with
values #000000 for the background and #010101 for the left atrium.
// EncodeImageToImageSegment consumes a multi - color human - visible image into an // image where each pixel has the same R G and B value mapped to the integer // ID of the Label . For example if background is transparent ( ID 0 ) and the // left atrium is red ( ID 1 ) it will produce an image that is all black with // values #000000 for the background and #010101 for the left atrium .
func (l LabelMap) EncodeImageToImageSegment(bmpImage image.Image) (image.Image, error) {
	colorPalette := color.Palette{}
	for _, v := range l.Sorted() {
		col, err := nrgbaFromColorCode(v.Color)
		if err != nil {
			continue
		}
		colorPalette = append(colorPalette, col)
		r, g, b, _ := col.RGBA()
		cl := fmt.Sprintf("#%02x%02x%02x", uint8(r), uint8(g), uint8(b))
		if cl != "#000000" && cl != v.Color {
			return nil, fmt.Errorf("Label ID %d (color %s) mapped to %s instead of its own color", v.ID, v.Color, cl)
		}
	}
	colorLabels := make(map[color.Color]Label)
	outputImage := image.NewRGBA(bmpImage.Bounds())
	for y := 0; y < bmpImage.Bounds().Max.Y; y++ {
		for x := 0; x < bmpImage.Bounds().Max.X; x++ {
			rawC := bmpImage.At(x, y)
			if _, exists := colorLabels[rawC]; !exists {
				c := colorPalette[colorPalette.Index(rawC)]
				r, g, b, _ := c.RGBA()
				cl := fmt.Sprintf("#%02x%02x%02x", uint8(r), uint8(g), uint8(b))
				for _, v := range l.Sorted() {
					if v.Color == cl {
						colorLabels[rawC] = v
						break
					} else if cl == "#000000" && v.ID == 0 {
						colorLabels[rawC] = v
						break
					}
				}
			}
			lab, exists := colorLabels[rawC]
			if !exists {
				return nil, fmt.Errorf("Saw color %v at {%d, %d} but could not find a neighbor for this color in the label map palette", rawC, x, y)
			}
			outputImage.Set(x, y, color.RGBA{
				R: uint8(lab.ID),
				G: uint8(lab.ID),
				B: uint8(lab.ID),
				A: 255,
			})
		}
	}
	return outputImage, nil
}
func ( l LabelMap ) EncodeImageToImageSegment ( bmpImage image . Image ) ( image . Image , error ) { colorPalette := color . Palette { } for _ , v := range l . Sorted ( ) { col , err := nrgbaFromColorCode ( v . Color ) if err != nil { continue } colorPalette = append ( colorPalette , col ) r , g , b , _ := col . RGBA ( ) cl := fmt . Sprintf ( " " , uint8 ( r ) , uint8 ( g ) , uint8 ( b ) ) if cl != " " && cl != v . Color { return nil , fmt . Errorf ( " " , v . ID , v . Color , cl ) } } colorLabels := make ( map [ color . Color ] Label ) outputImage := image . NewRGBA ( bmpImage . Bounds ( ) ) for y := 0 ; y < bmpImage . Bounds ( ) . Max . Y ; y ++ { for x := 0 ; x < bmpImage . Bounds ( ) . Max . X ; x ++ { rawC := bmpImage . At ( x , y ) if _ , exists := colorLabels [ rawC ] ; ! exists { c := colorPalette [ colorPalette . Index ( rawC ) ] r , g , b , _ := c . RGBA ( ) cl := fmt . Sprintf ( " " , uint8 ( r ) , uint8 ( g ) , uint8 ( b ) ) for _ , v := range l . Sorted ( ) { if v . Color == cl { colorLabels [ rawC ] = v break } else if cl == " " && v . ID == 0 { colorLabels [ rawC ] = v break } } } lab , exists := colorLabels [ rawC ] if ! exists { return nil , fmt . Errorf ( " " , rawC , x , y ) } outputImage . Set ( x , y , color . RGBA { R : uint8 ( lab . ID ) , G : uint8 ( lab . ID ) , B : uint8 ( lab . ID ) , A : 255 , } ) } } return outputImage , nil }
EncodeImageToImageSegment consumes a multi-color human-visible image into an
image where each pixel has the same R, G, and B value mapped to the integer
ID of the Label.
EncodeImageToImageSegment consumes a multi - color human - visible image into an image where each pixel has the same R G and B value mapped to the integer ID of the Label .
// Because the HTML5 canvas can be imprecise, we need to map nearby colors // Create this label's color.Color based on its hex representation, and // add it to our permitted color palette // Double check that our palette colors map back to themselves // Background is special cased to be allowed to mismatch // Map from the color code to the Label, with all of its attached // information // Find the color at this pixel // If we haven't yet mapped this point's color to a Label // identifier, do so now: // The exact color chosen by HTML5 may or may not exist, so we // will choose the nearest one from our palette: // Create the hex string representation // Look up the hex string representation and map it // Background, special case // Make sure that all labels are known // If // var opacity uint8 = 255 // if lab.ID == 0 { // 	opacity = 0 // }