亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

OpenCV 相當于 np.where()

OpenCV 相當于 np.where()

Go
慕沐林林 2023-07-31 17:33:20
例如,當使用gocv包時,可以執行圖像內模式的模板匹配。該包還提供MinMaxLoc函數來檢索矩陣內最小值和最大值的位置。然而,在下面的 python 示例中,作者使用numpy.Where對矩陣進行閾值處理并獲取多個最大值的位置。python?zip函數用于將值粘合在一起,因此它們就像一個切片[][2]int,內部切片是找到的匹配項的 xs 和 ys。該語法loc[::-1]?反轉數組。中的星號運算符zip(*loc..)用于解壓提供給 zip 的切片。import cv2 as cvimport numpy as npfrom matplotlib import pyplot as pltimg_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.8loc = 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)如何np.where在 Go 中實現相同的算法來獲取應用閾值后的多個位置?
查看完整描述

2 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

OpenCV 有一個與 內置(半)等效的函數np.where(),即findNonZero()。正如名稱所暗示的,它會查找圖像中的非零元素,這就是np.where()使用單個參數調用時的作用,如numpy 文檔所述。

這在 golang 綁定中也可用。來自gocvFindNonZero 的文檔:

func?FindNonZero(src?Mat,?idx?*Mat)

FindNonZero 返回非零像素位置的列表。

注意:np.where()按數組順序返回索引,即 (row, col) 或 (i, j),這與典型的圖像索引 (x, y) 相反。這就是為什么locPython 中是相反的。使用時findNonZero()你不需要這樣做,因為 OpenCV 總是使用 (x, y) 來表示點。


查看完整回答
反對 回復 2023-07-31
?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

對于任何遇到這個問題的人,我希望有一個完整的例子,讓你不用花幾天時間把頭撞在墻上,一遍又一遍地閱讀相同的谷歌結果,直到有東西點擊。


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)

    }

}



查看完整回答
反對 回復 2023-07-31
  • 2 回答
  • 0 關注
  • 310 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號