---
panlei
2019-07-31 f677cf7b9196a2aadc376cab4c769402d9ce1a5c
util/simpleCV.go
@@ -1,11 +1,17 @@
package util
import (
   "basic.com/pubsub/protomsg.git"
   "bufio"
   "fmt"
   "gocv.io/x/gocv"
   "image"
   "image/color"
   "gocv.io/x/gocv"
   "os"
   "ruleprocess/cache"
   "ruleprocess/logger"
   "ruleprocess/ruleserver"
   "strings"
)
func CvRTSP() {
@@ -14,7 +20,6 @@
   webcam, _ := gocv.OpenVideoCapture(url)
   window := gocv.NewWindow("Hello")
   img := gocv.NewMat()
   gocv.add
   for {
      webcam.Read(&img)
      window.IMShow(img)
@@ -150,34 +155,79 @@
   }
}
func DrawPolygon(){
   // draw the rook
   rook := gocv.NewMatWithSize(w, w, gocv.MatTypeCV8UC3)
func DrawPolygonOnImage(cameraId string, img protomsg.Image, results []ruleserver.Result,url string) (maps map[string]interface{}, err0 error) {
   rook, _ := gocv.NewMatFromBytes(int(img.Height), int(img.Width), gocv.MatTypeCV8UC3, img.Data)
   //rook := gocv.IMRead("/home/user/workspace/ruleprocess/util/105.jpg",gocv.IMReadColor)
   defer rook.Close()
   green := color.RGBA{0, 255, 0, 0}
   points := [][]image.Point{
      {
         image.Pt(w/4., 7*w/8.),
         image.Pt(3*w/4., 7*w/8.),
         image.Pt(3*w/4., 13*w/16.),
         image.Pt(11*w/16., 13*w/16.),
         image.Pt(19*w/32., 3*w/8.),
         image.Pt(3*w/4., 3*w/8.),
         image.Pt(3*w/4., w/8.),
         image.Pt(26*w/40., w/8.),
         image.Pt(26*w/40., w/4.),
         image.Pt(22*w/40., w/4.),
         image.Pt(22*w/40., w/8.),
         image.Pt(18*w/40., w/8.),
         image.Pt(18*w/40., w/4.),
         image.Pt(14*w/40., w/4.),
         image.Pt(14*w/40., w/8.),
         image.Pt(w/4., w/8.),
         image.Pt(w/4., 3*w/8.),
         image.Pt(13*w/32., 3*w/8.),
         image.Pt(5*w/16., 13*w/16.),
         image.Pt(w/4., 13*w/16.),
      },
   yellow := color.RGBA{255, 255, 0, 0}
   red := color.RGBA{255, 0, 0, 0}
   // 分割区域id集合并根据id查询区域然后画框
   for _,result := range results  {
      polygonIds := strings.Split(result.AlarmPolygon,",")
      logger.Info("-----------------------看看报警区域id:",polygonIds)
      for i := 0; i < len(polygonIds)-1; i++ {
         polygon := getPolygonById(polygonIds[i],cameraId)
         if polygon.Polygon != "[]" {
            DrawAPolygon(&rook,polygon.Polygon,yellow)
         }
      }
   }
   gocv.FillPoly(&rook, points, green)
   // 把目标框出来
   for _,result := range results  {
      for _,rect := range result.Location {
         gocv.Rectangle(&rook, image.Rect(int(rect.X), int(rect.Y), int(rect.X+rect.Width), int(rect.Y+rect.Height)), red, 1)
      }
   }
   //return nil,nil
   maps,err0 = UploadFromMat(url,rook)
   return
}
// 把图片转成二进制流
func RetrieveROM(filename string) ([]byte, error) {
   file, err := os.Open(filename)
   if err != nil {
      return nil, err
   }
   defer file.Close()
   stats, statsErr := file.Stat()
   if statsErr != nil {
      return nil, statsErr
   }
   var size int64 = stats.Size()
   bytes := make([]byte, size)
   bufr := bufio.NewReader(file)
   _, err = bufr.Read(bytes)
   return bytes, err
}
// 根据id去缓存里查询多边形
func getPolygonById(polygonId string, cameraId string) (protomsg.CameraPolygon){
   // 查到摄像机所有的区域并画框
   var cameraPolygons []protomsg.CameraPolygon
   cameraPolygons = cache.GetPolygonsByCameraId(cameraId)
   for _, polygon := range cameraPolygons {
      if polygon.Id == polygonId {
         return polygon
      }
   }
   return protomsg.CameraPolygon{}
}
// 在图上画一个框
func DrawAPolygon(rook *gocv.Mat,polygonString string, color color.RGBA) {
   points := ruleserver.Json2points(polygonString)
   for index := 0; index < len(points); index++ {
      if index == len(points)-1 { // 闭合图形
         gocv.Line(rook, image.Pt(int(points[index].X*1.33), int(points[index].Y*1.33)), image.Pt(int(points[0].X*1.33), int(points[0].Y*1.33)), color, 2)
      } else {
         gocv.Line(rook, image.Pt(int(points[index].X*1.33), int(points[index].Y*1.33)), image.Pt(int(points[index+1].X*1.33), int(points[index+1].Y*1.33)), color, 2)
      }
   }
}