Golang 二值化图片,并读取某个色块的坐标

package main

import (
"fmt"
"github.com/Comdex/imgo"
"os"
"strings"
)

func main() {
if len(os.Args) <= 1 {
fmt.Println(`需要指定一个本地的图片文件`)
return
}

img := imgo.MustRead(os.Args[1])
text := ""

for i := 0; i < len(img); i++ {
w := img[i]
for ii := 0; ii < len(w); ii++ {
//根据色块的 RGBA 色值,进行灰度二值化
//纯绿色块是我们要标记的值
r := "0"
if w[ii][0] > 125 {
r = "1"
}

text += r
}

text += "\n"
}

//过滤杂色
text = strings.Replace(text, "10", "11", -1)
text = strings.Replace(text, "01", "11", -1)

if len(os.Args) >= 3 {
fmt.Println(text)
}

x := 50
y := 50
result := "fail"

newImg := strings.Split(text, "\n")
for i := 0; i < len(newImg); i++ {
//读取绿色块的连续值
//如果做得严格一点,需要做转置
l := len(newImg[i])
if l > 0 && strings.Count(newImg[i], "0")*4 >= len(newImg[i]) {
//不能位于边界
if i+6 > l {
y = i + 3
} else {
y = i + 6 //字符高度不变,就写死增加两个像素的偏移
}

//查找连续色块
block := "00000000"

s := strings.IndexAny(newImg[i], block)
e := strings.LastIndexAny(newImg[i], block)

x = s + (e-s+4)/2

result = "ok"
break
}
}

fmt.Printf(`{"result":"%s", "x":"%d", "y":"%d"}`, result, x, y)
fmt.Println()
}

golang处理无限嵌套json

{
"took": 596,
"timed_out": false,
"_shards": {
"total": 11,
"successful": 11,
"failed": 0
},
"hits": {
"total": 1121497,
"max_score": 0,
"hits": []
},
"aggregations": {
"day.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "20170418",
"doc_count": 1121497,
"channel.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "channel01",
"doc_count": 901649,
"acttype.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "show",
"doc_count": 424711,
"ct": {
"value": 143760
}
},
{
"key": "click",
"doc_count": 253006,
"ct": {
"value": 114883
}
},
{
"key": "install",
"doc_count": 139527,
"ct": {
"value": 68115
}
},
{
"key": "installed",
"doc_count": 84405,
"ct": {
"value": 49037
}
}
]
}
},
{
"key": "channel02",
"doc_count": 107639,
"acttype.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "show",
"doc_count": 50364,
"ct": {
"value": 17019
}
},
{
"key": "click",
"doc_count": 32334,
"ct": {
"value": 14123
}
},
{
"key": "install",
"doc_count": 19891,
"ct": {
"value": 9259
}
},
{
"key": "installed",
"doc_count": 5050,
"ct": {
"value": 2922
}
}
]
}
},
{
"key": "channel03",
"doc_count": 69671,
"acttype.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "show",
"doc_count": 26617,
"ct": {
"value": 8229
}
},
{
"key": "click",
"doc_count": 22793,
"ct": {
"value": 7812
}
},
{
"key": "install",
"doc_count": 19919,
"ct": {
"value": 6165
}
},
{
"key": "installed",
"doc_count": 342,
"ct": {
"value": 290
}
}
]
}
},
{
"key": "channel04",
"doc_count": 42511,
"acttype.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "show",
"doc_count": 22565,
"ct": {
"value": 8044
}
},
{
"key": "click",
"doc_count": 11601,
"ct": {
"value": 5890
}
},
{
"key": "install",
"doc_count": 7208,
"ct": {
"value": 3802
}
},
{
"key": "installed",
"doc_count": 1137,
"ct": {
"value": 761
}
}
]
}
},
{
"key": "channel05",
"doc_count": 27,
"acttype.raw": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "show",
"doc_count": 20,
"ct": {
"value": 7
}
},
{
"key": "click",
"doc_count": 7,
"ct": {
"value": 2
}
}
]
}
}
]
}
}
]
}
}
}