本文整理匯總了Golang中image.NewGray函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewGray函數的具體用法?Golang NewGray怎麽用?Golang NewGray使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewGray函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestAdjustGamma
func TestAdjustGamma(t *testing.T) {
src := image.NewGray(image.Rect(0, 0, 256, 1))
dst := image.NewGray(image.Rect(0, 0, 256, 1))
for i := 0; i <= 255; i++ {
src.Pix[i] = uint8(i)
}
ag := Gamma(2.0)
ag.Draw(dst, src, nil)
for i := 100; i <= 150; i++ {
if dst.Pix[i] <= src.Pix[i] {
t.Errorf("Gamma unexpected color")
}
}
ag = Gamma(0.5)
ag.Draw(dst, src, nil)
for i := 100; i <= 150; i++ {
if dst.Pix[i] >= src.Pix[i] {
t.Errorf("Gamma unexpected color")
}
}
ag = Gamma(1.0)
ag.Draw(dst, src, nil)
for i := 100; i <= 150; i++ {
if dst.Pix[i] != src.Pix[i] {
t.Errorf("Gamma unexpected color")
}
}
}
示例2: DrawPicture
// DrawPicture .
func DrawPicture(src image.Image, width int, height int) *image.Gray {
rect := image.Rect(0, 0, width, height)
img := image.NewGray(rect)
rander := rander()
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
var c color.Gray
if IsBlack(src, x, y) {
if y > 1 && IsNotBlack(src, x, y-1) {
c = borderColor(rander)
} else {
c = insideColor(rander)
}
} else {
c = outsideColor(rander)
}
x1, y1 := transformCurve(x, y, width, height)
//c = color.Gray{c.Y + ScaledLuminanceAt(90, src, x, y)}
img.Set(x1, y1, c)
}
}
g := gift.New(
//gift.GaussianBlur(2),
)
img2 := image.NewGray(rect)
g.Draw(img2, img)
return img2
}
示例3: MakeComputeMaker
func MakeComputeMaker(src image.Image) *ComMaker {
b := src.Bounds()
sz := b.Size()
width := sz.X / 8
height := sz.Y / 8
smallB := image.Rect(0, 0, width, height)
cm := ComMaker{
width: sz.X / 8,
height: sz.Y / 8,
bound: smallB,
}
// Make Grey Img
gSrc := image.NewGray(b)
draw.Draw(gSrc, b, src, image.ZP, draw.Src)
// Make Smaller
gDst := image.NewGray(smallB)
// Prep Conversion
cfg, err := rez.PrepareConversion(gDst, gSrc)
if err != nil {
log.Println(err)
return nil
}
cm.rConv, err = rez.NewConverter(cfg, rez.NewBilinearFilter())
if err != nil {
log.Println(err)
return nil
}
return &cm
}
示例4: TestEdgeOps
func TestEdgeOps(t *testing.T) {
src, err := loadImage("../../testdata/gopher-100x150.png")
if err != nil {
t.Fatal(err)
}
for _, ot := range opTests {
mag := image.NewGray(src.Bounds())
dir := image.NewGray(src.Bounds())
if err := ot.fn(mag, dir, src); err != nil {
t.Fatalf("%s: %v", ot.name, err)
}
magFile := fmt.Sprintf("../../testdata/%s-mag.png", ot.name)
cmp, err := loadImage(magFile)
if err != nil {
t.Fatalf("%s mag: %v", ot.name, err)
}
if err = imageWithinTolerance(mag, cmp, 0x101); err != nil {
t.Fatalf("%s mag: %v", ot.name, err)
}
dirFile := fmt.Sprintf("../../testdata/%s-dir.png", ot.name)
cmp, err = loadImage(dirFile)
if err != nil {
t.Fatalf("%s dir: %v", ot.name, err)
}
if err = imageWithinTolerance(dir, cmp, 0x101); err != nil {
t.Fatalf("%s dir: %v", ot.name, err)
}
}
}
示例5: ToComputeImageManual
func ToComputeImageManual(src image.Image) *image.Gray {
fullBound := src.Bounds()
m := image.NewGray(fullBound)
draw.Draw(m, fullBound, src, image.ZP, draw.Src)
// Smaller Image
smallBound := image.Rect(0, 0, fullBound.Dx()/4, fullBound.Dy()/4)
smallImg := image.NewGray(smallBound)
swid := smallBound.Dx()
wid := fullBound.Dx()
for o, o2, omax := 0, 0, len(smallImg.Pix); o < omax; o, o2 = o+1, o2+4 {
if (o % swid) == 0 {
o2 = (o / swid) * wid * 4
}
smallImg.Pix[o] = uint8((int(m.Pix[o2+0]) +
int(m.Pix[o2+3]) +
int(m.Pix[o2+2+wid]) +
int(m.Pix[o2+1+wid*2]) +
int(m.Pix[o2+0+wid*3]) +
int(m.Pix[o2+3+wid*3])) / 6)
}
return smallImg
}
示例6: main
func main() {
flag.Parse()
reader, err := os.Open(*in)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
i, _, err := image.Decode(reader)
if err != nil {
log.Fatal(err)
}
b := i.Bounds()
m := image.NewGray(b) // monochrome
draw.Draw(m, b, i, b.Min, draw.Src)
o := image.NewGray(image.Rect(0, 0, 256, 256))
white := color.RGBA{0xff, 0xff, 0xff, 0xff}
black := color.RGBA{0x00, 0x00, 0x00, 0xff}
draw.Draw(o, o.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
delta := []int{-16, -1, 1, 16}
ud := []int{-6, 0, 0, 6}
lr := []int{0, -6, 6, 0}
for j := 0; j < 16; j++ {
for i := 0; i < 16; i++ {
cx := int(19 + 20.5333*float64(i))
cy := int(19 + 20.5333*float64(j))
s := []uint32{0, 0, 0, 0}
best := uint32(50000)
bestK := 0
for k := 0; k < 4; k++ {
for sx := -1; sx < 2; sx++ {
for sy := -1; sy < 2; sy++ {
r, g, b, _ := m.At(cx+lr[k]+sx, cy+ud[k]+sy).RGBA()
s[k] += (r + g + b) / 3
}
}
if k == 0 || s[k] < best {
best = s[k]
bestK = k
}
}
src := 16*j + i
dst := (src + delta[bestK] + 256) % 256
fmt.Printf("%v\n", dst)
o.Set(src, dst, black)
}
}
writer, err := os.Create(*out)
if err != nil {
log.Fatal(err)
}
png.Encode(writer, o)
writer.Close()
}
示例7: TestRotate270
func TestRotate270(t *testing.T) {
img0 := image.NewGray(image.Rect(-1, -1, 3, 1))
img0.Pix = []uint8{
1, 2, 3, 4,
5, 6, 7, 8,
}
img1Exp := image.NewGray(image.Rect(0, 0, 2, 4))
img1Exp.Pix = []uint8{
5, 1,
6, 2,
7, 3,
8, 4,
}
f := Rotate270()
img1 := image.NewGray(f.Bounds(img0.Bounds()))
f.Draw(img1, img0, nil)
if img1.Bounds().Size() != img1Exp.Bounds().Size() {
t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size())
}
if !comparePix(img1Exp.Pix, img1.Pix) {
t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix)
}
}
示例8: Canny
// Canny detects and returns edges from the given image.
// Each dst pixel is given one of three values:
// 0xff: an edge
// 0x80: possibly an edge
// 0x00: not an edge
func Canny(dst *image.Gray, src image.Image) error {
if dst == nil {
return errors.New("edge: dst is nil")
}
if src == nil {
return errors.New("edge: src is nil")
}
b := src.Bounds()
srcGray, ok := src.(*image.Gray)
if !ok {
srcGray = image.NewGray(b)
draw.Draw(srcGray, b, src, b.Min, draw.Src)
}
if err := graphics.Blur(srcGray, srcGray, nil); err != nil {
return err
}
mag, dir := image.NewGray(b), image.NewGray(b)
if err := Sobel(mag, dir, srcGray); err != nil {
return err
}
// Non-maximum supression.
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
d := dir.Pix[(y-b.Min.Y)*dir.Stride+(x-b.Min.X)*1]
var m0, m1 uint8
switch d {
case 0: // west and east
m0 = atOrZero(mag, x-1, y)
m1 = atOrZero(mag, x+1, y)
case 45: // north-east and south-west
m0 = atOrZero(mag, x+1, y-1)
m1 = atOrZero(mag, x-1, y+1)
case 90: // north and south
m0 = atOrZero(mag, x, y-1)
m1 = atOrZero(mag, x, y+1)
case 135: // north-west and south-east
m0 = atOrZero(mag, x-1, y-1)
m1 = atOrZero(mag, x+1, y+1)
default:
return fmt.Errorf("edge: bad direction (%d, %d): %d", x, y, d)
}
m := mag.Pix[(y-b.Min.Y)*mag.Stride+(x-b.Min.X)*1]
if m > m0 && m > m1 {
m = 0xff
} else if m > m0 || m > m1 {
m = 0x80
} else {
m = 0x00
}
dst.Pix[(y-b.Min.Y)*dst.Stride+(x-b.Min.X)*1] = m
}
}
return nil
}
示例9: diffOp
func diffOp(mag, dir *image.Gray, src image.Image, opX, opY *convolve.SeparableKernel) error {
if src == nil {
return errors.New("graphics: src is nil")
}
b := src.Bounds()
srcg, ok := src.(*image.Gray)
if !ok {
srcg = image.NewGray(b)
draw.Draw(srcg, b, src, b.Min, draw.Src)
}
mx := image.NewGray(b)
if err := convolve.Convolve(mx, srcg, opX); err != nil {
return err
}
my := image.NewGray(b)
if err := convolve.Convolve(my, srcg, opY); err != nil {
return err
}
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
off := (y-mx.Rect.Min.Y)*mx.Stride + (x-mx.Rect.Min.X)*1
cx := float64(mx.Pix[off])
cy := float64(my.Pix[off])
if mag != nil {
off = (y-mag.Rect.Min.Y)*mag.Stride + (x-mag.Rect.Min.X)*1
mag.Pix[off] = uint8(math.Sqrt(cx*cx + cy*cy))
}
if dir != nil {
off = (y-dir.Rect.Min.Y)*dir.Stride + (x-dir.Rect.Min.X)*1
angle := math.Atan(cy / cx)
// Round the angle to 0, 45, 90, or 135 degrees.
angle = math.Mod(angle, 2*math.Pi)
var degree uint8
if angle <= math.Pi/8 {
degree = 0
} else if angle <= math.Pi*3/8 {
degree = 45
} else if angle <= math.Pi*5/8 {
degree = 90
} else if angle <= math.Pi*7/8 {
degree = 135
} else {
degree = 0
}
dir.Pix[off] = degree
}
}
}
return nil
}
示例10: Convert
func (cm *ComMaker) Convert(src image.Image) *image.Gray {
// Make Grey Img
b := src.Bounds()
gSrc := image.NewGray(b)
draw.Draw(gSrc, b, src, image.ZP, draw.Src)
// Make Smaller
gDst := image.NewGray(cm.bound)
cm.rConv.Convert(gDst, gSrc)
return gDst
}
示例11: TestGrayPlanes
func TestGrayPlanes(t *testing.T) {
w, h := 256, 256
src := readImage(t, "testdata/gray.png")
ref := image.NewGray(src.Bounds())
err := Convert(ref, src, nil)
expect(t, err, nil)
raw := image.NewGray(image.Rect(0, 0, w*2, h*2))
dst := raw.SubImage(image.Rect(7, 7, 7+w, 7+h))
err = Convert(dst, src, NewBicubicFilter())
expect(t, err, nil)
err = Convert(src, dst, NewBicubicFilter())
expect(t, err, nil)
checkPsnrs(t, ref, src, image.Rectangle{}, []float64{38})
}
示例12: TestSobel
func TestSobel(t *testing.T) {
testData := []struct {
desc string
srcb, dstb image.Rectangle
srcPix, dstPix []uint8
}{
{
"sobel 0x0",
image.Rect(0, 0, 0, 0),
image.Rect(0, 0, 0, 0),
[]uint8{},
[]uint8{},
},
{
"sobel 6x6",
image.Rect(-1, -1, 5, 5),
image.Rect(0, 0, 6, 6),
[]uint8{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x99, 0x99, 0x99, 0x99,
0x00, 0x00, 0x99, 0x99, 0x99, 0x99,
0x00, 0x00, 0x99, 0x99, 0x99, 0x99,
0x00, 0x00, 0x99, 0x99, 0x99, 0x99,
},
[]uint8{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xd8, 0xff, 0xff, 0xff, 0xff,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
},
},
}
for _, d := range testData {
src := image.NewGray(d.srcb)
src.Pix = d.srcPix
f := Sobel()
dst := image.NewGray(f.Bounds(src.Bounds()))
f.Draw(dst, src, nil)
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) {
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix)
}
}
}
示例13: TestInvert
func TestInvert(t *testing.T) {
src := image.NewGray(image.Rect(0, 0, 256, 1))
for i := 0; i <= 255; i++ {
src.Pix[i] = uint8(i)
}
g := New(Invert())
dst := image.NewGray(g.Bounds(src.Bounds()))
g.Draw(dst, src)
for i := 0; i <= 255; i++ {
if dst.Pix[i] != 255-src.Pix[i] {
t.Errorf("InvertColors: index %d: expected %d got %d", i, 255-src.Pix[i], dst.Pix[i])
}
}
}
示例14: newIntegrals
// newIntegrals returns the integral and the squared integral.
func newIntegrals(src image.Image) (*integral, *integral) {
b := src.Bounds()
srcg, ok := src.(*image.Gray)
if !ok {
srcg = image.NewGray(b)
draw.Draw(srcg, b, src, b.Min, draw.Src)
}
m := integral{
pix: make([]uint64, b.Max.Y*b.Max.X),
stride: b.Max.X,
rect: b,
}
mSq := integral{
pix: make([]uint64, b.Max.Y*b.Max.X),
stride: b.Max.X,
rect: b,
}
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
os := (y-b.Min.Y)*srcg.Stride + x - b.Min.X
om := (y-b.Min.Y)*m.stride + x - b.Min.X
c := uint64(srcg.Pix[os])
m.pix[om] = c
mSq.pix[om] = c * c
}
}
m.integrate()
mSq.integrate()
return &m, &mSq
}
示例15: BenchmarkFractalGeneration
// from fib_test.go
func BenchmarkFractalGeneration(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
im := image.NewGray(image.Rectangle{image.Point{0, 0}, image.Point{1000, 1000}})
GenerateFractal(im)
}
}