Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new binding for cv::Erode. #1158

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions imgproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ void ErodeWithParams(Mat src, Mat dst, Mat kernel, Point anchor, int iterations,
cv::erode(*src, *dst, *kernel, pt1, iterations, borderType, cv::morphologyDefaultBorderValue());
}

void ErodeWithParamsAndBorderValue(Mat src, Mat dst, Mat kernel, Point anchor, int iterations, int borderType, Scalar borderValue) {
cv::Point pt1(anchor.x, anchor.y);
cv::Scalar c = cv::Scalar(borderValue.val1, borderValue.val2, borderValue.val3, borderValue.val4);

cv::erode(*src, *dst, *kernel, pt1, iterations, borderType, c);
}


void MatchTemplate(Mat image, Mat templ, Mat result, int method, Mat mask) {
cv::matchTemplate(*image, *templ, *result, method, *mask);
}
Expand Down
22 changes: 22 additions & 0 deletions imgproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,28 @@ func ErodeWithParams(src Mat, dst *Mat, kernel Mat, anchor image.Point, iteratio
C.ErodeWithParams(src.p, dst.p, kernel.p, cAnchor, C.int(iterations), C.int(borderType))
}

// ErodeWithParamsAndBorderValue erodes an image by using a specific structuring
// element. Same as ErodeWithParams but requires an additional borderValue
// parameter.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaeb1e0c1033e3f6b891a25d0511362aeb
func ErodeWithParamsAndBorderValue(src Mat, dst *Mat, kernel Mat, anchor image.Point, iterations, borderType int, borderValue Scalar) {
cAnchor := C.struct_Point{
x: C.int(anchor.X),
y: C.int(anchor.Y),
}

bv := C.struct_Scalar{
val1: C.double(borderValue.Val1),
val2: C.double(borderValue.Val2),
val3: C.double(borderValue.Val3),
val4: C.double(borderValue.Val4),
}

C.ErodeWithParamsAndBorderValue(src.p, dst.p, kernel.p, cAnchor, C.int(iterations), C.int(borderType), bv)
}

// RetrievalMode is the mode of the contour retrieval algorithm.
type RetrievalMode int

Expand Down
1 change: 1 addition & 0 deletions imgproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void DilateWithParams(Mat src, Mat dst, Mat kernel, Point anchor, int iterations
void DistanceTransform(Mat src, Mat dst, Mat labels, int distanceType, int maskSize, int labelType);
void Erode(Mat src, Mat dst, Mat kernel);
void ErodeWithParams(Mat src, Mat dst, Mat kernel, Point anchor, int iterations, int borderType);
void ErodeWithParamsAndBorderValue(Mat src, Mat dst, Mat kernel, Point anchor, int iterations, int borderType, Scalar borderValue);
void MatchTemplate(Mat image, Mat templ, Mat result, int method, Mat mask);
struct Moment Moments(Mat src, bool binaryImage);
void PyrDown(Mat src, Mat dst, Size dstsize, int borderType);
Expand Down
19 changes: 19 additions & 0 deletions imgproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,25 @@ func TestErodeWithParams(t *testing.T) {
}
}

func TestErodeWithParamsAndBorderValue(t *testing.T) {
img := IMRead("images/face-detect.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid read of Mat in ErodeWithParamsAndBorderValue test")
}
defer img.Close()

dest := NewMat()
defer dest.Close()

kernel := GetStructuringElement(MorphRect, image.Pt(1, 1))
defer kernel.Close()

ErodeWithParamsAndBorderValue(img, &dest, kernel, image.Pt(-1, -1), 3, 0, NewScalar(0,0,0,0))
if dest.Empty() || img.Rows() != dest.Rows() || img.Cols() != dest.Cols() {
t.Error("Invalid ErodeWithParamsAndBorderValue test")
}
}

func TestMorphologyDefaultBorderValue(t *testing.T) {
zeroScalar := Scalar{}
morphologyDefaultBorderValue := MorphologyDefaultBorderValue()
Expand Down
Loading