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
| /** * 计算8个方向的高斯导数 * @param src CV_8UC1 * @param gauss_vector */ void calc_8_direction_guass(const Mat &src, vector<Mat> &gauss_vector) { // cout << src.channels() << endl;
Mat gauss, gauss_pos, gauss_neg; Mat rotated, rotated_gauss; Mat rotated_gauss_tmp;
// x轴,向左/向右 Scharr(src, gauss, CV_32F, 1, 0); threshold(gauss, gauss_pos, 0, 0, THRESH_TOZERO); threshold(gauss, gauss_neg, 0, 0, THRESH_TOZERO_INV); gauss_vector.emplace_back(gauss_pos); gauss_vector.emplace_back(gauss_neg);
// y轴,向上/向下 gauss.release(); Scharr(src, gauss, CV_32F, 0, 1);
gauss_pos.release(); gauss_neg.release(); threshold(gauss, gauss_pos, 0, 0, THRESH_TOZERO); threshold(gauss, gauss_neg, 0, 0, THRESH_TOZERO_INV); gauss_vector.emplace_back(gauss_pos); gauss_vector.emplace_back(gauss_neg);
// 逆时针旋转45度 Point2f center(src.cols / 2.0f, src.rows / 2.0f); Mat rot = cv::getRotationMatrix2D(center, 45.0, 1.0); Rect bbox = cv::RotatedRect(center, src.size(), 45.0).boundingRect(); rot.at<double>(0, 2) += bbox.width / 2.0 - center.x; rot.at<double>(1, 2) += bbox.height / 2.0 - center.y; warpAffine(src, rotated, rot, bbox.size()); // cout << rotated.size() << endl;
// 计算x轴方向导数 Scharr(rotated, rotated_gauss, CV_32F, 1, 0);
// 顺时针旋转45度,获取原先图像大小 center = Point((int) (rotated.cols / 2.0), (int) (rotated.rows / 2.0)); rot = cv::getRotationMatrix2D(center, -45.0, 1.0); warpAffine(rotated_gauss, rotated_gauss_tmp, rot, bbox.size()); gauss = rotated_gauss_tmp(Rect((bbox.width - src.cols) / 2, (bbox.height - src.rows) / 2, src.cols, src.rows)); gauss_pos.release(); gauss_neg.release(); threshold(gauss, gauss_pos, 0, 0, THRESH_TOZERO); threshold(gauss, gauss_neg, 0, 0, THRESH_TOZERO_INV); gauss_vector.emplace_back(gauss_pos); gauss_vector.emplace_back(gauss_neg);
// 重复上一步骤 rotated_gauss.release(); Scharr(rotated, rotated_gauss, CV_32F, 0, 1);
// 顺时针旋转45度,获取原先图像大小 center = Point((int) (rotated.cols / 2.0), (int) (rotated.rows / 2.0)); rot = cv::getRotationMatrix2D(center, -45.0, 1.0); warpAffine(rotated_gauss, rotated_gauss_tmp, rot, bbox.size()); gauss = rotated_gauss_tmp(Rect((bbox.width - src.cols) / 2, (bbox.height - src.rows) / 2, src.cols, src.rows)); gauss_pos.release(); gauss_neg.release(); threshold(gauss, gauss_pos, 0, 0, THRESH_TOZERO); threshold(gauss, gauss_neg, 0, 0, THRESH_TOZERO_INV); gauss_vector.emplace_back(gauss_pos); gauss_vector.emplace_back(gauss_neg);
// Normalisze gaussiaans in 0-255 range (for faster computation of histograms) // 缩放图像到0-255,方便直方图计算 for (int i = 0; i < 8; i++) { double hmin, hmax; minMaxLoc(gauss_vector[i], &hmin, &hmax);
Mat tmp; gauss_vector[i].convertTo(tmp, CV_8U, 255 / (hmax - hmin), -255 * hmin / (hmax - hmin)); gauss_vector[i] = tmp; } }
|