安装
#3.4.2sudo apt-get install opencvsudo apt-get install golang-1.9vi ~/.profileexport GOPATH=$HOME/Desktop/GoPathexport PATH=$PATH:$GOPATH/bin:wq!#https://github.com/hybridgroup/gocv#version 0.14.0go get gocv.io/x/gocv
gocv
//灰度图 gocv.CvtColor(img,&grayMat, gocv.ColorRGBAToGray) //均值滤波 gocv.Blur(grayMat, &canny, image.Point{3,3}) //边缘检测 gocv.Canny(grayMat, &canny, 125, 225) //二值化处理,非黑即白图片 gocv.Threshold(canny, &binaryMat, 165, 255, gocv.ThresholdBinary) //中值滤波 gocv.MedianBlur(binaryMat,&binaryMat,3) //创建一个40x40的Mat element_9 := gocv.NewMatWithSizeFromScalar(gocv.NewScalar(1,1,1,255), 40, 40, gocv.MatTypeCV8U) defer element_9.Close() //将图片binaryMat 中 + - 40 距离的连通矩形 连在一起 gocv.MorphologyEx(binaryMat,&element_9,gocv.MorphCross, element_9) //寻找连通矩形 contoursList := gocv.FindContours(element_9, gocv.RetrievalCComp,gocv.ChainApproxNone) for _,contours := range(contoursList){ rect := gocv.BoundingRect(contours) gocv.Rectangle(&img,rect,blue,1) }
准备数据
mkdir opencv_datacd opencv_data#放置训练数据图片(大小一致)mkdir img#放置纯背景图片mkdir neg_img#根据提示操作,选择需要识别的区域opencv_annotation --annotations=positive.txt --images=img#将positive.txt中的图片对应的矩阵放置到 positive_sample.vec中等待训练使用opencv_createsamples -info positive.txt -vec positive_sample.vec -num 66 -w 100 -h 63
训练数据
mkdir dataopencv_traincascade -data data -vec positive_sample.vec -bg negative.txt -numPos 66 -numNeg 7 -numStage 15 -w 100 -h 63 -minHitRate 0.99 -maxFalseAlarmRate 0.5 -mode ALL -featureType HAAR -precalcValBufSize 4048 -precalcIdxBufSize 4048 -numThreads 24//Required leaf false alarm rate achieved. Branch training terminated //样本太少