dongruolin5324 2014-05-09 19:03
浏览 51
已采纳

为什么将opencv.NewWindow()移到子函数中时程序立即终止?

I'm still trying to wrap my head around the Go language and I've just come across some rather confusing behavior. Here is a working version of my code:

Note, you will need to install OpenCV (package libopencv-dev in Ubuntu) and go-opencv (go get github.com/lazywei/go-opencv/opencv) if you want to execute these examples.

Working:

package main

import (
    "fmt"
    "github.com/lazywei/go-opencv/opencv"
)

func main() {
    win := opencv.NewWindow("Go-OpenCV Webcam")
    defer win.Destroy()

    frames := GetFrameGenerator()
    go DisplayFrames(win, frames)

    opencv.WaitKey(0)
}

func GetFrameGenerator() chan *opencv.IplImage {
    frames := make(chan *opencv.IplImage)

    cap := opencv.NewCameraCapture(0)
    if cap == nil {
        panic("cannot open camera")
    }

    go func() {
        defer cap.Release()
        for {
            if cap.GrabFrame() {
                img := cap.RetrieveFrame(1)
                if img != nil {
                    frames <- img
                } else {
                    fmt.Println("Image ins nil")
                }
            }
        }
    }()

    return frames
}

func DisplayFrames(win *opencv.Window, frames <-chan *opencv.IplImage) {
    for fr := range frames {
        win.ShowImage(fr)
    }
}

Not Working:

package main

import (
    "fmt"
    "github.com/lazywei/go-opencv/opencv"
)

func main() {

    frames := GetFrameGenerator()
    go DisplayFrames(frames)

    opencv.WaitKey(0)
}

func GetFrameGenerator() chan *opencv.IplImage {
    frames := make(chan *opencv.IplImage)

    cap := opencv.NewCameraCapture(0)
    if cap == nil {
        panic("cannot open camera")
    }

    go func() {
        defer cap.Release()
        for {
            if cap.GrabFrame() {
                img := cap.RetrieveFrame(1)
                if img != nil {
                    frames <- img
                } else {
                    fmt.Println("Image ins nil")
                }
            }
        }
    }()

    return frames
}

func DisplayFrames(frames <-chan *opencv.IplImage) {
    win := opencv.NewWindow("Go-OpenCV Webcam")
    defer win.Destroy()
    for fr := range frames {
        win.ShowImage(fr)
    }
}

Intuitively, it seems as though the program should run until it is forcibly stopped because

  • the call to opencv.WaitKey(0) should just sit there and wait for some sort of keypress
  • the anonymous goroutine in GetFrameGenerator should just keep chugging away, synchronizing with DisplayFrames on each frame.
  • The OpenCV window instance is tied to the lifetime of DisplayFrames' call stack, which is in turn tied to frames generator being unclosed.

So what gives? What am I missing, here?

  • 写回答

1条回答 默认 最新

  • duanfei1268 2014-05-09 20:35
    关注

    I don't have opencv installed so I could not test, but something like this should do the trick:

    package main
    
    import (
            "fmt"
            "github.com/lazywei/go-opencv/opencv"
    )
    
    func main() {
    
            frames := GetFrameGenerator()
            ready := make(chan struct{})
            defer close(ready)
            go DisplayFrames(frames, ready)
            <-ready
            opencv.WaitKey(0)
    }
    
    func GetFrameGenerator() chan *opencv.IplImage {
            frames := make(chan *opencv.IplImage)
    
            cap := opencv.NewCameraCapture(0)
            if cap == nil {
                    panic("cannot open camera")
            }
    
            go func() {
                    defer cap.Release()
                    for {
                            if cap.GrabFrame() {
                                    img := cap.RetrieveFrame(1)
                                    if img != nil {
                                            frames <- img
                                    } else {
                                            fmt.Println("Image ins nil")
                                    }
                            }
                    }
            }()
    
            return frames
    }
    
    func DisplayFrames(frames <-chan *opencv.IplImage, ready <-chan struct{}) {
            win := opencv.NewWindow("Go-OpenCV Webcam")
            defer win.Destroy()
            ready <- struct{}{}
    
            for fr := range frames {
                    win.ShowImage(fr)
            }
    }
    

    opencv.WaitKey() will block only if opencv.NewWindow() has been called. As you are calling it in a goroutine, you need to add synchronization to make sure it has been done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?