Initial commit
154
main.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
_ "image/jpeg"
|
||||
)
|
||||
|
||||
func max16(a, b uint16) uint16 {
|
||||
if a > b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func max32(a, b uint32) uint32 {
|
||||
if a > b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func maxDiff(a, d1, d2 uint32) uint32 {
|
||||
var d uint32
|
||||
if d1 > d2 {
|
||||
d = d1 - d2
|
||||
} else {
|
||||
d = d2 - d1
|
||||
}
|
||||
|
||||
return max32(a, d)
|
||||
}
|
||||
|
||||
func detectEdges(src image.Image) *image.Gray16 {
|
||||
bounds := src.Bounds()
|
||||
ret := image.NewGray16(bounds)
|
||||
|
||||
for y := bounds.Min.Y + 1; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X + 1; x < bounds.Max.X; x++ {
|
||||
inr, ing, inb, _ := src.At(x, y).RGBA()
|
||||
tempr, tempg, tempb, _ := src.At(x - 1, y - 1).RGBA()
|
||||
|
||||
var c uint32
|
||||
c = maxDiff(c, inr, tempr)
|
||||
c = maxDiff(c, ing, tempg)
|
||||
c = maxDiff(c, inb, tempb)
|
||||
|
||||
outc := color.Gray16{Y: uint16(c)}
|
||||
ret.Set(x, y, outc)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func loadImage(path string) (image.Image, error) {
|
||||
fh, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fh.Close()
|
||||
|
||||
img, _, err := image.Decode(fh)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func normalize(src *image.Gray16) *image.Gray16 {
|
||||
bounds := src.Bounds()
|
||||
var m uint16
|
||||
|
||||
for y := bounds.Min.Y + 1; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X + 1; x < bounds.Max.X; x++ {
|
||||
m = max16(m, src.Gray16At(x, y).Y)
|
||||
}
|
||||
}
|
||||
|
||||
scale := math.MaxUint16 / m
|
||||
ret := image.NewGray16(bounds)
|
||||
|
||||
for y := bounds.Min.Y + 1; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X + 1; x < bounds.Max.X; x++ {
|
||||
c := src.Gray16At(x, y)
|
||||
c.Y *= scale
|
||||
ret.Set(x, y, c)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func find(shape, img *image.Gray16) {
|
||||
sb := shape.Bounds()
|
||||
imgb := img.Bounds()
|
||||
|
||||
for y := imgb.Min.Y; y < imgb.Max.Y - (sb.Max.Y - sb.Min.Y); y++ {
|
||||
for x := imgb.Min.X; x < imgb.Max.X - (sb.Max.X - sb.Min.X); x++ {
|
||||
score(shape, img, x, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func score(shape, img *image.Gray16, ox, oy int) {
|
||||
sb := shape.Bounds()
|
||||
imgb := img.Bounds()
|
||||
|
||||
for y := sb.Min.Y; y < sb.Max.Y; y++ {
|
||||
for x := sb.Min.X; x < sb.Max.X; x++ {
|
||||
// imgy := oy + y
|
||||
//
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Add("Content-Type", "image/png")
|
||||
|
||||
t, err := loadImage("shapes/T.png")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
te := detectEdges(t)
|
||||
|
||||
src, err := loadImage("samples/big.png")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
srce := detectEdges(src)
|
||||
|
||||
find(te, srce)
|
||||
|
||||
err = png.Encode(w, srce)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Printf("starting...\n")
|
||||
http.HandleFunc("/test", test)
|
||||
http.ListenAndServe(":8090", nil)
|
||||
}
|
||||
BIN
samples/Alabama.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Alaska.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Arizona.jpg
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
samples/Arkansas.jpg
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
samples/California.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Colorado.jpg
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
samples/Connecticut.jpg
Normal file
|
After Width: | Height: | Size: 9.7 KiB |
BIN
samples/Delaware.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/DistrictOfColumbia.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Florida.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Georgia.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Guam.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/Hawaii.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Idaho.jpg
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
samples/Illinois.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Indiana.jpg
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
samples/Iowa.jpg
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
samples/Kansas.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Kentucky.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Louisiana.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Maine.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/Maryland.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Massachusetts.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Michigan.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/Minnesota.jpg
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
samples/Mississippi.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Missouri.jpg
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
samples/Montana.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Nebraska.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Nevada.jpg
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
samples/NewHampshire.jpg
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
samples/NewJersey.jpg
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
samples/NewMexico.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/NewYork.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/NorthCarolina.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/NorthDakota.jpg
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
samples/Ohio.jpg
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
samples/Oklahoma.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/Oregon.jpg
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
samples/Pennsylvania.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/PuertoRico.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/RhodeIsland.jpg
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
samples/SouthCarolina.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/SouthDakota.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/Tennessee.jpg
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
samples/Texas.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Utah.jpg
Normal file
|
After Width: | Height: | Size: 16 KiB |
BIN
samples/Vermont.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Virginia.jpg
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
samples/Washington.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/WestVirginia.jpg
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
samples/Wisconsin.jpg
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
samples/Wyoming.jpg
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
samples/big.png
Normal file
|
After Width: | Height: | Size: 1.1 MiB |
BIN
shapes/T.png
Normal file
|
After Width: | Height: | Size: 947 B |