func Contrast(picture *image.Gray, contrast uint8) *image.Gray { bounds := picture.Bounds() newPic := image.NewGray(bounds) average := getAverage(picture) for x := 0; x < bounds.Dx(); x++ { for y := 0; y < bounds.Dy(); y++ { pixel := picture.At(x, y).(color.Gray).Y if pixel > average { if pixel+contrast < pixel { pixel = 0xff } else { pixel += contrast } } else if pixel < average { if pixel-contrast > pixel { pixel = 0x00 } else { pixel -= contrast } } newPic.Set(x, y, color.Gray{pixel}) } } return newPic }
func valueAt(img *image.Gray, x, y float32) float32 { dx, dy := x/float32(screenWidth), y/float32(screenHeight) b := img.Bounds().Max px, py := int(dx*float32(b.X)), int(dy*float32(b.Y)) v := float32(img.At(px, py).(color.Gray).Y) / 255 return v }
func getAverage(picture *image.Gray) uint8 { var sum uint64 = 0 bounds := picture.Bounds() for x := 0; x < bounds.Dx(); x++ { for y := 0; y < bounds.Dy(); y++ { sum += uint64(picture.At(x, y).(color.Gray).Y) } } return uint8(sum / uint64(bounds.Dx()*bounds.Dy())) }
func GrayImageToMatrix(src image.Gray) (*matrix.Dense, error) { bounds := src.Bounds() mtx := make([][]float64, bounds.Max.X) for x := 0; x < bounds.Max.X; x++ { mtx[x] = make([]float64, bounds.Max.Y) for y := 0; y < bounds.Max.Y; y++ { _, _, b, _ := src.At(x, y).RGBA() mtx[x][y] = float64(b) } } return matrix.NewDense(mtx) }
func GrayImageToMatrix(src image.Gray) (*mat64.Dense, error) { bounds := src.Bounds() rows := bounds.Max.Y cols := bounds.Max.X mtx := make([]float64, rows*cols) for x := 0; x < cols; x++ { for y := 0; y < rows; y++ { _, _, b, _ := src.At(x, y).RGBA() mtx[y*cols+x] = float64(b) } } return mat64.NewDense(rows, cols, mtx), nil }
func (entity *Entity) deviation(model *image.Gray) float64 { deviation := 0.0 actual := entity.render() for x := 0; x < entity.width; x++ { for y := 0; y < entity.height; y++ { actualColor := float64(actual.At(x, y).(color.Gray).Y) modelColor := float64(model.At(x, y).(color.Gray).Y) dev := actualColor - modelColor deviation += dev * dev } } return deviation }
func dctPoint(img image.Gray, u, v, N, M int) float64 { sum := 0.0 for i := 0; i < N; i++ { for j := 0; j < M; j++ { _, _, b, _ := img.At(i, j).RGBA() // sum += math.Cos( ( float64(2*i+1)/float64(2*N) ) * float64( u ) * math.Pi ) * // math.Cos( ( float64(2*j+1)/float64(2*M) ) * float64( v ) * math.Pi ) * // float64(b) sum += math.Cos(math.Pi/(float64(N))*(float64(i)+1/2)*float64(u)) * math.Cos(math.Pi/(float64(M))*(float64(j)+1/2)*float64(v)) * float64(b) } } return sum * ((coefficient(u) * coefficient(v)) / 4.0) }
func average(oldPic *image.Gray) *Picture { bounds := oldPic.Bounds() newWidth := charWidth * (bounds.Dx() / charWidth) newHeight := charHeight * (bounds.Dy() / charHeight) newPic := New(newWidth/charWidth, newHeight/charHeight) for x := 0; x < newWidth-charWidth; x += charWidth { for y := 0; y < newHeight-charHeight; y += charHeight { sum := 0 for i := x; i < x+charWidth; i++ { for j := y; j < y+charHeight; j++ { sum += int(oldPic.At(i, j).(color.Gray).Y) } } newPic.Set(x/charWidth, y/charHeight, uint8(sum/(charWidth*charHeight))) } } return newPic }
// kf3 is a generic convolution 3x3 kernel filter that operatates on // images of type image.Gray from the Go standard image library. func kf3(k *[9]float64, src, dst *image.Gray) { for y := src.Rect.Min.Y; y < src.Rect.Max.Y; y++ { for x := src.Rect.Min.X; x < src.Rect.Max.X; x++ { var sum float64 var i int for yo := y - 1; yo <= y+1; yo++ { for xo := x - 1; xo <= x+1; xo++ { if (image.Point{xo, yo}).In(src.Rect) { sum += k[i] * float64(src.At(xo, yo).(color.Gray).Y) } else { sum += k[i] * float64(src.At(x, y).(color.Gray).Y) } i++ } } dst.SetGray(x, y, color.Gray{uint8(math.Min(255, math.Max(0, sum)))}) } } }
func addPixelsToGray(src image.Image, sx, sy int, dst image.Gray, dx, dy int) { clr := src.At(sx, sy) greyColor, _ := color.GrayModel.Convert(clr).(color.Gray) dst.Set(dx, dy, color.Gray{dst.At(dx, dy).(color.Gray).Y + greyColor.Y}) }