-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the draw function and color constant
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |