OpenCV 相当于 np.where()
来源:stackoverflow
时间:2024-04-13 17:33:34 482浏览 收藏
从现在开始,我们要努力学习啦!今天我给大家带来《OpenCV 相当于 np.where()》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!
当使用 gocv 包时,可以执行图像内模式的模板匹配。该包还提供 minmaxloc 函数来检索矩阵内最小值和最大值的位置。
但是,在下面的 python 示例中,作者使用 numpy.where 对矩阵进行阈值处理并获取多个最大值的位置。 python zip 函数用于将值粘合在一起,因此它们就像一个切片 [][2]int
,内部切片是找到的匹配项的 xs 和 ys。
语法 loc[::-1]
反转数组。
zip(*loc..)
中的星号运算符用于解压提供给 zip 的切片。
https://docs.opencv.org/master/d4/dc6/tutorial_py_template_matching.html
import cv2 as cv import numpy as np from matplotlib import pyplot as plt img_rgb = cv.imread('mario.png') img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY) template = cv.imread('mario_coin.png',0) w, h = template.shape[::-1] res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED) threshold = 0.8 loc = np.where( res >= threshold) for pt in zip(*loc[::-1]): cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) cv.imwrite('res.png',img_rgb)
如何在 go 中实现相同的 np.where
算法来获取应用阈值后的多个位置?
解决方案
opencv 有一个与 np.where()
内置(半)等效的函数,即 findNonZero()
。顾名思义,它查找图像中的非零元素,这就是 np.where ()
在使用单个参数调用时执行此操作,如 the numpy docs 所示。
这在 golang 绑定中也可用。来自gocv
docs on FindNonZero:
func findnonzero(src mat, idx *mat)
findnonzero 返回非零像素的位置列表。
更多详情请参阅:https://docs.opencv.org/master/d2/de8/group__core__array.html#gaed7df59a3539b4cc0fe5c9c8d7586190
注意:np.where()
按数组顺序返回索引,即 (row, col) 或 (i, j),这与典型的图像索引 (x, y) 相反。这就是为什么 loc
在 python 中是相反的。使用 findnonzero()
时,您不需要这样做,因为 opencv 始终使用 (x, y) 表示点。
对于遇到此问题的任何人,我希望有一个完整的示例,让您不必花几天时间将头撞在墙上,一遍又一遍地阅读相同的谷歌结果,直到有点击为止。
package main import ( "fmt" "image" "image/color" "os" "gocv.io/x/gocv" ) func OpenImage(path string) (image.Image, error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() img, _, err := image.Decode(f) return img, err } func main() { src := gocv.IMRead("haystack.png", gocv.IMReadGrayScale) tgt := gocv.IMRead("needle.png", gocv.IMReadGrayScale) if src.Empty() { fmt.Printf("failed to read image") os.Exit(1) } if tgt.Empty() { fmt.Printf("failed to read image") os.Exit(1) } // Get image size tgtImg, _ := tgt.ToImage() iX, iY := tgtImg.Bounds().Size().X, tgtImg.Bounds().Size().Y // Perform a match template operation res := gocv.NewMat() gocv.MatchTemplate(src, tgt, &res, gocv.TmSqdiffNormed, gocv.NewMat()) // Set a thresh hold. Using the `gocv.TmSqdiffNormed` confidence levels are // reversed. Meaning the lowest value is actually the greatest confidence. // So here I perform an Inverse Binary Threshold setting all values // above 0.16 to 1. thresh := gocv.NewMat() gocv.Threshold(res, &thresh, 0.16, 1.0, gocv.ThresholdBinaryInv) // Filter out all the non-zero values. gocv.FindNonZero(thresh, &res) // FindNonZero returns a list or vector of locations in the form of a gocv.Mat when using gocv. // There may be a better way to do this, but I iterate through each found location getting the int vector in value // at each row. I have to convert the returned int32 values into ints. Then draw a rectangle around each point. // // The result of get res.GetVeciAt(i, 0) is just a slice of x, y integers so each value can be accessed by // using slice/array syntax. for i := 0; i < res.Rows(); i++ { x, y := res.GetVeciAt(i, 0)[0], res.GetVeciAt(i, 0)[1] xi, yi := int(x), int(y) gocv.Rectangle(&src, image.Rect(xi, yi, xi+iX, yi+iY), color.RGBA{0, 0, 0, 1}, 2) } w := gocv.NewWindow("Test") w.IMShow(src) if w.WaitKey(0) > 1 { os.Exit(0) } }
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习