Skip to content

Update find ellipses #3369

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

Merged
merged 1 commit into from
Dec 19, 2022
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
4 changes: 2 additions & 2 deletions modules/ximgproc/include/opencv2/ximgproc/find_ellipses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace ximgproc {
@brief Finds ellipses fastly in an image using projective invariant pruning.
*
* The function detects ellipses in images using projective invariant pruning.
* For more details about this implementation, please see
* [JIA2017FAST] Jia, Qi et al, (2017).
* For more details about this implementation, please see @cite jia2017fast
* Jia, Qi et al, (2017).
* A Fast Ellipse Detector using Projective Invariant Pruning. IEEE Transactions on Image Processing.
*
@param image input image, could be gray or color.
Expand Down
46 changes: 46 additions & 0 deletions modules/ximgproc/samples/find_ellipses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/python

'''
This example illustrates how to use cv.ximgproc.findEllipses function.

Usage:
find_ellipses.py [<image_name>]
image argument defaults to stuff.jpg
'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv
import sys
import math

def main():
try:
fn = sys.argv[1]
except IndexError:
fn = 'stuff.jpg'

src = cv.imread(cv.samples.findFile(fn))
cv.imshow("source", src)

ells = cv.ximgproc.findEllipses(src,scoreThreshold = 0.4, reliabilityThreshold = 0.7, centerDistanceThreshold = 0.02)

if ells is not None:
for i in range(len(ells)):
center = (int(ells[i][0][0]), int(ells[i][0][1]))
axes = (int(ells[i][0][2]),int(ells[i][0][3]))
angle = ells[i][0][5] * 180 / math.pi
color = (0, 0, 255)
cv.ellipse(src, center, axes, angle,0, 360, color, 2, cv.LINE_AA)

cv.imshow("detected ellipses", src)
cv.waitKey(0)
print('Done')


if __name__ == '__main__':
print(__doc__)
main()
cv.destroyAllWindows()
23 changes: 11 additions & 12 deletions modules/ximgproc/src/find_ellipses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ float EllipseDetectorImpl::getMedianSlope(std::vector<Point2f> &med, Point2f &ce
// centers : centroid of the points in med
// slopes : vector of the slopes

unsigned pointCount = med.size();
size_t pointCount = med.size();
// CV_Assert(pointCount >= 2);

unsigned halfSize = pointCount >> 1;
unsigned quarterSize = halfSize >> 1;
size_t halfSize = pointCount >> 1;
size_t quarterSize = halfSize >> 1;

std::vector<float> xx, yy;
slopes.reserve(halfSize);
Expand Down Expand Up @@ -1341,7 +1341,7 @@ void EllipseDetectorImpl::preProcessing(Mat1b &image, Mat1b &dp, Mat1b &dn) {
}

const int CANNY_SHIFT = 15;
const float TAN22_5 = 0.4142135623730950488016887242097; // tan(22.5) = sqrt(2) - 1
const float TAN22_5 = 0.4142135623730950488016887242097f; // tan(22.5) = sqrt(2) - 1
const int TG22 = (int) (TAN22_5 * (1 << CANNY_SHIFT) + 0.5);

// #define CANNY_PUSH(d) *(d) = (uchar)2, *stack_top++ = (d)
Expand Down Expand Up @@ -1723,8 +1723,8 @@ void EllipseDetectorImpl::findEllipses(Point2f &center, VP &edge_i, VP &edge_j,
}

// find peak in N and K accumulator
int iN = std::distance(accN, std::max_element(accN, accN + ACC_N_SIZE));
int iK = std::distance(accR, std::max_element(accR, accR + ACC_R_SIZE)) + 90;
int iN = (int)std::distance(accN, std::max_element(accN, accN + ACC_N_SIZE));
int iK = (int)std::distance(accR, std::max_element(accR, accR + ACC_R_SIZE)) + 90;

// recover real values
auto fK = float(iK);
Expand Down Expand Up @@ -1767,7 +1767,7 @@ void EllipseDetectorImpl::findEllipses(Point2f &center, VP &edge_i, VP &edge_j,
}

// find peak in A accumulator
int A = std::distance(accA, std::max_element(accA, accA + ACC_A_SIZE));
int A = (int)std::distance(accA, std::max_element(accA, accA + ACC_A_SIZE));
auto fA = float(A);

// find B value. See Eq [23] in the paper
Expand Down Expand Up @@ -1974,15 +1974,14 @@ void findEllipses(
edi.detect(grayImage, ellipseResults);

// convert - ellipse format to std::vector<Vec6f>
auto ellipseSize = unsigned(ellipseResults.size());
Mat _ellipses(1, ellipseSize, CV_32FC(6));
for (unsigned i = 0; i < ellipseSize; i++) {
std::vector<Vec6f> _ellipses;
for (size_t i = 0; i < ellipseResults.size(); i++) {
Ellipse tmpEll = ellipseResults[i];
Vec6f tmpVec(tmpEll.center.x, tmpEll.center.y, tmpEll.a, tmpEll.b, tmpEll.score,
tmpEll.radius);
_ellipses.at<Vec6f>(i) = tmpVec;
_ellipses.push_back(tmpVec);
}
_ellipses.copyTo(ellipses);
Mat(_ellipses).copyTo(ellipses);
}
} // namespace ximgproc
} // namespace cv