gocv

安装gocv(windows)

安装mingw(即gcc)

没有安装gcc会报以下错

"gcc": executable file not found in %PATH%

  • 下载

地址 https://sourceforge.net/projects/mingw-w64/files/

选择版本posix" — “seh”

我选择的是[x86_64-posix-seh](https://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win64/Personal Builds/mingw-builds/7.3.0/threads-posix/seh/x86_64-7.3.0-release-posix-seh-rt_v5-rev0.7z)

  • 解压并添加环境变量

D:\software\mingw64\bin加入环境变量

安装opencv(不推荐这种方法)

  • 下载源码opencv4.0.1以及opencv contrib 4.0.1(之前试了4.1和4.3版本都编译失败了),记住后面要把OPENCV_EXTRA_MODULES_PATH选择为opencv_contrib文件夹中的modules文件夹
  • 将这两个解压到同一个目录下,并在这个目录下新建build文件夹
  • 使用cmake 编译,记得编译器选中MinGW Makefiles
  • Generate done后,在build目录下执行mingw32-make或mingw32-make -j 8
  • 安装:mingw32-make install,执行完成后build目录下会多一个install文件夹
  • 查看gocv的cgo文件把install 文件放在指定位置,并把C:\opencv\build\install\x64\mingw\lib和C:\opencv\build\install\x64\mingw\bin加入环境变量
mingw32-make -j 8
mingw32-make install

上面的方法太蠢,直接在gocv里面执行win_build_opencv.cmd就行

我这报了一个错误是因为连接到了anaconda3里的hdf5库上,我直接卸载掉了anaconda3

切记切记,就算不编译hdf5,然后编译通过也有问题,因为他编译的时候用的python里面的lib库,和dll文件对不上

测试opencv

cd %GOPATH%\src\gocv.io\x\gocv
go run cmd\version\main.go

结果

gocv version: 0.21.0
opencv lib version: 4.1.2

bug

运行报错 exit status 3221225781

opencv编译完毕之后记得将编译后的opencv配置到环境变量,不添加就报上面的错误。

编译报错boostdesc_bgm.i: 没有那个文件或目录

把下列压缩包解压到目录opencv_contrib/modules/xfeatures2d/src/下即可。

链接:https://pan.baidu.com/s/17XhXX_cLz46bsj9ZWRkNIg
提取码:p50x

rtsp播放不了,usb的能播放

原因是缺少ffmpeg库,这种错误很难发现,通过CMAKE编译出的install文件,竟然不全,查看build/CMakeDownloadLog.txt可以看到下载opencv_ffmpeg.dll和opencv_ffmpeg_64.dll和ffmpeg_version.cmake失败了

解决:

查看CMakeDownloadLog.txt

#cmake_download "C:/opencv/opencv-4.0.1/.cache/ffmpeg/849286ccc527c99e5a218b67f13c6e8c-opencv_ffmpeg.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/759a23e24ab787a0979f8a93103dcc3105ec10c1/ffmpeg/opencv_ffmpeg.dll"

#cmake_download "C:/opencv/opencv-4.0.1/.cache/ffmpeg/96444a4645753aaafa296479665c9185-opencv_ffmpeg_64.dll" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/759a23e24ab787a0979f8a93103dcc3105ec10c1/ffmpeg/opencv_ffmpeg_64.dll"

#cmake_download "C:/opencv/opencv-4.0.1/.cache/ffmpeg/f710891525a04586d565d0e700e62a9c-ffmpeg_version.cmake" "https://raw.githubusercontent.com/opencv/opencv_3rdparty/759a23e24ab787a0979f8a93103dcc3105ec10c1/ffmpeg/ffmpeg_version.cmake"

下载

通过https://www.ipaddress.com/,输入raw.githubusercontent.com查询真实IP地址,我好像查询到的用不了,直接用下面的199.232.4.133就可以

修改windows/system32/drivers/etc/hosts文件

199.232.4.133 raw.githubusercontent.com

ffmpeg_version.cmake opencv_videoio_ffmpeg.dll opencv_videoio_ffmpeg_64.dll 三项替换掉.cache目录下的的三项即可然后重新编译即可

修改了hosts之后也可能直接成功。

注:也可以等编译安装完成后手动下载,放到C:\opencv\build\install\x64\mingw\bin目录下但要改一下名字例如opencv_videoio_ffmpeg_64.dll改为opencv_videoio_ffmpeg401_64.dll

实例

package test

import (
	"fmt"
	"github.com/hybridgroup/mjpeg"
	"gocv.io/x/gocv"
	"net/http"
	"testing"
)
var (
	deviceID int
	err      error
	webcam   *gocv.VideoCapture
	stream   *mjpeg.Stream
)
func mjpegCapture() {
	img := gocv.NewMat()
	defer img.Close()

	for {
		if ok := webcam.Read(&img); !ok {
			fmt.Printf("Device closed: %v\n", deviceID)
			return
		}
		if img.Empty() {
			continue
		}

		buf, _ := gocv.IMEncode(".jpg", img)
		stream.UpdateJPEG(buf)
	}
}

func TestGocv(t *testing.T){


	// parse args
	deviceID = 0
	host := ":8554"

	// open webcam
	webcam, err = gocv.OpenVideoCapture(deviceID)
	if err != nil{
		fmt.Println("OpenVideoCapture error:",err)
	}
	defer webcam.Close()
	// create the mjpeg stream
	stream = mjpeg.NewStream()

	// start capturing
	go mjpegCapture()

	// start http server
	http.Handle("/", stream)
	fmt.Println(http.ListenAndServe(host, nil))
}

然后在网页中输入127.0.0.1:8554/
即可访问摄像头

Linux 下安装

超简单安装readme安装就行

cd $GOPATH/src/gocv.io/x/gocv
make install

如果下载opencv速度太慢可以更换为国内的源

curl -Lo opencv.zip https://github.com/opencv/opencv/archive/$(OPENCV_VERSION).zip

curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/$(OPENCV_VERSION).zip

安装Makefile直接自己下载这两个包放到/tmp/opencv下面

扩展

//如果需要安装CUDA版本
make install_cuda

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×