Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我用官方示例进行测试,编译报错了,是不是缺少什么步骤 #1270

Open
nxk88998 opened this issue Jan 16, 2025 · 0 comments

Comments

@nxk88998
Copy link

package main

import (
"fmt"
"github.com/kbinani/screenshot"
"gocv.io/x/gocv"
"image"
"image/color"
"image/jpeg"
"log"
"os"
"time"
)

func saveImage(img *image.RGBA, fileName string) error {
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()

jpeg.Encode(f, img, nil)

return nil

}

func processScreenshot(tpl gocv.Mat) {
for {
img1, err := screenshot.Capture(0, 0, 800, 600)
if err != nil {
log.Fatal(err)
}

	fileName := fmt.Sprintf("D:\\test.png")
	err = saveImage(img1, fileName)
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(time.Second)

	// 读取源图片和模板图片
	img := gocv.IMRead(fileName, gocv.IMReadColor)
	if img.Empty() || tpl.Empty() {
		panic("无法加载图片")
	}
	defer img.Close()

	// 创建输出结果矩阵
	result := gocv.NewMat()
	defer result.Close()

	// 进行模板匹配
	gocv.MatchTemplate(img, tpl, &result, gocv.TmCcoeffNormed, gocv.NewMat())

	// 获取匹配结果
	_, maxVal, _, maxLoc := gocv.MinMaxLoc(result)
	fmt.Println("max correlation value:", maxVal)

	// 设定一个阈值,判断是否匹配成功
	threshold := float32(0.8)
	if maxVal >= threshold {
		fmt.Printf("Found template at: %+v\n", maxLoc)
	} else {
		fmt.Println("Template not found.")
	}

	// 可以在源图片上标记出模板匹配的位置,这一步,可以删除
	if maxVal >= threshold {

		rect := image.Rect(maxLoc.X, maxLoc.Y, maxLoc.X+tpl.Cols(), maxLoc.Y+tpl.Rows())
		color := color.RGBA{R: 0, G: 0, B: 255, A: 0}
		gocv.Rectangle(&img, rect, color, 2)

		window := gocv.NewWindow("Template Match")
		window.IMShow(img)

		// 窗口等待时间
		window.WaitKey(1000)
		window.Close()
		break
	}

}

}

func main() {
// 读取模板图片
tpl := gocv.IMRead("D:\gin_demo\photo\demo1\1.jpg", gocv.IMReadColor)
if tpl.Empty() {
panic("无法加载模板图片")
}
defer tpl.Close()

processScreenshot(tpl)

}

gocv.io/x/gocv

D:\go\pkg\mod\gocv.io\x\[email protected]\calib3d_string.go:3:9: undefined: CalibFlag
D:\go\pkg\mod\gocv.io\x\[email protected]\calib3d_string.go:29:9: undefined: CalibCBFlag
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:3:9: undefined: MatType
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:57:9: undefined: CompareType
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:75:9: undefined: CovarFlags
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:93:9: undefined: DftFlags
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:113:9: undefined: RotateFlag
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:125:9: undefined: KMeansFlags
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:137:9: undefined: NormType
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:159:9: undefined: TermCriteriaType
D:\go\pkg\mod\gocv.io\x\[email protected]\core_string.go:159:9: too many errors
编译完成,并显示退出代码 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant