Skip to content

Commit

Permalink
Implementation of the draw function and color constant
Browse files Browse the repository at this point in the history
  • Loading branch information
mpsdantas committed Mar 17, 2020
1 parent a12da54 commit 73c4ef9
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
29 changes: 29 additions & 0 deletions colors.go
Original file line number Diff line number Diff line change
@@ -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,
}
)
74 changes: 74 additions & 0 deletions draw.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 73c4ef9

Please sign in to comment.