Steps for OpenCV learning and development using golang

  • 2020-06-19 10:32:13
  • OfStack

preface

As time goes by, C++ gradually replaces C. iOS also has integrated lib which can use OpenCV. Now Python, go and other languages are developed.

The name OpenCV contains both Open and Computer Vision. In fact, Open refers to Open Source (open source) and Computer Vision refers to computer vision. The development of OpenCV has had an important impact on software development.

As an open digital image processing and computer vision software platform, OpenCV has the following characteristics:

Open C source code.

Based on Intel processor instruction set development of optimized code.

The structural and functional definitions of Series 1.

Powerful image and matrix computing capabilities.

Convenient and flexible user interface.

Supports both MSWindows and Linux platforms.

As an open source project for basic computer vision, image processing, and pattern recognition, OpenCV can be used directly in many areas and is ideal for secondary development.

The choice of the library

Use Opencv 2 +, you can use this library https: / / github com/go - opencv/go - opencv, but the library without support OpenCV 3 +, so want to use OpenCV 3 + can use https: / / github com/hybridgroup/gocv, mainly is the introduction to the use of gocv today.

Environment configuration

If OpenCV is not installed, Mac OS can be installed via brew install opencv 3.4.1.

After installed go, run directly go get u - d gocv. io x/gocv command to get gocv library, access to the library root directory cd $GOPATH/src/gocv io/x/gocv, running source. / env sh, You can then run the sample using the go run command, and modify the code to run the sample at the beginning.

The choice of IDE

Originally want to use IDEA + go plugin way to develop, who knows IDEA out of go ES1212en (need to charge), go plugin will stop maintenance, can only give up, then choose vscode+plugin way, convenient and fast, feel good, smart tips and code jump have, debugging has not tried, estimated debugging and run words can be configured.

The sample

Writing code configured after just can't wait, because of OpenCV gocv encapsulation, the method name generally 1, but still use some differences, but vscode code jump is very convenient, don't jump straight to the source code to see to know clearly, the following example of a lookup border, and see if C + + writing is not very 1 sample.


// What it does:
//
// This example uses the Window class to open an image file, and then display
// the image in a Window class.
//
// How to run:
//
// go run ./cmd/showimage/main.go /home/ron/Pictures/mcp23017.jpg
//
// +build example

package main

import (
 "fmt"
 "image"
 "image/color"
 "os"

 "gocv.io/x/gocv"
)

func main() {
 if len(os.Args) < 2 {
 fmt.Println("How to run:\n\tshowimage [imgfile]")
 return
 }

 filename := os.Args[1]
 window := gocv.NewWindow("Hello")
 img := gocv.IMRead(filename, gocv.IMReadColor)
 grayImage := gocv.NewMat()
 defer grayImage.Close()

 gocv.CvtColor(img, &grayImage, gocv.ColorBGRToGray)
 destImage := gocv.NewMat()
 gocv.Threshold(grayImage, &destImage, 100, 255, gocv.ThresholdBinaryInv)
 resultImage := gocv.NewMatWithSize(500, 400, gocv.MatTypeCV8U)

 gocv.Resize(destImage, &resultImage, image.Pt(resultImage.Rows(), resultImage.Cols()), 0, 0, gocv.InterpolationCubic)
 gocv.Dilate(resultImage, &resultImage, gocv.NewMat())
 gocv.GaussianBlur(resultImage, &resultImage, image.Pt(5, 5), 0, 0, gocv.BorderWrap)
 results := gocv.FindContours(resultImage, gocv.RetrievalTree, gocv.ChainApproxSimple)
 imageForShowing := gocv.NewMatWithSize(resultImage.Rows(), resultImage.Cols(), gocv.MatChannels4)
 for index, element := range results {
 fmt.Println(index)
 gocv.DrawContours(&imageForShowing, results, index, color.RGBA{R: 0, G: 0, B: 255, A: 255}, 1)
 gocv.Rectangle(&imageForShowing,
 gocv.BoundingRect(element),
 color.RGBA{R: 0, G: 255, B: 0, A: 100}, 1)
 }

 if img.Empty() {
 fmt.Println("Error reading image from: %v", filename)
 return
 }

 for {
 window.IMShow(imageForShowing)
 if window.WaitKey(1) >= 0 {
 break
 }
 }
}

I used the example showimage. go and the processing order is:

Gray turn CvtColor 2 threshold Threshold Zoom out image Resize Inflation Dilate Gaussian blur GaussianBlur Find the contour FindContours Draw the contour DrawContours Draws the smallest enclosing rectangle Rectangle of the outline

As you can see from the above example, the methods are all under package gocv, and there are code hints when using them, including parameters. Since go has no class constructor, it USES New+ type names and other functions to create types.

I am also a novice to go, here is the code while looking at the documentation, fortunately there is a hint of smart code, it is quite handy to use, I hope this introductory article to go to use OpenCV development students help.

Conclusion:


Related articles: