So, the image has been magically read into the matrix. How do we get anything out of it?
The answer is that we have to copy the data from the data structure controlled by OpenCV into a Go-native data structure. Fortunately, GoCV handles that as well. Here, we write it out to a file:
goImg, err := img.ToImage() if err != nil { log.Fatal(err) } outFile, err := os.OpenFile("first.png", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err != nil { log.Fatal(err) } png.Encode(outFile, goImg)
First, the matrix has to be converted to image.Image. To do that, img.ToImage() is called. Then, it is encoded as a PNG by using png.Encode.
And you will have a test image. This was mine:
In the picture, I'm holding a box with a photo of ...