Merge pull request #1 from davidoram/feature/puttext
Added support for putText
This commit is contained in:
commit
aa77750ab9
3 changed files with 108 additions and 0 deletions
BIN
images/lena_with_text.jpg
Normal file
BIN
images/lena_with_text.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 105 KiB |
|
|
@ -566,6 +566,48 @@ func Circle(image *IplImage, pt1 Point, radius int, color Scalar, thickness, lin
|
|||
)
|
||||
}
|
||||
|
||||
const (
|
||||
CV_FONT_HERSHEY_SIMPLEX = int(C.CV_FONT_HERSHEY_SIMPLEX)
|
||||
CV_FONT_HERSHEY_PLAIN = int(C.CV_FONT_HERSHEY_PLAIN)
|
||||
CV_FONT_HERSHEY_DUPLEX = int(C.CV_FONT_HERSHEY_DUPLEX)
|
||||
CV_FONT_HERSHEY_COMPLEX = int(C.CV_FONT_HERSHEY_COMPLEX)
|
||||
CV_FONT_HERSHEY_TRIPLEX = int(C.CV_FONT_HERSHEY_TRIPLEX)
|
||||
CV_FONT_HERSHEY_COMPLEX_SMALL = int(C.CV_FONT_HERSHEY_COMPLEX_SMALL)
|
||||
CV_FONT_HERSHEY_SCRIPT_SIMPLEX = int(C.CV_FONT_HERSHEY_SCRIPT_SIMPLEX)
|
||||
CV_FONT_HERSHEY_SCRIPT_COMPLEX = int(C.CV_FONT_HERSHEY_SCRIPT_COMPLEX)
|
||||
CV_FONT_ITALIC = int(C.CV_FONT_ITALIC)
|
||||
)
|
||||
|
||||
type Font struct {
|
||||
font C.CvFont
|
||||
}
|
||||
|
||||
//void cvInitFont(CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 )
|
||||
func InitFont(fontFace int, hscale, vscale, shear float32, thickness, lineType int) *Font {
|
||||
font := new(Font)
|
||||
C.cvInitFont(
|
||||
&font.font,
|
||||
C.int(fontFace),
|
||||
C.double(hscale),
|
||||
C.double(vscale),
|
||||
C.double(shear),
|
||||
C.int(thickness),
|
||||
C.int(lineType),
|
||||
)
|
||||
return font
|
||||
}
|
||||
|
||||
// void cvPutText(CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color)
|
||||
func (this *Font) PutText(image *IplImage, text string, pt1 Point, color Scalar) {
|
||||
C.cvPutText(
|
||||
unsafe.Pointer(image),
|
||||
C.CString(text),
|
||||
C.cvPoint(C.int(pt1.X), C.int(pt1.Y)),
|
||||
&this.font,
|
||||
(C.CvScalar)(color),
|
||||
)
|
||||
}
|
||||
|
||||
//CVAPI(void) cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
|
||||
// CvScalar color, int thickness CV_DEFAULT(1),
|
||||
// int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );
|
||||
|
|
|
|||
|
|
@ -1,9 +1,75 @@
|
|||
package opencv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadImage2(t *testing.T) {
|
||||
// t.Errorf("aaa")
|
||||
}
|
||||
|
||||
func TestInitFont(t *testing.T) {
|
||||
// Will assert at the C layer on error
|
||||
InitFont(CV_FONT_HERSHEY_DUPLEX, 1, 1, 0, 1, 8)
|
||||
}
|
||||
|
||||
func TestPutText(t *testing.T) {
|
||||
_, currentfile, _, _ := runtime.Caller(0)
|
||||
filename := path.Join(path.Dir(currentfile), "../images/lena.jpg")
|
||||
|
||||
image := LoadImage(filename)
|
||||
if image == nil {
|
||||
t.Fatal("LoadImage fail")
|
||||
}
|
||||
defer image.Release()
|
||||
|
||||
// Write 'Hello' on the image
|
||||
font := InitFont(CV_FONT_HERSHEY_DUPLEX, 1, 1, 0, 1, 8)
|
||||
color := NewScalar(255, 255, 255, 0)
|
||||
|
||||
pos := Point{image.Width() / 2, image.Height() / 2}
|
||||
font.PutText(image, "Hello", pos, color)
|
||||
|
||||
filename = path.Join(path.Dir(currentfile), "../images/lena_with_text.jpg")
|
||||
|
||||
// Uncomment this code to create the test image "../images/lena_with_text.jpg"
|
||||
// It is part of the repo, and what this test compares against
|
||||
//
|
||||
// SaveImage(filename, image, 0)
|
||||
// println("Saved file", filename)
|
||||
|
||||
tempfilename := path.Join(os.TempDir(), "lena_with_text.jpg")
|
||||
defer syscall.Unlink(tempfilename)
|
||||
|
||||
SaveImage(tempfilename, image, 0)
|
||||
|
||||
// Compare actual image with expected image
|
||||
same, err := BinaryCompare(filename, tempfilename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !same {
|
||||
t.Error("Actual file differs from expected file with text")
|
||||
}
|
||||
}
|
||||
|
||||
// Compare two files, return true if exactly the same
|
||||
func BinaryCompare(file1, file2 string) (bool, error) {
|
||||
f1, err := ioutil.ReadFile(file1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
f2, err := ioutil.ReadFile(file2)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return bytes.Equal(f1, f2), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue