5 size_t getNumLines(
const Mat &img,
int threshold1,
int threshold2)
9 if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
12 GaussianBlur(I, I, Size(5, 5), 0.75, 0.75);
15 Canny(I, canny8b, threshold1 / 2, threshold1, 3);
18 std::vector<Vec2f> vLines;
26 std::vector<Vec4i> vLines;
39 imshow(
"Canny", canny8b);
42 if (img.channels() == 1) cvtColor(img, tmp, cv::ColorConversionCodes::COLOR_GRAY2RGB);
46 for (Vec2f &l : vLines) {
49 double a = cos(theta);
50 double b = sin(theta);
54 pt1.x = cvRound(x0 - 1000 * b);
55 pt1.y = cvRound(y0 + 1000 * a);
56 pt2.x = cvRound(x0 + 1000 * b);
57 pt2.y = cvRound(y0 - 1000 * a);
58 line(tmp, pt1, pt2, CV_RGB(255, 0, 0), 1, cv::LineTypes::LINE_AA);
61 for (Vec4i &l : vLines)
62 line(tmp, Point(l[0], l[1]), Point(l[2], l[3]), CV_RGB(255, 0, 0), 1, cv::LineTypes::LINE_AA);
64 imshow(
"detected lines", tmp);
75 if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
78 GaussianBlur(I, I, Size(9, 9), 2, 2);
80 std::vector<Vec3f> vCircles;
83 , cv::HoughModes::HOUGH_GRADIENT
92 Canny(I, canny8b, threshold1 / 2, threshold1, 3);
93 imshow(
"Canny", canny8b);
96 if (img.channels() == 1) cvtColor(img, tmp, cv::ColorConversionCodes::COLOR_GRAY2RGB);
99 for (Vec3f &c : vCircles) {
100 Point center(cvRound(c[0]), cvRound(c[1]));
101 int radius = cvRound(c[2]);
102 circle(tmp, center, 3, CV_RGB(0, 255, 0), -1, 8, 0);
103 circle(tmp, center, radius, CV_RGB(255, 0, 0), 1, 8, 0);
105 imshow(
"circles", tmp);
109 return vCircles.size();
114 int width = img.cols;
115 int height = img.rows;
120 if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
123 float _mean =
static_cast<float>(mean(I)[0]);
126 for (
int y = 0; y < height; y++) {
127 byte *pI = I.ptr<byte>(y);
128 for (
int x = 0; x < width; x++) {
129 float dx = x - 0.5f * width;
130 float dy = y - 0.5f * height;
131 float dist = sqrtf(dx*dx + dy*dy);
133 float weight = 1.0f - dist / R;
134 res += weight * fabs(static_cast<float>(pI[x]) - _mean);
138 return res / (width * height);
145 if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
149 meanStdDev(I, mean, stddev);
150 float res =
static_cast<float>(stddev[0] * stddev[0]);
159 if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
163 for (
int y = 0; y < I.rows; y++) {
164 byte *pI = I.ptr<byte>(y);
165 for (
int x = 0; x < I.cols; x++)
166 if (pI[x] > 0) res++;
176 if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
179 Mat tmp(I.size(), CV_8UC1, Scalar(0));
181 for (
int y = 1; y < I.rows; y++) {
182 byte *pTmp = tmp.ptr<byte>(y);
183 byte *pI = I.ptr<byte>(y);
184 byte *pI1 = I.ptr<byte>(y - 1);
185 for (
int x = 1; x < I.cols; x++)
186 if ((pI[x] != pI[x - 1]) || (pI[x] != pI1[x])) pTmp[x] = 255;
196 float S =
static_cast<float>(
getArea(img));
198 return (S > 0) ? P * P / (S * 4 * Pif) : 0;
float getCompactness(const Mat &img)
Returns the compactness of the object in the source image.
float getOpacity(const Mat &img)
Returns the weighted-mean transparancy of the source image.
int getArea(const Mat &img)
Returns the number of non-zero pixels in the source image.
int getPerimeter(const Mat &img)
Returns the perimeter of an object in the source image.
float getVariance(const Mat &img)
Retunrs the variance of the source image.
size_t getNumCircles(const Mat &img, int threshold1, int threshold2)
Returns the number of circles in the source image.
size_t getNumLines(const Mat &img, int threshold1, int threshold2)
Returns the number of staight lines in the image.