gocv
gocv copied to clipboard
SetUCharAt3 segmentation violation
When trying to set color value using SetUCharAt3 on channel 2 pixels a segmentation violation occurs. This happens irrespective of the method used to create the destination Mat (in my example below b). If instead of creating a MAT clone or copy I modify the original Mat (a) then it works fine. Might be related to #583
` func main() { a := gocv.IMRead("image.jpg", gocv.IMReadAnyColor) defer a.Close() colorType := a.Type() fmt.Println("color type for image: " + colorType.String())
fmt.Println("rows: " + strconv.Itoa(a.Rows()))
fmt.Println("columns: " + strconv.Itoa(a.Cols()))
// tb := make([]int, 256)
// create color reduction index table
// for i := 0; i < 256; i++ {
// tb[i] = i / 10
// tb[i] *= 10
// fmt.Println(tb[i])
// }
fmt.Printf("number of channels in image %v\n", a.Channels())
b := a.Clone()
defer b.Close()
fmt.Println("color type for image: " + b.Type().String())
fmt.Println("rows: " + strconv.Itoa(b.Rows()))
fmt.Println("columns: " + strconv.Itoa(b.Cols()))
for channel := 0; channel < 3; channel++ {
for i := 0; i < a.Cols(); i++ {
for j := 0; j < a.Rows(); j++ {
color := a.GetUCharAt3(i, j, channel)
fmt.Printf("%d,%d,%d,%d\n", i, j, channel, color)
// color -= 5
// color *= 10
b.SetUCharAt3(i, j, channel, color)
}
}
}
w := gocv.NewWindow("hello")
defer w.Close()
w2 := gocv.NewWindow("hello2")
defer w2.Close()
w.IMShow(a)
w2.IMShow(b)
for {
if w.WaitKey(0) == 13 || w2.WaitKey(0) == 13 {
fmt.Println("return key pressed")
break
}
}
// returns the sum of the diagonal elements of the matrix mtx
// fmt.Printf("%+v", gocv.Trace(a))
} `
Here's the error:
fatal error: unexpected signal during runtime execution [signal SIGSEGV: segmentation violation code=0x1 addr=0x7f2249c2e670 pc=0x4b4208]
runtime stack: runtime.throw(0x50447d, 0x2a) /usr/local/go/src/runtime/panic.go:774 +0x72 runtime.sigpanic() /usr/local/go/src/runtime/signal_unix.go:378 +0x47c
goroutine 1 [syscall]: runtime.cgocall(0x4af210, 0xc000044c58, 0x4a0be1) /usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc000044c28 sp=0xc000044bf0 pc=0x41cb6b gocv.io/x/gocv._Cfunc_Mat_SetUChar3(0x1c20290, 0x0, 0x1) _cgo_gotypes.go:4166 +0x45 fp=0xc000044c58 sp=0xc000044c28 pc=0x4abd65 gocv.io/x/gocv.(*Mat).SetUCharAt3.func1(0xc000044e70, 0x0, 0x0, 0x1, 0x0) /home/ramf/dev/go/src/gocv.io/x/gocv/core.go:620 +0x82 fp=0xc000044c98 sp=0xc000044c58 pc=0x4acc32 gocv.io/x/gocv.(*Mat).SetUCharAt3(0xc000044e70, 0x0, 0x0, 0x1, 0xc000044f00) /home/ramf/dev/go/src/gocv.io/x/gocv/core.go:620 +0x52 fp=0xc000044cd0 sp=0xc000044c98 pc=0x4ac442 main.main() /home/ramf/dev/go/src/opencv/color_reduction/main.go:42 +0x836 fp=0xc000044f60 sp=0xc000044cd0 pc=0x4adb46 runtime.main() /usr/local/go/src/runtime/proc.go:203 +0x21e fp=0xc000044fe0 sp=0xc000044f60 pc=0x44563e runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:1357 +0x1 fp=0xc000044fe8 sp=0xc000044fe0 pc=0x46dcc1 exit status 2
Your Environment
- Operating System and version: ubuntu 18.04
- OpenCV version used: 4.20
- How did you install OpenCV?
- GoCV version used: 0.22.0
- Go version: 1.13.5
- Did you run the
env.sh
orenv.cmd
script before trying togo run
orgo build
? yes
So if I modify the original Mat (a) instead of the copy/clone (b) then it works. So it seems the problem is in the Mat copy/clone. Any ideas?
Also fails if I create destination the Mat with the same size and type
Hey. OP from #583 here. I would just recommend using the SetUCharAt function and calculating the indices manually. SetUCharAt3 is not intended for channel wise access at the moment from what I could see in the code.
@rodneymo have you try to change
b.SetUCharAt3(i, j, channel, color)
to
b.SetUCharAt3(j, i, channel, color)
?