dongzong8110 2017-05-14 02:39
浏览 240
已采纳

使用golang的drawmask和draw.Over产生图像故障

I am rewriting with golang a script I originally wrote in python. The function I am trying to recreate currently takes one image (a coloured overlay) and pastes it on a background (a map) of the same size, with 50% transparency. The desired output, as produced by my python script, is:

Desired Output with Python

The golang code I have written to try to replicate this in golang, with reference to this stackoverflow thread.

In main.go

// overlayImg is type image.Image, consists of the coloured overlay
overlayImg = lib.AddMap(overlayImg)
// lib.SaveToRecords is a function that saves image.Image overlayImg to path specified by string download.FileName
lib.SaveToRecords(overlayImg, download.FileName)

In package myproject/lib

func AddMap(i image.Image) image.Image{
    // mapFile is the background map image
    mapFile, err := os.Open("./res/map.png")
    if err != nil { panic(err) }
    mapImg, _, err := image.Decode(mapFile)
    if err != nil { panic(err) }
    mask := image.NewUniform(color.Alpha{128})

    canvas := image.NewRGBA(mapImg.Bounds())
    draw.Draw(canvas, canvas.Bounds(), mapImg, image.Point{0, 0}, draw.Src)

    draw.DrawMask(canvas, canvas.Bounds(), i, image.Point{0, 0}, mask, image.Point{0, 0}, draw.Over)

    return canvas
}

However, the resulting image is produced with a sort of 'glitch' in parts of the coloured overlay. This colour glitch is reliably reproduced on each run of the script, even with different overlays (the map stays the same throughout, of course).

Output with Golang

How do I get rid of the 'glitch'?


Things I have tried:

  • Using draw.Draw in place of draw.DrawMask with draw.Over as the 'op' setting. Results in same colour 'glitch', but without transparency.

draw.Draw with draw.Over

  • Using draw.DrawMask, except with draw.Src as the 'op' setting. Result:

draw.DrawMask with draw.Src

  • Using draw.Draw in place of draw.DrawMask, and with draw.Src as the 'op' setting. This was with a slightly different overlay image. Result:

draw.Draw with draw.Src


Update

I've tried assigning mask := image.NewUniform(color.Alpha16{32767}) in place of mask := image.NewUniform(color.Alpha{128}) according to putu's comment. Glitch still appears. Additionally, this glitch is not as consistent as I thought, showing up at only roughly 10% of the time. It seems the glitch shows up depending on the content of the image pasted unto the background.

drawMask with draw.Over and Alpha16


Update 2

Originally, mapImg was of type *image.NRGBA, i and canvas of type *image.RGBA. Once again following the advice in the comments, I converted mapImg to type *image.RGBA to match with the rest. To do this, I used the following few lines of code:

mapImg, _, err := image.Decode(mapFile)
mapImgRGBA := image.NewRGBA(mapImg.Bounds())
draw.Draw(mapImgRGBA, mapImgRGBA.Bounds(), mapImg, image.Point{0, 0}, draw.Src)
// Use mapImgRGBA in place of mapImg from here

Still don't work:

Converting mapImg to RGBA

  • 写回答

1条回答 默认 最新

  • duanhuan5409 2017-05-15 03:38
    关注

    Thanks to use noethics in the freenode #go-nuts channel for requesting a look at the original overlay, I found the issue while generating the output after each line that manipulates the image.

    The issue is not with "image/draw", but with an external library I'm using to resize the overlay to the dimensions of the map, "github.com/nfnt/resize".

    The overlay before resizing:

    Original image

    The overlay after resizing (red & black pixels undesirable):

    return resize.Resize(1491, 836, i, resize.Lanczos3)


    Update

    It's not even an issue with "github.com/nfnt/resize". I was simply using the wrong interpolation algorithm. Turns out bilinear gives me the best output for the image I am working with. I suspect it's also the one used in the library I used to write my original script in.

    interpolationComparison

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私