Read GIF images
GIF images cannot be read in opencv. This is due to license reasons. Instead, use videocapture or a third-party PIL library (Python), but in fact, Golang's basic library image
has a way to read gif images. So a simple example is as follows
func ReadAndShowGIF(filename string) {
w := gocv.NewWindow(filename)
f, _ := os.Open(filename)
defer f.Close()
gi, _ := gif.DecodeAll(f)
for k, v := range gi.Image {
img, err := gocv.ImageToMatRGB(v)
if err != nil {
log.Fatal(err)
}
w.IMShow(img)
w.WaitKey(gi.Delay[k] * 10) // delay 单位是百分之一秒,waitkey参数为毫秒
}
w.WaitKey(0)
}
The gif image will only be played once here. We can also parse the LoopCount in the gif to add loop playback logic.
Read mp4 video files
First, make sure that the dependencies are successfully installed during cmake installation opencv_ffmpeg_64.dll and opencv_ffmpeg.dll
, otherwise an error will be reported when calling gocv.VideoCaptureFile
or .gocv.OpenVideoCapture
Error opening file: showimage/video1.mp4
Open the path where opencv is compiled and installed C:\opencv\build\lib
, but these two dependencies are indeed not found, so what should I do?
When compiling opencv, it will first check whether ffmpeg is installed in the current system. If it is not installed, it will download and install it. However, it may fail during the download, so this dependency is not installed. The log of the download failure can be found, opencv/build/CMakeDownloadLog.txt
so , we open the ladder software and recompile opencv.
Read the video file using gocv.VideoCaptureFile(filename)
or gocv.OpenVideoCapture(filename)
and then process it frame by frame
func ReadAndShowVideo(filename string) {
w := gocv.NewWindow(filename)
vc, err := gocv.VideoCaptureFile(filename)
if err != nil {
fmt.Println(err)
return
}
mat := gocv.NewMat()
for {
if vc.Read(&mat) {
w.IMShow(mat)
w.WaitKey(10)
} else {
break
}
}
w.WaitKey(0)
}
In fact, you can also use ReadAndShowVideo
functions to read GIF images, but it is not ReadAndShowGIF
as detailed as the control.