侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

在go语言中使用OpenCV实现检测颜色的示例

Go  /  管理员 发布于 2年前   407

如何用OpenCV和Golang识别颜色名称的代码例子,它可以检测红、绿、蓝三种颜色。我把它作为一个练习,让你来实现黄色的颜色识别。

顺便说一下,这并不是一个真正的好的解决方案(但它能完成工作)....,只是因为它依赖于太多的 "IF "语句。

正确的方法应该是使用一个矩阵,检查一个颜色的十进制代码是否在这个范围内。

然而,在这个阶段,我只需要一个快速的方法来用OpenCV告诉一个颜色的名字。


代码示例:

 package main
 import (
         "fmt"
         "image"
         "runtime"
         "github.com/lazywei/go-opencv/opencv"
         "github.com/mattn/go-gtk/glib"
         "github.com/mattn/go-gtk/gtk"
 )
 var (
         win              = new(opencv.Window)
         win2             = new(opencv.Window)
         webCamera        = new(opencv.Capture)
         cascade          = new(opencv.HaarCascade)
         statusbar        = new(gtk.Statusbar)
         snapshotFileName string
         width, height    int
         filterOn         = true
         label            *image.RGBA
         opencvLabel      *opencv.IplImage
         whiteColor       = opencv.NewScalar(255, 255, 255, 255) // (blue, green, red, alpha)
         greenColor       = opencv.NewScalar(170, 255, 102, 255) // (blue, green, red, alpha)
         blueColor        = opencv.NewScalar(255, 0, 0, 255)     // (blue, green, red, alpha)
         redColor         = opencv.NewScalar(0, 0, 255, 255)     // (blue, green, red, alpha)
         blackColor       = opencv.NewScalar(0, 0, 0, 255)       // (blue, green, red, alpha)
         horizontalScale  = float32(0.5)
         verticalScale    = float32(0.5)
         shear            = float32(1.0)
         thickness        = 1
         lineType         = 8
         textFont         = opencv.InitFont(opencv.CV_FONT_HERSHEY_SIMPLEX, horizontalScale, verticalScale, shear, thickness, lineType)
 )
 func detectColor(img *opencv.IplImage) {
         //突出我们感兴趣的区域
         opencv.Rectangle(img,
                 opencv.Point{100, 100},
                 opencv.Point{120, 120},
                 greenColor, 3, 4, 0)
         // 我们要从ROI中提取BGRA值。
         //从ROI中提取。取中间的点
         scalar := img.Get2D(110, 110)
         //fmt.Println(scalar.Val())
         scalarArray := scalar.Val()
         //fmt.Println(scalarArray)
         Bchannel := scalarArray[0]
         Gchannel := scalarArray[1]
         Rchannel := scalarArray[2]
         Achannel := scalarArray[3]
         fmt.Println("B,G,R,A : ", Bchannel, Gchannel, Rchannel, Achannel)
         // 所以,我们可以得到B,G,R,A的值....time来检测颜色
         // 但让我们暂时忽略Alpha通道。
         IsBlue(scalarArray, img)
         IsGreen(scalarArray, img)
         IsRed(scalarArray, img)
         win.ShowImage(img)
 }
 func IsGreen(inputArray [4]float64, img *opencv.IplImage) {
         //来自http://www.rapidtables.com/web/color/RGB_Color.htm
         //调整上、下限以增加范围和灵敏度
         greenUpper := [4]float64{210, 255, 199, 0} // Blue, Green, Red
         greenLower := [4]float64{50, 80, 50, 0}
         if (inputArray[0] >= greenLower[0]) && (inputArray[0] <= greenUpper[0]) {
                 if (inputArray[1] >= greenLower[1]) && (inputArray[1] <= greenUpper[1]) {
                         if (inputArray[2] >= greenLower[2]) && (inputArray[2] <= greenUpper[2]) {
                                 fmt.Println("Green color detected.")
                                 textFont.PutText(img, "I think it is green", opencv.Point{10, 50}, greenColor)
                         }
                 }
         }
 }
 func IsRed(inputArray [4]float64, img *opencv.IplImage) {
         //来自http://www.rapidtables.com/web/color/RGB_Color.htm
         //调整上、下限以增加范围和灵敏度
         redUpper := [4]float64{70, 75, 255, 0} // Blue, Green, Red
         redLower := [4]float64{15, 15, 100, 0}
         if (inputArray[0] >= redLower[0]) && (inputArray[0] <= redUpper[0]) {
                 if (inputArray[1] >= redLower[1]) && (inputArray[1] <= redUpper[1]) {
                         if (inputArray[2] >= redLower[2]) && (inputArray[2] <= redUpper[2]) {
                                 fmt.Println("Red color detected.")
                                 textFont.PutText(img, "I think it is red", opencv.Point{10, 50}, redColor)
                         }
                 }
         }
 }
 func IsBlue(inputArray [4]float64, img *opencv.IplImage) {
         // 我们将只考虑海军蓝、深蓝、中蓝和真蓝。
         //来自http://www.rapidtables.com/web/color/RGB_Color.htm
         //调整上、下限以增加范围和灵敏度
         blueUpper := [4]float64{255, 175, 95, 0}
         blueLower := [4]float64{128, 25, 25, 0}
         if (inputArray[0] >= blueLower[0]) && (inputArray[0] <= blueUpper[0]) {
                 if (inputArray[1] >= blueLower[1]) && (inputArray[1] <= blueUpper[1]) {
                         if (inputArray[2] >= blueLower[2]) && (inputArray[2] <= blueUpper[2]) {
                                 fmt.Println("Blue color detected.")
                                 textFont.PutText(img, "I think it is blue", opencv.Point{10, 50}, blueColor)
                         }
                 }
         }
 }
 func processFrameAndUpdate() {
         for {
                 if webCamera.GrabFrame() {
                         IplImgFrame := webCamera.RetrieveFrame(1)
                         if IplImgFrame != nil {
                                 // 必须使图像框架
                                 // 小一点,因为性能原因--即速度
                                 // 面积越大,速度就越慢......
                                 IplImgFrame = opencv.Resize(IplImgFrame, 0, height-200, opencv.CV_INTER_LINEAR)
                                 if filterOn == true {
                                         detectColor(IplImgFrame)
                                 } else {
                                         win.ShowImage(IplImgFrame)
                                 }
                         }
                 }
         }
 }
 func main() {
         cores := runtime.NumCPU()
         //最大限度地提高CPU的使用率,以获得最大的性能
         runtime.GOMAXPROCS(cores)
         fmt.Printf("This machine has %d CPU cores. Using all cores. \n", cores)
         //一个新的OpenCV窗口
         win = opencv.NewWindow("Go-OpenCV color recognition example")
         defer win.Destroy()
         //激活网络摄像机
         webCamera = opencv.NewCameraCapture(opencv.CV_CAP_ANY) // 自动检测
         if webCamera == nil {
                 panic("Unable to open camera")
         }
         defer webCamera.Release()
         //从相机中获取一些数据
         width = int(webCamera.GetProperty(opencv.CV_CAP_PROP_FRAME_WIDTH))
         height = int(webCamera.GetProperty(opencv.CV_CAP_PROP_FRAME_HEIGHT))
         fmt.Println("Camera width : ", width)
         fmt.Println("Camera height : ", height)
         //首先打开一个新的 "纯 "OpenCV窗口
         go processFrameAndUpdate() //更新摄像机信息的Goroutine
         //我们的 "浮动 "GTK工具条
         gtk.Init(nil)
         window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
         window.SetPosition(gtk.WIN_POS_CENTER)
         window.SetTitle("Go-OpenCV color recognition example")
         window.SetIconName("gtk-dialog-info")
         window.Connect("destroy", func(ctx *glib.CallbackContext) {
                 println("got destroy!", ctx.Data().(string))
                 gtk.MainQuit()
         }, "Happy coding!")
         vbox := gtk.NewVBox(false, 1)
         //--------------------------------------------------------
         // GtkVPaned
         //--------------------------------------------------------
         vpaned := gtk.NewVPaned()
         vbox.Add(vpaned)
         frame2 := gtk.NewFrame("Go-OpenCV color recognition example")
         framebox2 := gtk.NewVBox(false, 1)
         frame2.Add(framebox2)
         //--------------------------------------------------------
         // GtkScale
         //--------------------------------------------------------
         scaleHBox := gtk.NewHBox(false, 1)
         framebox2.PackStart(scaleHBox, false, false, 0)
         vpaned.Pack2(frame2, false, false)
         //--------------------------------------------------------
         // GtkHBox
         //--------------------------------------------------------
         buttons := gtk.NewHBox(false, 1)
         //--------------------------------------------------------
         // GtkButton
         //--------------------------------------------------------
         quitButton := gtk.NewButtonWithLabel("Quit")
         quitButton.Clicked(func() {
                 gtk.MainQuit()
         })
         buttons.Add(quitButton)
         framebox2.PackStart(buttons, false, false, 0)
         //--------------------------------------------------------
         // GtkVSeparator
         //--------------------------------------------------------
         vsep := gtk.NewVSeparator()
         framebox2.PackStart(vsep, false, false, 0)
         statusbar = gtk.NewStatusbar()
         context_id := statusbar.GetContextId("go-gtk")
         combos := gtk.NewHBox(false, 1)
         //--------------------------------------------------------
         // GtkStatusbar and GtkComboBox
         //--------------------------------------------------------
         combobox := gtk.NewComboBoxNewText()
         combobox.AppendText("Detect color")
         combobox.AppendText("No color detection")
         combobox.SetActive(0) // Detect color
         combobox.Connect("changed", func() {
                 println("value:", combobox.GetActiveText())
                 if combobox.GetActiveText() == "Detect color" {
                         statusbar.Push(context_id, "Detecting color in region of interest (ROI).")
                         filterOn = true
                 } else {
                         statusbar.Push(context_id, "Not detecting color in region of interest (ROI).")
                         filterOn = false
                 }
         })
         combos.Add(combobox)
         framebox2.PackStart(combos, false, false, 0)
         //--------------------------------------------------------
         // GtkStatusbar
         //--------------------------------------------------------
         framebox2.PackStart(statusbar, false, false, 0)
         //--------------------------------------------------------
         // Event
         //--------------------------------------------------------
         window.Add(vbox)
         window.SetSizeRequest(400, 85)
         window.ShowAll()
         gtk.Main()
 }



  • 上一条:
    在laravel项目中生成内置的对象配置文件扩展包推荐:punchcard
    下一条:
    在go语言中使用网络摄像头和OpenCV实现即时监控功能示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-09
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-12
    • 2021-01
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-08
    • 2024-11
    • 2025-02
    • 2025-04
    • 2025-05
    • 2025-06
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客