From 73c4ef974e1c852fdcee3f16c94e316fe56d3937 Mon Sep 17 00:00:00 2001 From: Marcus Dantas Date: Tue, 17 Mar 2020 11:30:40 -0300 Subject: [PATCH] Implementation of the draw function and color constant --- colors.go | 29 ++++++++++++++++++++++ draw.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 colors.go create mode 100644 draw.go diff --git a/colors.go b/colors.go new file mode 100644 index 0000000..625b431 --- /dev/null +++ b/colors.go @@ -0,0 +1,29 @@ +package cvutils + +var ( + Black = Color{ + Red: 0, + Green: 0, + Blue: 0, + } + White = Color{ + Red: 255, + Green: 255, + Blue: 255, + } + Red = Color{ + Red: 255, + Green: 0, + Blue: 0, + } + Green = Color{ + Red: 0, + Green: 255, + Blue: 0, + } + Blue = Color{ + Red: 0, + Green: 0, + Blue: 255, + } +) diff --git a/draw.go b/draw.go new file mode 100644 index 0000000..5feea15 --- /dev/null +++ b/draw.go @@ -0,0 +1,74 @@ +package cvutils + +import ( + "errors" + "fmt" + "log" + + "gocv.io/x/gocv" +) + +type ImageOptions struct { + Name string + Flags gocv.IMReadFlag + WindowName string + Draw DrawOptions +} + +type DrawOptions struct { + DrawFunc DrawFunc + Color Color + StartingPoint Point + EndPoint Point +} + +type Point struct { + X int + Y int +} + +type DrawFunc func(image Image, color Color, point Point) + +func ensureImageCanBeDraw(dimensions []int, points []Point) error { + for _, point := range points { + if point.X > dimensions[0] || point.Y > dimensions[1] { + return errors.New(fmt.Sprintf("The point: %v is greater "+ + "than the upper limit of the image: x = %v y = %v", point, dimensions[0], dimensions[1])) + } + } + + return nil +} + +func ShowIMG(opts ImageOptions) { + img := Image{ + Mat: gocv.IMRead(opts.Name, opts.Flags), + } + + if img.Mat.Empty() { + log.Fatal(fmt.Sprintf("Error reading image from: %v\n", opts.Name)) + } + + defer img.Mat.Close() + + if err := ensureImageCanBeDraw(img.Mat.Size(), []Point{ + opts.Draw.StartingPoint, + opts.Draw.EndPoint, + }); err != nil { + log.Fatal(err.Error()) + } + + window := gocv.NewWindow(opts.WindowName) + + for i := opts.Draw.StartingPoint.X; i < opts.Draw.EndPoint.X; i++ { + for j := opts.Draw.StartingPoint.Y; j < opts.Draw.EndPoint.Y; j++ { + opts.Draw.DrawFunc(img, opts.Draw.Color, Point{ + X: i, + Y: j, + }) + } + } + + window.IMShow(img.Mat) + window.WaitKey(0) +}