data = np.where(binary_array == 1, 1, )
时间: 2023-10-08 12:07:02 浏览: 135
在这段代码中,第三个参数缺失了。正确的代码应该是:
data = np.where(binary_array == 1, 1, 0)
这行代码的作用是将二进制数组 binary_array 中的所有值为 1 的元素替换成 1,其余元素替换成 0,生成一个新的数组 data。其中 np.where() 函数的用法是,第一个参数是条件,第二个参数是条件为真时的取值,第三个参数是条件为假时的取值。
相关问题
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++ opencv实现
以下是使用C++和OpenCV实现该函数的代码:
```
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
int find_center(cv::Mat img) {
int h = img.rows;
int w = img.cols;
int roi_h = static_cast<int>(h * 2 / 3);
cv::Mat roi_img = img(cv::Rect(0, roi_h, w, h - roi_h));
cv::GaussianBlur(roi_img, roi_img, cv::Size(15, 15), 0);
cv::Mat th2;
cv::threshold(roi_img, th2, 0, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);
cv::Mat g2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::morphologyEx(th2, th2, cv::MORPH_OPEN, g2, cv::Point(-1, -1), 3);
cv::Mat x_sum = cv::Mat::zeros(1, w, CV_32FC1);
for (int i = 0; i < w; i++) {
for (int j = 0; j < h - roi_h; j++) {
if (th2.at<uchar>(j, i) > 0) {
x_sum.at<float>(0, i)++;
}
}
}
int min_idx, max_idx;
cv::minMaxIdx(x_sum, nullptr, nullptr, &min_idx, &max_idx);
int point_x = (min_idx + max_idx) / 2;
return point_x;
}
```
注意,由于C++和Python的数值类型不同,因此我们需要使用`cv::Mat`类来处理图像数据,而不是使用`np.array`。此外,还需要使用`cv::Rect`类来裁剪图像,并使用`cv::Point`类来指定卷积核中心的位置。
binaryImg = binaryImg.astype(np.uint8)
This line of code converts the data type of the numpy array binaryImg from whatever it was before (presumably a floating-point type) to an unsigned 8-bit integer (np.uint8). This is a common step in image processing, as many operations require the input to be in a specific data type. In this case, the binaryImg array is likely being used to represent a binary image, where each pixel is either black or white (0 or 1), so using an 8-bit integer is sufficient to represent this information.
阅读全文