SoFunction
Updated on 2025-03-04

Steps to learn and develop OpenCV with golang

Preface

I remember when I started using OpenCV in college, I used C language at that time. The OpenCV version seemed to be 1.1. As time goes by, C++ gradually replaced C later. iOS also has integrated lib that can be used to use OpenCV. Now Python, go and other languages ​​are developed. Today, I mainly talk about how to use the go language to configure the development of OpenCV.

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

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

① Open C source code.

② Optimized code developed based on the Intel processor instruction set.

③ Unified structural and functional definitions.

④ Strong image and matrix computing capabilities.

⑤ Convenient and flexible user interface.

⑥ Supports MSWindows and Linux platforms at the same time.

As an open source project for basic computer vision, image processing and pattern recognition, OpenCV can be directly applied to many fields and is an ideal tool for secondary development.

Selection of library

If you use Opencv 2+, you can use this library/go-opencv/go-opencv, but this library does not support OpenCV 3+, so if you want to use OpenCV 3+, you can use it/hybridgroup/gocv, Today is mainly to introduce the use of gocv.

Environment configuration

If OpenCV is not installed yet, Mac OS can install OpenCV 3.4.1 through brew install opencv.

After installing go, run the go get -u -d /x/gocv command to get the gocv library, enter the root directory of the library cd $GOPATH/src//x/gocv, run source ./, and then you can use the go run command to run the examples. You can directly modify the code in the examples at the beginning.

Choice of IDE

I originally wanted to use IDEA + go plugin to develop, but I didn't know that when IDEA released the go IDE (charged), go plugin stopped maintaining and had to give up. Then I chose vscode+plugin, which was convenient and fast, and it felt good. There were smart prompts and code jumps. I haven't tried debugging yet. I guess debugging and run can be configured.

Example

After configuration, I couldn't wait to write the code. Since gocv encapsulates OpenCV, the method names are basically the same, but there are still some differences in use. However, the vscode code jump is very convenient. If you don't know, just jump to the source code and take a look. Here is an example of finding borders to see if it is very different from C++.

// 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/ /home/ron/Pictures/
//
// +build example

package main

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

 "/x/gocv"
)

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

 filename := [1]
 window := ("Hello")
 img := (filename, )
 grayImage := ()
 defer ()

 (img, &grayImage, )
 destImage := ()
 (grayImage, &destImage, 100, 255, )
 resultImage := (500, 400, gocv.MatTypeCV8U)

 (destImage, &resultImage, ((), ()), 0, 0, )
 (resultImage, &resultImage, ())
 (resultImage, &resultImage, (5, 5), 0, 0, )
 results := (resultImage, , )
 imageForShowing := ((), (), gocv.MatChannels4)
 for index, element := range results {
 (index)
 (&imageForShowing, results, index, {R: 0, G: 0, B: 255, A: 255}, 1)
 (&imageForShowing,
 (element),
 {R: 0, G: 255, B: 0, A: 100}, 1)
 }

 if () {
 ("Error reading image from: %v", filename)
 return
 }

 for {
 (imageForShowing)
 if (1) >= 0 {
 break
 }
 }
}

I changed it using this example, and the processing order is:

  • Grayscale CvtColor
  • Binary Threshold
  • Resize
  • Expansion Dilate
  • GaussianBlur
  • Find Contours
  • DrawContours
  • Minimum circtangle for drawing outline Rectangle

From the above example, we can see that the methods are all under package gocv. When using them, there will be code prompts, including parameters. Since go does not have a class constructor, they all use functions such as New+ type names to create types.

I am also a novice to go. Here are the codes written while watching the documents. Fortunately, there are smart code tips, which are quite handy to use. I hope this introductory article will be helpful to students who want to use go for OpenCV development.

Summarize:

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.