int n = matrix.size(); // C++ 这里的 = 拷贝是值拷贝,会得到一个新的数组 auto matrix_new = matrix;
时间: 2024-05-26 14:11:22 浏览: 5
好的,您的第一个问题是关于代码中的int n = matrix.size();和auto matrix_new = matrix;。这里的=是拷贝,会得到一个新的数组。具体来说,n变量是使用matrix.size()函数得到的数组大小,而matrix_new数组则是通过将原始的matrix数组进行值拷贝得到的。值拷贝将原始数组的值复制到新数组中,因此新数组的内容和原始数组的内容是相同的,但它们是两个不同的数组。这通常用于避免对原始数组的修改而影响到其他部分的代码。
相关问题
C++实验内容: 假设square_matrix是n阶整型方阵,请实现下列运算: (1)cin>> square_matrix (2)cout<< square_matrix (3)IntMatrix(n)生成一个n阶单位矩阵 (4)square_matrix+IntMatrix(n) (5)square_matrix_A=square_matrix_B
下面是 C++ 实现:
```c++
#include <iostream>
using namespace std;
class IntMatrix {
private:
int **matrix; // 二维动态数组
int n; // 方阵阶数
public:
// 构造函数,创建一个 n 阶方阵,默认为零矩阵
IntMatrix(int n) {
this->n = n;
matrix = new int*[n];
for (int i = 0; i < n; i++) {
matrix[i] = new int[n];
for (int j = 0; j < n; j++) {
matrix[i][j] = 0;
}
}
}
// 拷贝构造函数
IntMatrix(const IntMatrix& other) {
n = other.n;
matrix = new int*[n];
for (int i = 0; i < n; i++) {
matrix[i] = new int[n];
for (int j = 0; j < n; j++) {
matrix[i][j] = other.matrix[i][j];
}
}
}
// 重载输入运算符
friend istream& operator>>(istream& in, IntMatrix& m) {
for (int i = 0; i < m.n; i++) {
for (int j = 0; j < m.n; j++) {
in >> m.matrix[i][j];
}
}
return in;
}
// 重载输出运算符
friend ostream& operator<<(ostream& out, const IntMatrix& m) {
for (int i = 0; i < m.n; i++) {
for (int j = 0; j < m.n; j++) {
out << m.matrix[i][j] << " ";
}
out << endl;
}
return out;
}
// 重载加法运算符
IntMatrix operator+(const IntMatrix& other) const {
IntMatrix m(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
m.matrix[i][j] = matrix[i][j] + other.matrix[i][j];
}
}
return m;
}
// 重载赋值运算符
IntMatrix& operator=(const IntMatrix& other) {
if (this != &other) {
// 先删除原有的矩阵
for (int i = 0; i < n; i++) {
delete[] matrix[i];
}
delete[] matrix;
// 重新分配内存
n = other.n;
matrix = new int*[n];
for (int i = 0; i < n; i++) {
matrix[i] = new int[n];
for (int j = 0; j < n; j++) {
matrix[i][j] = other.matrix[i][j];
}
}
}
return *this;
}
// 析构函数,释放动态内存
~IntMatrix() {
for (int i = 0; i < n; i++) {
delete[] matrix[i];
}
delete[] matrix;
}
};
int main() {
int n;
cin >> n;
// 输入方阵
IntMatrix square_matrix(n);
cin >> square_matrix;
// 输出方阵
cout << square_matrix;
// 生成单位矩阵
IntMatrix unit_matrix(n);
for (int i = 0; i < n; i++) {
unit_matrix.matrix[i][i] = 1;
}
// 方阵加单位矩阵
IntMatrix result = square_matrix + unit_matrix;
cout << result;
// 赋值
IntMatrix square_matrix_A = square_matrix;
cout << square_matrix_A;
return 0;
}
```
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++ Eigen opencv
这段代码是一个Python函数,用于从一张图像中找到中心点的x坐标。其中使用了OpenCV库中的一些函数进行图像处理。
如果需要将这段代码转化为C++代码,可以使用Eigen和OpenCV库来实现:
```c++
#include <opencv2/opencv.hpp>
#include <Eigen/Core>
int find_center(cv::Mat img) {
int h = img.rows;
int w = img.cols;
int roi_h = h * 2 / 3;
cv::Mat roi_img = img(cv::Rect(0, roi_h, w, h - roi_h));
cv::Mat img_blur;
cv::GaussianBlur(roi_img, img_blur, cv::Size(15, 15), 0);
cv::Mat th2;
cv::threshold(img_blur, th2, 0, 255, cv::THRESH_BINARY + cv::THRESH_OTSU);
cv::Mat g2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::Mat open_img;
cv::morphologyEx(th2, open_img, cv::MORPH_OPEN, g2, cv::Point(-1, -1), 3);
Eigen::MatrixXi open_mat(h - roi_h, w);
for (int i = 0; i < h - roi_h; i++) {
for (int j = 0; j < w; j++) {
open_mat(i, j) = open_img.at<uchar>(i, j);
}
}
Eigen::VectorXi x_sum(w);
for (int i = 0; i < w; i++) {
x_sum(i) = open_mat.col(i).sum();
}
Eigen::ArrayXi x_point = (x_sum.array() > 0).select(Eigen::ArrayXi::LinSpaced(w, 0, w - 1), -1);
int point_x = (x_point(0) + x_point(w - 1)) / 2;
return point_x;
}
```
注意,此处使用了Eigen库来进行矩阵计算,需要在代码中引入相应的头文件和命名空间。同时,将OpenCV中的Mat转换为Eigen库中的Matrix类型,需要使用循环将每个像素的值拷贝到矩阵中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)