|
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 | | - int _col_num = cvRound((end_angle - start_angle) / theta); |
21 | | - int _row_num, _out_mat_type; |
22 | | - Point _center; |
23 | | - Mat _srcMat, _masked_src; |
| 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; |
24 | 24 |
|
25 | | - transpose(src, _srcMat); |
| 25 | + transpose(src, srcMat); |
26 | 26 |
|
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 | | - } |
33 | | - |
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); |
42 | | - // crop the source into circle |
43 | | - Mat _mask(_srcMat.size(), CV_8UC1, Scalar(0)); |
44 | | - _center = Point(_srcMat.cols / 2, _srcMat.rows / 2); |
45 | | - circle(_mask, _center, _srcMat.cols / 2, Scalar(255), FILLED); |
46 | | - _srcMat.copyTo(_masked_src, _mask); |
47 | | - } |
48 | | - else { |
49 | | - // avoid cropping corner when rotating |
50 | | - _row_num = cvCeil(sqrt(_srcMat.rows * _srcMat.rows + _srcMat.cols * _srcMat.cols)); |
51 | | - _masked_src = Mat(Size(_row_num, _row_num), _srcMat.type(), Scalar(0)); |
52 | | - _center = Point(_masked_src.cols / 2, _masked_src.rows / 2); |
53 | | - _srcMat.copyTo(_masked_src(Rect( |
54 | | - (_row_num - _srcMat.cols) / 2, |
55 | | - (_row_num - _srcMat.rows) / 2, |
56 | | - _srcMat.cols, _srcMat.rows))); |
57 | | - } |
| 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 | + } |
58 | 33 |
|
59 | | - Mat _radon(_row_num, _col_num, _out_mat_type); |
| 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); |
| 42 | + |
| 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 | + } |
60 | 59 |
|
61 | | - // Define the parallel loop as a lambda function |
62 | | - parallel_for_(Range(0, _col_num), [&](const Range& range) { |
63 | | - for (int _col = range.start; _col < range.end; _col++) { |
64 | | - // rotate the source by _t |
65 | | - double _t = (start_angle + _col * theta); |
66 | | - Mat _r_matrix = getRotationMatrix2D(_center, _t, 1); |
| 60 | + Mat radon(row_num, col_num, out_mat_type); |
67 | 61 |
|
68 | | - Mat _rotated_src; |
69 | | - warpAffine(_masked_src, _rotated_src, _r_matrix, _masked_src.size()); |
| 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); |
70 | 68 |
|
71 | | - Mat _col_mat = _radon.col(_col); |
72 | | - // make projection |
73 | | - reduce(_rotated_src, _col_mat, 1, REDUCE_SUM, _out_mat_type); |
74 | | - } |
75 | | - }); |
| 69 | + Mat rotated_src; |
| 70 | + warpAffine(masked_src, rotated_src, r_matrix, masked_src.size()); |
76 | 71 |
|
77 | | - if (norm) { |
78 | | - normalize(_radon, _radon, 0, 255, NORM_MINMAX, CV_8UC1); |
| 72 | + Mat col_mat = radon.col(col); |
| 73 | + // Make projection |
| 74 | + reduce(rotated_src, col_mat, 1, REDUCE_SUM, out_mat_type); |
79 | 75 | } |
| 76 | + }); |
80 | 77 |
|
81 | | - _radon.copyTo(dst); |
| 78 | + if (norm) { |
| 79 | + normalize(radon, radon, 0, 255, NORM_MINMAX, CV_8UC1); |
82 | 80 | } |
| 81 | + |
| 82 | + radon.copyTo(dst); |
| 83 | +} |
83 | 84 | } } |
0 commit comments