|
5 | 5 | #include "precomp.hpp"
|
6 | 6 |
|
7 | 7 | namespace cv {namespace ximgproc {
|
8 |
| - void RadonTransform(InputArray src, |
9 |
| - OutputArray dst, |
10 |
| - double theta, |
11 |
| - double start_angle, |
12 |
| - double end_angle, |
13 |
| - bool crop, |
14 |
| - bool norm) |
15 |
| - { |
16 |
| - CV_Assert(src.dims() == 2); |
17 |
| - CV_Assert(src.channels() == 1); |
18 |
| - CV_Assert((end_angle - start_angle) * theta > 0); |
| 8 | +void RadonTransform(InputArray src, |
| 9 | + OutputArray dst, |
| 10 | + double theta, |
| 11 | + double start_angle, |
| 12 | + double end_angle, |
| 13 | + bool crop, |
| 14 | + bool norm) |
| 15 | +{ |
| 16 | + CV_Assert(src.dims() == 2); |
| 17 | + CV_Assert(src.channels() == 1); |
| 18 | + CV_Assert((end_angle - start_angle) * theta > 0); |
19 | 19 |
|
20 |
| - Mat _srcMat = src.getMat(); |
| 20 | + int col_num = cvRound((end_angle - start_angle) / theta); |
| 21 | + int row_num, out_mat_type; |
| 22 | + Point center; |
| 23 | + Mat srcMat, masked_src; |
21 | 24 |
|
22 |
| - int _row_num, _col_num, _out_mat_type; |
23 |
| - _col_num = cvRound((end_angle - start_angle) / theta); |
24 |
| - transpose(_srcMat, _srcMat); |
25 |
| - Mat _masked_src; |
26 |
| - cv::Point _center; |
| 25 | + transpose(src, srcMat); |
27 | 26 |
|
28 |
| - if (_srcMat.type() == CV_32FC1 || _srcMat.type() == CV_64FC1) { |
29 |
| - _out_mat_type = CV_64FC1; |
30 |
| - } |
31 |
| - else { |
32 |
| - _out_mat_type = CV_32SC1; |
33 |
| - } |
| 27 | + if (srcMat.type() == CV_32FC1 || srcMat.type() == CV_64FC1) { |
| 28 | + out_mat_type = CV_64FC1; |
| 29 | + } |
| 30 | + else { |
| 31 | + out_mat_type = CV_32SC1; |
| 32 | + } |
34 | 33 |
|
35 |
| - if (crop) { |
36 |
| - // crop the source into square |
37 |
| - _row_num = min(_srcMat.rows, _srcMat.cols); |
38 |
| - cv::Rect _crop_ROI( |
39 |
| - _srcMat.cols / 2 - _row_num / 2, |
40 |
| - _srcMat.rows / 2 - _row_num / 2, |
41 |
| - _row_num, _row_num); |
42 |
| - _srcMat = _srcMat(_crop_ROI); |
43 |
| - // crop the source into circle |
44 |
| - Mat _mask(_srcMat.size(), CV_8UC1, Scalar(0)); |
45 |
| - _center = Point(_srcMat.cols / 2, _srcMat.rows / 2); |
46 |
| - circle(_mask, _center, _srcMat.cols / 2, Scalar(255), FILLED); |
47 |
| - _srcMat.copyTo(_masked_src, _mask); |
48 |
| - } |
49 |
| - else { |
50 |
| - // avoid cropping corner when rotating |
51 |
| - _row_num = cvCeil(sqrt(_srcMat.rows * _srcMat.rows + _srcMat.cols * _srcMat.cols)); |
52 |
| - _masked_src = Mat(Size(_row_num, _row_num), _srcMat.type(), Scalar(0)); |
53 |
| - _center = Point(_masked_src.cols / 2, _masked_src.rows / 2); |
54 |
| - _srcMat.copyTo(_masked_src(Rect( |
55 |
| - (_row_num - _srcMat.cols) / 2, |
56 |
| - (_row_num - _srcMat.rows) / 2, |
57 |
| - _srcMat.cols, _srcMat.rows))); |
58 |
| - } |
| 34 | + if (crop) { |
| 35 | + // Crop the source into square |
| 36 | + row_num = min(srcMat.rows, srcMat.cols); |
| 37 | + Rect crop_ROI( |
| 38 | + srcMat.cols / 2 - row_num / 2, |
| 39 | + srcMat.rows / 2 - row_num / 2, |
| 40 | + row_num, row_num); |
| 41 | + srcMat = srcMat(crop_ROI); |
59 | 42 |
|
60 |
| - double _t; |
61 |
| - Mat _rotated_src; |
62 |
| - Mat _radon(_row_num, _col_num, _out_mat_type); |
| 43 | + // Crop the source into circle |
| 44 | + Mat mask(srcMat.size(), CV_8UC1, Scalar(0)); |
| 45 | + center = Point(srcMat.cols / 2, srcMat.rows / 2); |
| 46 | + circle(mask, center, srcMat.cols / 2, Scalar(255), FILLED); |
| 47 | + srcMat.copyTo(masked_src, mask); |
| 48 | + } |
| 49 | + else { |
| 50 | + // Avoid cropping corner when rotating |
| 51 | + row_num = cvCeil(sqrt(srcMat.rows * srcMat.rows + srcMat.cols * srcMat.cols)); |
| 52 | + masked_src = Mat(Size(row_num, row_num), srcMat.type(), Scalar(0)); |
| 53 | + center = Point(masked_src.cols / 2, masked_src.rows / 2); |
| 54 | + srcMat.copyTo(masked_src(Rect( |
| 55 | + (row_num - srcMat.cols) / 2, |
| 56 | + (row_num - srcMat.rows) / 2, |
| 57 | + srcMat.cols, srcMat.rows))); |
| 58 | + } |
63 | 59 |
|
64 |
| - for (int _col = 0; _col < _col_num; _col++) { |
65 |
| - // rotate the source by _t |
66 |
| - _t = (start_angle + _col * theta); |
67 |
| - cv::Mat _r_matrix = cv::getRotationMatrix2D(_center, _t, 1); |
68 |
| - cv::warpAffine(_masked_src, _rotated_src, _r_matrix, _masked_src.size()); |
69 |
| - Mat _col_mat = _radon.col(_col); |
70 |
| - // make projection |
71 |
| - cv::reduce(_rotated_src, _col_mat, 1, REDUCE_SUM, _out_mat_type); |
72 |
| - } |
| 60 | + Mat radon(row_num, col_num, out_mat_type); |
73 | 61 |
|
74 |
| - if (norm) { |
75 |
| - normalize(_radon, _radon, 0, 255, NORM_MINMAX, CV_8UC1); |
| 62 | + // Define the parallel loop as a lambda function |
| 63 | + parallel_for_(Range(0, col_num), [&](const Range& range) { |
| 64 | + for (int col = range.start; col < range.end; col++) { |
| 65 | + // Rotate the source by t |
| 66 | + double t = (start_angle + col * theta); |
| 67 | + Mat r_matrix = getRotationMatrix2D(center, t, 1); |
| 68 | + |
| 69 | + Mat rotated_src; |
| 70 | + warpAffine(masked_src, rotated_src, r_matrix, masked_src.size()); |
| 71 | + |
| 72 | + Mat col_mat = radon.col(col); |
| 73 | + // Make projection |
| 74 | + reduce(rotated_src, col_mat, 1, REDUCE_SUM, out_mat_type); |
76 | 75 | }
|
| 76 | + }); |
77 | 77 |
|
78 |
| - _radon.copyTo(dst); |
79 |
| - return; |
| 78 | + if (norm) { |
| 79 | + normalize(radon, radon, 0, 255, NORM_MINMAX, CV_8UC1); |
80 | 80 | }
|
| 81 | + |
| 82 | + radon.copyTo(dst); |
| 83 | +} |
81 | 84 | } }
|
0 commit comments