img2 = cv2.blur(img, (9, 9))将这句python转换成C++
时间: 2023-11-13 07:03:04 浏览: 114
以下是将Python中的`cv2.blur(img, (9, 9))`转换为C++的代码:
```
cv::Mat img2;
cv::blur(img, img2, cv::Size(9, 9));
```
需要注意的是,C++中的`cv::Mat`和Python中的`img`可能有不同的数据类型和通道数。因此,在使用前需要先确认两者的数据类型和通道数是否一致。
相关问题
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类型,需要使用循环将每个像素的值拷贝到矩阵中。
将以下的python程序转化为c++版本,并在vs2022上实现。import cv2 import numpy as np from skimage.transform import radon import os thre1=10 thre2=-10 r=60 maxVal = 0 index = 0 sequence_path = "./images/" for file in os.listdir(sequence_path): filename=os.path.join(sequence_path, file) image=cv2.imread(filename, 0) image=cv2.blur(image,(3,3)) img=np.zeros((len(image), len(image[0])),np.uint8) maxVal = 0 index = 0 retval, labels, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8) for i in range(1, len(stats)): if stats[i][4] > maxVal: maxVal = stats[i][4] index = i #x,y,h,w s for i in range(len(labels)): for j in range(len(labels[0])): if labels[i][j]==index: labels[i][j]=255 else: labels[i][j] = 0 cv2.imwrite('./4-max_region.jpg',labels) img2=cv2.imread('./4-max_region.jpg',0) img3=cv2.Canny(img2,15,200) # theta = np.linspace(0, 180, endpoint=False) img4 = radon(img3) max_angel=0 for i in range(len(img4)): for j in range(len(img4[0])): if img4[i][j]>max_angel: max_angel=img4[i][j] angel=j print("{}方向为:{} °".format(filename,angel))
在VS2022上实现这段Python代码的C++版本,你可以使用以下代码作为参考:
```cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
using namespace cv;
int main() {
int thre1 = 10;
int thre2 = -10;
int r = 60;
int maxVal = 0;
int index = 0;
string sequence_path = "./images/";
for (const auto &entry : filesystem::directory_iterator(sequence_path)) {
string filename = entry.path().string();
Mat image = imread(filename, IMREAD_GRAYSCALE);
blur(image, image, Size(3,3));
Mat img(image.rows, image.cols, CV_8U, Scalar(0));
maxVal = 0;
index = 0;
Mat labels, stats, centroids;
int retval = connectedComponentsWithStats(img, labels, stats, centroids, 8);
for (int i = 1; i < stats.rows; i++) {
if (stats.at<int>(i, CC_STAT_AREA) > maxVal) {
maxVal = stats.at<int>(i, CC_STAT_AREA);
index = i;
}
}
for (int i = 0; i < labels.rows; i++) {
for (int j = 0; j < labels.cols; j++) {
if (labels.at<int>(i, j) == index)
labels.at<int>(i, j) = 255;
else
labels.at<int>(i, j) = 0;
}
}
imwrite("./4-max_region.jpg", labels);
Mat img2 = imread("./4-max_region.jpg", IMREAD_GRAYSCALE);
Mat img3;
Canny(img2, img3, 15, 200);
Mat img4;
radon(img3, img4);
int max_angel = 0;
int angel = 0;
for (int i = 0; i < img4.rows; i++) {
for (int j = 0; j < img4.cols; j++) {
if (img4.at<float>(i, j) > max_angel) {
max_angel = img4.at<float>(i, j);
angel = j;
}
}
}
cout << filename << " 方向为:" << angel << " °" << endl;
}
return 0;
}
```
请注意,你需要在项目中添加OpenCV库的链接。此外,代码中使用了C++17的filesystem库进行文件遍历,因此你需要在编译时启用C++17标准。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)