1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
| #include <iostream> #include <string> #include <sstream> #include <ctime> #include <vector> #include <cmath> #include <opencv2/core.hpp> #include <opencv2/videoio.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/videoio.hpp> #include <opencv2/highgui.hpp> #include <opencv2/video.hpp>
using namespace std; using namespace cv;
const double PI = 3.14159265358979323846;
class ToughTransform{ public: void LineDetection(Mat& Image); void CircleDetection(Mat& Image); private: void ShowParameterSpace(vector<vector<int>>& PS_AfterFilter, string file_name, int thetaScale); void DrawLineDetectedImage(Mat& Image, vector<vector<int>>& PS_AfterFilter, int row_max, int theta_max , int second_filter_threshold); void ShowParameterSpace_Circle(vector<vector<vector<int>>>& PS_AfterFilter, string file_name, int thetaScale); void DrawCircleDetectedImage(Mat& Image,vector<vector<vector<int>>>& PS_AfterFilter, int max_a, int max_b, int max_r, int second_filter_threshold); };
void ToughTransform::ShowParameterSpace(vector<vector<int>>& PS_AfterFilter, string file_name, int thetaScale = 15) { int rows = PS_AfterFilter.size(); int cols = PS_AfterFilter[0].size();
Mat houghSpaceImage(rows, cols, CV_8UC1, Scalar(0)); int maxVal = 0; for (const auto& row : PS_AfterFilter) { maxVal = max(maxVal, *max_element(row.begin(), row.end())); } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { houghSpaceImage.at<uchar>(i, j) = static_cast<uchar>(255.0 * PS_AfterFilter[i][j] / maxVal); } } Mat enlargedHoughSpace; resize(houghSpaceImage, enlargedHoughSpace, Size(cols * thetaScale, rows), 0, 0, INTER_LINEAR); Mat colorHoughSpace; applyColorMap(enlargedHoughSpace, colorHoughSpace, COLORMAP_JET);
namedWindow("Enhanced Hough Space", WINDOW_NORMAL); imshow("Enhanced Hough Space", colorHoughSpace); imwrite(file_name, colorHoughSpace); waitKey(0); }
void ToughTransform::DrawLineDetectedImage(Mat& Image, vector<vector<int>>& PS_AfterFilter, int row_max, int theta_max = 180, int second_filter_threshold = 100) { vector<vector<int>> PS_FinalFilter(2 * static_cast<int>(ceil(row_max)), vector<int>(theta_max, 0));
for (int i = 0; i < PS_AfterFilter.size(); i++){ for (int j = 0; j < PS_AfterFilter[0].size(); j++){ if(PS_AfterFilter[i][j] >= second_filter_threshold) PS_FinalFilter[i][j] = PS_AfterFilter[i][j]; else PS_FinalFilter[i][j] = 0; } } ShowParameterSpace(PS_FinalFilter,"source/line-result/EnhancedHoughSpaceImage-FinalFilter.png");
int rows = PS_FinalFilter.size(); int cols = PS_FinalFilter[0].size(); for (int row = 0; row < rows; row++) { for (int theta = 0; theta < cols; theta++) { if (PS_FinalFilter[row][theta] != 0) { double this_theta_rad = theta * PI / 180.0; double this_row = row - row_max;
Point pt1, pt2; double a = cos(this_theta_rad); double b = sin(this_theta_rad); double x0 = a * this_row; double y0 = b * this_row; pt1.x = cvRound(x0 + 1000 * (-b)); pt1.y = cvRound(y0 + 1000 * (a)); pt2.x = cvRound(x0 - 1000 * (-b)); pt2.y = cvRound(y0 - 1000 * (a));
line(Image, pt1, pt2, Scalar(0, 0, 255), 2, LINE_AA); } } }
namedWindow("Detected Lines", WINDOW_NORMAL); imshow("Detected Lines", Image); imwrite("source/line-result/DetectedLinesImage.png", Image); waitKey(0); }
void ToughTransform::LineDetection(Mat& Image){ int img_width = Image.size().width; int img_height = Image.size().height; double row_max = sqrt(img_width * img_width + img_height * img_height); int theta_max = 180; vector<vector<int>> PatameterSpace(2 * static_cast<int>(ceil(row_max)), vector<int>(theta_max, 0)); cout << "ImageSize:" << img_height << "*" << img_width << endl; cout << "PatameterSpace:" << 2*row_max << "*" << theta_max << " --> " << 2 * static_cast<int>(ceil(row_max)) << "*" << theta_max << endl;
for (int y = 0; y < img_height; y++){ for (int x = 0; x < img_width; x++){ int B = Image.at<Vec3b>(y,x)[0]; int G = Image.at<Vec3b>(y,x)[1]; int R = Image.at<Vec3b>(y,x)[2]; if(B == 0 && G == 0 && R == 0){ for(int theta = 0; theta <= theta_max; theta++){ double theta_rad = theta * PI / 180.0; double row = x * cos(theta_rad) + y * sin(theta_rad); PatameterSpace[static_cast<int>(ceil(row) + ceil(row_max))][theta]++; } } } } int threshold = 10; vector<vector<int>> PS_AfterFilter(2 * static_cast<int>(ceil(row_max)), vector<int>(theta_max, 0)); for (int i = 0; i < PatameterSpace.size(); i++){ for (int j = 0; j < PatameterSpace[0].size(); j++){ if(PatameterSpace[i][j] >= threshold) PS_AfterFilter[i][j] = PatameterSpace[i][j]; else PS_AfterFilter[i][j] = 0; } } ShowParameterSpace(PS_AfterFilter,"source/line-result/EnhancedHoughSpaceImage-FirstFilter.png");
int second_filter_threshold = 500; DrawLineDetectedImage(Image,PS_AfterFilter,static_cast<int>(ceil(row_max)),theta_max,second_filter_threshold); }
void ToughTransform::ShowParameterSpace_Circle(vector<vector<vector<int>>>& PS_AfterFilter, string file_name, int thetaScale = 1){ int height = PS_AfterFilter.size(); int width = PS_AfterFilter[0].size(); int num_r = PS_AfterFilter[0][0].size(); Mat parameter_space_image(height, width, CV_8UC1, cv::Scalar(0));
for (int b = 0; b < height; b++) { for (int a = 0; a < width; a++) { for (int r = 0; r < num_r; r++) { int accumulator_value = PS_AfterFilter[b][a][r]; int intensity = static_cast<int>(255.0 * accumulator_value / 255.0); parameter_space_image.at<uchar>(b, a) = intensity; } } }
Mat enlargedHoughSpace; resize(parameter_space_image, enlargedHoughSpace, Size(width * thetaScale, height), 0, 0, INTER_LINEAR); Mat colorHoughSpace; applyColorMap(enlargedHoughSpace, colorHoughSpace, COLORMAP_JET);
namedWindow("Detected Circles", WINDOW_NORMAL); imshow("Detected Circles", colorHoughSpace); imwrite(file_name, colorHoughSpace); waitKey(0); }
void ToughTransform::DrawCircleDetectedImage(Mat& Image,vector<vector<vector<int>>>& PS_AfterFilter, int max_a, int max_b, int max_r, int second_filter_threshold){ vector<vector<vector<int>>> PS_FinalFilter(max_b, vector<vector<int>>(max_a, vector<int>(max_r) ));
for (int i = 0; i < PS_AfterFilter.size(); i++){ for (int j = 0; j < PS_AfterFilter[0].size(); j++){ for (int k = 0; k < PS_AfterFilter[0][0].size(); k++){ if(PS_AfterFilter[i][j][k] >= second_filter_threshold) PS_FinalFilter[i][j][k] = PS_AfterFilter[i][j][k]; else PS_FinalFilter[i][j][k] = 0; } } } ShowParameterSpace_Circle(PS_FinalFilter,"source/circle-result/EnhancedHoughSpaceImage-FinalFilter.png");
for (int i = 0; i < PS_FinalFilter.size(); i++){ for (int j = 0; j < PS_FinalFilter[0].size(); j++){ for (int k = 0; k < PS_FinalFilter[0][0].size(); k++){ if(PS_FinalFilter[i][j][k] != 0){ Point center(j, i); circle(Image, center, k, cv::Scalar(0, 0, 255), 2); } } } }
namedWindow("Detected Circles", WINDOW_NORMAL); imshow("Detected Circles", Image); imwrite("source/circle-result/DrawCircleDetectedImage.png", Image); waitKey(0); }
void ToughTransform::CircleDetection(Mat& Image){ int img_width = Image.size().width; int img_height = Image.size().height; int max_a = img_width; int max_b = img_height; int max_r = static_cast<int>(0.5* min(max_a,max_b)); vector<vector<vector<int>>> ParameterSpace(max_b, vector<vector<int>>(max_a, vector<int>(max_r) ));
for (int y = 0; y < img_height; y++){ for (int x = 0; x < img_width; x++){ int B = Image.at<Vec3b>(y,x)[0]; int G = Image.at<Vec3b>(y,x)[1]; int R = Image.at<Vec3b>(y,x)[2]; if(B == 0 && G == 0 && R == 0){ for(int r = 20; r <= max_r; r++){ for(int theta = 0; theta < 360; theta++){ double theta_rad = theta * PI / 180.0; int a = static_cast<int>(x - r * cos(theta_rad)); int b = static_cast<int>(y - r * sin(theta_rad)); if(a >= 0 && a < max_a && b >= 0 && b < max_b){ ParameterSpace[b][a][r]++; } } } } } }
int threshold = 10; vector<vector<vector<int>>> PS_AfterFilter(max_b, vector<vector<int>>(max_a, vector<int>(max_r) )); for (int i = 0; i < ParameterSpace.size(); i++){ for (int j = 0; j < ParameterSpace[0].size(); j++){ for (int k = 0; k < ParameterSpace[0][0].size(); k++){ if(ParameterSpace[i][j][k] >= threshold) PS_AfterFilter[i][j][k] = ParameterSpace[i][j][k]; else PS_AfterFilter[i][j][k] = 0; } } }
ShowParameterSpace_Circle(PS_AfterFilter,"source/circle-result/EnhancedHoughSpaceImage-FirstFilter.png");
const int second_filter_threshold = 300; DrawCircleDetectedImage(Image,PS_AfterFilter,max_a,max_b,max_r,second_filter_threshold); }
int main(){
ToughTransform handle;
Mat img3 = imread("source/line_circle.png"); handle.CircleDetection(img3);
return 0; }
|
预览: