gocv滑动验证码

supermassive

以下基于macOS

第一步,安装opencv

Installation
You can install OpenCV 4.6.0 using Homebrew.

If you already have an earlier version of OpenCV (3.4.x) installed, you should probably remove it before installing the new version:

brew uninstall opencv

You can then install OpenCV 4.6.0:

brew install opencv

pkgconfig Installation
pkg-config is used to determine the correct flags for compiling and linking OpenCV. You can install it by using Homebrew:

brew install pkgconfig

Verifying the installation
To verify your installation you can run one of the included examples.

First, change the current directory to the location of the GoCV repo:

cd $HOME/folder/with/your/src/gocv

Now you should be able to build or run any of the examples:

go run ./cmd/version/main.go

The version program should output the following:

gocv version: 0.31.0
opencv lib version: 4.6.0

第二步, 上代码

func distance(bgFilename, blockFilename string) {
    originBlock, err := getMat(blockFilename)
    if err != nil {
        fmt.Println("@#$%", err)
        return
    }
    defer originBlock.Close()

    originBg, err := getMat(bgFilename)
    if err != nil {
        fmt.Println("@#$%", err)
        return
    }
    defer originBg.Close()

    alpha, block, _ := preprocess(resize(originBlock, 68, 68))
    // fmt.Println("----", originBlock.Cols(), originBlock.Channels())
    _, bg, _ := preprocess(resize(originBg, 341, 195))
    // fmt.Println(bg.Cols())
    // fmt.Println("----", originBg.Cols(), originBg.Channels())
    loc := match(bg, block, alpha)
    fmt.Println(loc.X)
    // saveGray(bg, "bg_gray.png")
    // saveGray(block, "block_gray.png")
    // saveGray(threshold(bg), "bg_gray_t.png")
    // saveGray(threshold(block), "block_gray_t.png")
}

func saveGray(grayed gocv.Mat, filename string) {
    img, err := grayed.ToImage()
    if err != nil {
        fmt.Println(err)
    }
    out, _ := os.Create(filename)
    defer out.Close()
    png.Encode(out, img)
}

func Crop(width int, filename string) string {
    read := gocv.IMRead(filename, gocv.IMReadUnchanged)
    frontImg, _ := read.ToImage()
    rgbImg := frontImg.(*image.RGBA)
    subImg := rgbImg.SubImage(image.Rect(width, 0, rgbImg.Rect.Dx(), rgbImg.Rect.Dy())).(*image.RGBA)
    out, _ := os.Create("bg_sub" + filepath.Ext(filename))
    defer out.Close()
    png.Encode(out, subImg)
    return out.Name()
}

func getMat(filename string) (gocv.Mat, error) {
    f, err := ioutil.ReadFile(filename)
    if err != nil {
        fmt.Println(err)
    }
    origin, err := gocv.IMDecode(f, gocv.IMReadUnchanged)
    return origin, err
}

func preprocess(origin gocv.Mat) (alpha, processed gocv.Mat, err error) {
    rst := gray(origin)

    if origin.Channels() == 4 {
        return gocv.Split(origin)[3], threshold(rst), nil
    }

    return gocv.Mat{}, threshold(rst), nil
}

func resize(origin gocv.Mat, cols, rows int) gocv.Mat {
    resized := gocv.NewMatWithSize(cols, rows, origin.Type())
    gocv.Resize(origin, &resized, image.Pt(cols, rows), 0, 0, gocv.InterpolationNearestNeighbor)
    return resized
}

func gray(origin gocv.Mat) gocv.Mat {
    grayed := gocv.NewMat()
    gocv.CvtColor(origin, &grayed, gocv.ColorBGRToGray)

    return grayed
}

func threshold(origin gocv.Mat) gocv.Mat {
    dst := gocv.NewMat()
    gocv.AdaptiveThreshold(origin, &dst, 255, gocv.AdaptiveThresholdMean, gocv.ThresholdBinary, 7, -4)
    // gocv.Threshold(origin, &dst, 100, 255, gocv.ThresholdBinaryInv)
    return dst
}

func match(bg, block, mask gocv.Mat) image.Point {
    result := gocv.NewMatWithSize(
        bg.Rows()-block.Rows()+1,
        bg.Cols()-block.Cols()+1,
        gocv.MatTypeCV32FC1)
    defer result.Close()

    gocv.MatchTemplate(bg, block, &result, gocv.TmCcoeff, mask)
    // gocv.MatchTemplate(bg, block, &result, gocv.TmSqdiff, mask)
    gocv.Normalize(result, &result, 0, 1, gocv.NormMinMax)

    _, _, _, maxLoc := gocv.MinMaxLoc(result)
    // fmt.Println(minVal, maxVal, minLoc.X, minLoc.Y, maxLoc.X, maxLoc.Y)

    return maxLoc
}

测试过程中发现一个问题,不进行二值化,不行

阅读 207
1 声望
1 粉丝
0 条评论
1 声望
1 粉丝
文章目录
宣传栏