Skip to content

Commit

Permalink
RotatedRect type constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce committed Aug 20, 2024
1 parent 0f1afaf commit 6de0be3
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
54 changes: 54 additions & 0 deletions core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,3 +1235,57 @@ void SetNumThreads(int n) {
int GetNumThreads() {
return cv::getNumThreads();
}

struct RotatedRect RotatedRect_Create(struct Point2f center, int width, int height, float angle){

cv::Point2f cvpoint2f = cv::Point2f(center.x, center.y);
cv::Size2f cvsize2f = cv::Size2f(width, height);

cv::RotatedRect cvrect = cv::RotatedRect(cvpoint2f, cvsize2f, angle);

Point* rpts = new Point[4];
cv::Point2f* pts4 = new cv::Point2f[4];
cvrect.points(pts4);

for (size_t j = 0; j < 4; j++) {
Point pt = {int(lroundf(pts4[j].x)), int(lroundf(pts4[j].y))};
rpts[j] = pt;
}

delete[] pts4;

cv::Rect bRect = cvrect.boundingRect();
Rect r = {bRect.x, bRect.y, bRect.width, bRect.height};
Point centrpt = {int(lroundf(cvrect.center.x)), int(lroundf(cvrect.center.y))};
Size szsz = {int(lroundf(cvrect.size.width)), int(lroundf(cvrect.size.height))};

RotatedRect retrect = {(Contour){rpts, 4}, r, centrpt, szsz, cvrect.angle};
return retrect;
}

struct RotatedRect2f RotatedRect2f_Create(struct Point2f center, float width, float height, float angle){

cv::Point2f cvpoint2f = cv::Point2f(center.x, center.y);
cv::Size2f cvsize2f = cv::Size2f(width, height);

cv::RotatedRect cvrect = cv::RotatedRect(cvpoint2f, cvsize2f, angle);

Point2f* rpts = new Point2f[4];
cv::Point2f* pts4 = new cv::Point2f[4];
cvrect.points(pts4);

for (size_t j = 0; j < 4; j++) {
Point2f pt = {pts4[j].x, pts4[j].y};
rpts[j] = pt;
}

delete[] pts4;

cv::Rect bRect = cvrect.boundingRect();
Rect r = {bRect.x, bRect.y, bRect.width, bRect.height};
Point2f centrpt = {cvrect.center.x, cvrect.center.y};
Size2f szsz = {cvrect.size.width, cvrect.size.height};

RotatedRect2f retrect = {(Contour2f){rpts, 4}, r, centrpt, szsz, cvrect.angle};
return retrect;
}
47 changes: 47 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2870,3 +2870,50 @@ func SetNumThreads(n int) {
func GetNumThreads() int {
return int(C.GetNumThreads())
}

// NewRotatedRect creates [RotatedRect] (i.e. not up-right) rectangle on a plane.
//
// For further information, see:
// https://docs.opencv.org/4.x/db/dd6/classcv_1_1RotatedRect.html#aba20dfc8444fff72bd820b616f0297ee
func NewRotatedRect(center image.Point, width int, height int, angle float64) RotatedRect {

p2f := C.struct_Point2f{
x: C.float(float32(center.X)),
y: C.float(float32(center.Y)),
}

c_rotRect := C.RotatedRect_Create(p2f, C.int(width), C.int(height), C.float(angle))
defer C.Points_Close(c_rotRect.pts)

return RotatedRect{
Points: toPoints(c_rotRect.pts),
BoundingRect: image.Rect(int(c_rotRect.boundingRect.x), int(c_rotRect.boundingRect.y), int(c_rotRect.boundingRect.x)+int(c_rotRect.boundingRect.width), int(c_rotRect.boundingRect.y)+int(c_rotRect.boundingRect.height)),
Center: image.Pt(int(c_rotRect.center.x), int(c_rotRect.center.y)),
Width: int(c_rotRect.size.width),
Height: int(c_rotRect.size.height),
Angle: float64(c_rotRect.angle),
}

}

// NewRotatedRect2f creates [RotatedRect2f] (i.e. not up-right) rectangle on a plane.
//
// For further information, see:
// https://docs.opencv.org/4.x/db/dd6/classcv_1_1RotatedRect.html#aba20dfc8444fff72bd820b616f0297ee
func NewRotatedRect2f(center Point2f, width float32, height float32, angle float64) RotatedRect2f {
p2f := C.struct_Point2f{
x: C.float(center.X),
y: C.float(center.Y),
}
c_rotRect2f := C.RotatedRect2f_Create(p2f, C.float(width), C.float(height), C.float(angle))
defer C.Points2f_Close(c_rotRect2f.pts)

return RotatedRect2f{
Points: toPoints2f(c_rotRect2f.pts),
BoundingRect: image.Rect(int(c_rotRect2f.boundingRect.x), int(c_rotRect2f.boundingRect.y), int(c_rotRect2f.boundingRect.x)+int(c_rotRect2f.boundingRect.width), int(c_rotRect2f.boundingRect.y)+int(c_rotRect2f.boundingRect.height)),
Center: NewPoint2f(float32(c_rotRect2f.center.x), float32(c_rotRect2f.center.y)),
Width: float32(c_rotRect2f.size.width),
Height: float32(c_rotRect2f.size.height),
Angle: float64(c_rotRect2f.angle),
}
}
17 changes: 16 additions & 1 deletion core.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ typedef struct Rect {
int height;
} Rect;

// Wrapper for an individual cv::cvRect2f
typedef struct Rect2f {
float x;
float y;
float width;
float height;
} Rect2f;

// Wrapper for the vector of Rect struct aka std::vector<Rect>
typedef struct Rects {
Rect* rects;
Expand Down Expand Up @@ -136,7 +144,7 @@ typedef struct RotatedRect {
double angle;
} RotatedRect;

// Wrapper for an individual cv::RotatedRect
// Wrapper for an individual cv::RotatedRect2f
typedef struct RotatedRect2f {
Points2f pts;
Rect boundingRect;
Expand Down Expand Up @@ -253,6 +261,7 @@ typedef std::vector< cv::Point2f >* Point2fVector;
typedef std::vector< std::vector< cv::Point2f> >* Points2fVector;
typedef std::vector< cv::Point3f >* Point3fVector;
typedef std::vector< std::vector< cv::Point3f > >* Points3fVector;
typedef cv::RotatedRect* RotatedRectT;
#else
typedef void* Mat;
typedef void* TermCriteria;
Expand All @@ -263,6 +272,7 @@ typedef void* Point2fVector;
typedef void* Points2fVector;
typedef void* Point3fVector;
typedef void* Points3fVector;
typedef void* RotatedRectT;
#endif

// Wrapper for the vector of Mat aka std::vector<Mat>
Expand Down Expand Up @@ -547,6 +557,11 @@ void Points3fVector_Close(Points3fVector ps);
void SetNumThreads(int n);
int GetNumThreads();


struct RotatedRect RotatedRect_Create(struct Point2f center, int width, int height, float angle);
struct RotatedRect2f RotatedRect2f_Create(struct Point2f center, float width, float height, float angle);


#ifdef __cplusplus
}
#endif
Expand Down
23 changes: 23 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3381,3 +3381,26 @@ func TestMinMaxLocWithMask(t *testing.T) {
t.Errorf("maxLoc got: %v, want %v", maxLoc, wantMaxLoc)
}
}

func TestNewRotatedRect(t *testing.T) {

rr := NewRotatedRect(image.Pt(1, 1), 10, 10, 75.0)
if rr.Angle != 75.0 {
t.Errorf("NewRotatedRect not working as intended")
}

}

func TestNewRotatedRect2f(t *testing.T) {

pts := Point2f{
X: 1.5,
Y: 1.5,
}

rr := NewRotatedRect2f(pts, 10.5, 10.5, 75.0)
if rr.Angle != 75.0 {
t.Errorf("NewRotatedRect not working as intended")
}

}

0 comments on commit 6de0be3

Please sign in to comment.