Displaying the image 

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 ...

Get Go Machine Learning Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.