def find_center(img): h, w = img.shape roi_h = int(h * 2 / 3) roi_img = img[roi_h:, :] img_blur = cv2.GaussianBlur(roi_img, (15, 15), 0) # 高斯模糊 ret, th2 = cv2.threshold(img_blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) g2 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) open_img = cv2.morphologyEx(th2, cv2.MORPH_OPEN, g2, iterations=3) x_sum = np.sum(open_img, axis=0) x_point = np.where(x_sum > 0) point_x = int((x_point[0][0] + x_point[0][-1]) / 2) # print(roi_h, w) # np.savetxt('reshape_data.txt', x_point, delimiter=' ', fmt='%i') return point_x 转c++
时间: 2024-01-09 21:04:48 浏览: 88
#include <opencv2/opencv.hpp>
using namespace cv;
int find_center(Mat img) {
int h = img.rows;
int w = img.cols;
int roi_h = h * 2 / 3;
Mat roi_img = img(Rect(0, roi_h, w, h - roi_h));
Mat img_blur;
GaussianBlur(roi_img, img_blur, Size(15, 15), 0);
Mat th2;
threshold(img_blur, th2, 0, 255, THRESH_BINARY+THRESH_OTSU);
Mat g2 = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat open_img;
morphologyEx(th2, open_img, MORPH_OPEN, g2, Point(-1, -1), 3);
Mat x_sum;
reduce(open_img, x_sum, 0, REDUCE_SUM);
std::vector<int> x_point;
for (int i = 0; i < x_sum.cols; i++) {
if (x_sum.at<uchar>(0, i) > 0)
x_point.push_back(i);
}
int point_x = (x_point.front() + x_point.back()) / 2;
return point_x;
}
阅读全文