Fixing pixel to image conversion

This commit is contained in:
Endre Simo
2019-05-15 16:43:01 +03:00
parent eabc8cdbae
commit a2974e2133
2 changed files with 25 additions and 13 deletions

View File

@@ -10,7 +10,7 @@ os.system('go build -o pigo.so -buildmode=c-shared pigo.go')
pigo = cdll.LoadLibrary('./pigo.so') pigo = cdll.LoadLibrary('./pigo.so')
os.system('rm pigo.so') os.system('rm pigo.so')
max_buff_len = 32000 max_buff_len = 25000
# define class MapGoMethod to map to: # define class MapGoMethod to map to:
# C type struct { void *data; GoInt len; GoInt cap; } # C type struct { void *data; GoInt len; GoInt cap; }

View File

@@ -4,6 +4,7 @@ import "C"
import ( import (
"image" "image"
"image/color"
"io/ioutil" "io/ioutil"
"log" "log"
"runtime" "runtime"
@@ -40,12 +41,12 @@ func FindFaces(pixels []uint8) uintptr {
proc := &triangle.Processor{ proc := &triangle.Processor{
BlurRadius: 1, BlurRadius: 1,
SobelThreshold: 2, SobelThreshold: 2,
PointsThreshold: 5, PointsThreshold: 2,
MaxPoints: 50, MaxPoints: 180,
Wireframe: 1, Wireframe: 1,
Noise: 0, Noise: 0,
StrokeWidth: 1, StrokeWidth: 2,
IsSolid: false, IsSolid: true,
Grayscale: false, Grayscale: false,
OutputToSVG: false, OutputToSVG: false,
OutputInWeb: false, OutputInWeb: false,
@@ -54,9 +55,10 @@ func FindFaces(pixels []uint8) uintptr {
pointCh := make(chan uintptr) pointCh := make(chan uintptr)
go func() { go func() {
dets := px.clusterDetection(pixels)
img := px.pixToImage(pixels) img := px.pixToImage(pixels)
grayscale := pigo.RgbToGrayscale(img.(*image.NRGBA))
dets := px.clusterDetection(grayscale)
tFaces := make([][]int, len(dets)) tFaces := make([][]int, len(dets))
totalPixDim := 0 totalPixDim := 0
@@ -76,7 +78,6 @@ func FindFaces(pixels []uint8) uintptr {
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
triPix := px.imgToPix(res) triPix := px.imgToPix(res)
tFaces[i] = append(tFaces[i], triPix...) tFaces[i] = append(tFaces[i], triPix...)
@@ -105,6 +106,7 @@ func FindFaces(pixels []uint8) uintptr {
// Append the generated triangle slices to the detected faces array. // Append the generated triangle slices to the detected faces array.
result = append(result, convTri...) result = append(result, convTri...)
// Convert the slice into an array pointer. // Convert the slice into an array pointer.
s := *(*[]uint8)(unsafe.Pointer(&result)) s := *(*[]uint8)(unsafe.Pointer(&result))
p := uintptr(unsafe.Pointer(&s[0])) p := uintptr(unsafe.Pointer(&s[0]))
@@ -161,12 +163,22 @@ func (px pixs) clusterDetection(pixels []uint8) []pigo.Detection {
// pixToImage converts the pixel array to an image. // pixToImage converts the pixel array to an image.
func (px pixs) pixToImage(pixels []uint8) image.Image { func (px pixs) pixToImage(pixels []uint8) image.Image {
width, height := px.cols, px.rows width, height := px.cols, px.rows
img := image.NewRGBA(image.Rect(0, 0, width, height)) img := image.NewNRGBA(image.Rect(0, 0, width, height))
for x := 0; x < len(pixels); x += 4 { c := color.NRGBA{
img.Pix[x+0] = uint8(pixels[x+0]) R: uint8(0),
img.Pix[x+1] = uint8(pixels[x+1]) G: uint8(0),
img.Pix[x+2] = uint8(pixels[x+2]) B: uint8(0),
img.Pix[x+3] = uint8(pixels[x+3]) A: uint8(255),
}
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X*3; x += 3 {
c.R = uint8(pixels[x+(y*width*3)+0])
c.G = uint8(pixels[x+(y*width*3)+1])
c.B = uint8(pixels[x+(y*width*3)+2])
img.SetNRGBA(int(x/3), y, c)
}
} }
return img return img
} }