dongruolin5324 2014-05-09 11: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 12: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.

    展开全部

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

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部