将以下python代码转化为c++版本。import math import cv2 import numpy as np import os thre1=10 thre2=-10 r=60 ang =0 def select_point(image,ang): #根据遥杆方向确定跟踪点坐标 sinA=math.sin(ang) cosA=math.cos(ang) dirBaseX=int(cosA1000) disBaseY=int(-sinA1000) dirValMax=-1000000000 for i in range(len(image)): for j in range(len(image[0])): if image[i][j]==255: dirVal=idisBaseY+jdirBaseX if dirVal>dirValMax: rstRow=i rstCol=j dirValMax=dirVal return [rstCol,rstRow] sequence_path = "./images/" save_path="./out/" 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) for i in range(r,len(image)-r): for j in range(r,len(image[0])-r): shizi_1=( int(image[i][j])-int(image[i-r][j])>thre1 and int(image[i][j])-int(image[i][j-r])>thre1 and (int(image[i][j])-int(image[i+r][j])>thre1) and int(image[i][j])-int(image[i][j+r])>thre1 ) xieshizi_1=( int(image[i][j])-int(image[i-r][j-r])<thre2 and int(image[i][j])-int(image[i+r][j-r])<thre2 and int(image[i][j])-int(image[i-r][j+r])<thre2 and int(image[i][j])-int(image[i+r][j+r])<thre2 ) if (shizi_1 or xieshizi_1): img[i][j]=255 else: img[i][j] =0 retval, labels, stats, centroids = cv2.connectedComponentsWithStats(img, connectivity=8) maxVal = 0 index = 0 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 img2=np.array(labels) target_x,target_y=select_point(img2,ang) print("跟踪点坐标:{}".format((target_x,target_y))) cv2.imwrite(os.path.join(save_path, file), cv2.circle(image,(int(target_x),int(target_y)),5,(255,255,0),2))
时间: 2024-01-11 08:03:25 浏览: 96
以下是将给定的Python代码转化为C++版本的示例:
```cpp
#include <iostream>
#include <cmath>
#include <opencv2/opencv.hpp>
int thre1 = 10;
int thre2 = -10;
int r = 60;
double ang = 0;
std::vector<int> select_point(cv::Mat image, double ang) {
double sinA = std::sin(ang);
double cosA = std::cos(ang);
int dirBaseX = static_cast<int>(cosA * 1000);
int disBaseY = static_cast<int>(-sinA * 1000);
int dirValMax = -1000000000;
int rstRow = 0, rstCol = 0;
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
if (image.at<uchar>(i, j) == 255) {
int dirVal = i * disBaseY + j * dirBaseX;
if (dirVal > dirValMax) {
rstRow = i;
rstCol = j;
dirValMax = dirVal;
}
}
}
}
return { rstCol, rstRow };
}
int main() {
std::string sequence_path = "./images/";
std::string save_path = "./out/";
for (const auto& file : std::filesystem::directory_iterator(sequence_path)) {
std::string filename = file.path().string();
cv::Mat image = cv::imread(filename, cv::IMREAD_GRAYSCALE);
cv::blur(image, image, cv::Size(3, 3));
cv::Mat img(image.rows, image.cols, CV_8UC1, cv::Scalar(0));
for (int i = r; i < image.rows - r; i++) {
for (int j = r; j < image.cols - r; j++) {
bool shizi_1 = (image.at<uchar>(i, j) - image.at<uchar>(i - r, j) > thre1 &&
image.at<uchar>(i, j) - image.at<uchar>(i, j - r) > thre1 &&
image.at<uchar>(i, j) - image.at<uchar>(i + r, j) > thre1 &&
image.at<uchar>(i, j) - image.at<uchar>(i, j + r) > thre1);
bool xieshizi_1 = (image.at<uchar>(i, j) - image.at<uchar>(i - r, j - r) < thre2 &&
image.at<uchar>(i, j) - image.at<uchar>(i + r, j - r) < thre2 &&
image.at<uchar>(i, j) - image.at<uchar>(i - r, j + r) < thre2 &&
image.at<uchar>(i, j) - image.at<uchar>(i + r, j + r) < thre2);
if (shizi_1 || xieshizi_1) {
img.at<uchar>(i, j) = 255;
}
else {
img.at<uchar>(i, j) = 0;
}
}
}
cv::Mat labels, stats, centroids;
int retval = cv::connectedComponentsWithStats(img, labels, stats, centroids, 8);
int maxVal = 0;
int index = 0;
for (int i = 1; i < stats.rows; i++) {
if (stats.at<int>(i, cv::CC_STAT_AREA) > maxVal) {
maxVal = stats.at<int>(i, cv::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;
}
}
}
cv::Mat img2(labels.rows, labels.cols, CV_8UC1);
labels.convertTo(img2, CV_8UC1);
std::vector<int> target = select_point(img2, ang);
int target_x = target[0];
int target_y = target[1];
std::cout << "跟踪点坐标:" << target_x << "," << target_y << std::endl;
cv::circle(image, cv::Point(target_x, target_y), 5, cv::Scalar(255, 255, 0), 2);
cv::imwrite(save_path + file.path().filename().string(), image);
}
return 0;
}
```
请注意,C++代码中的OpenCV库版本可能需要根据您的环境进行调整,并且可能需要包含适当的头文件和链接适当的库。此外,文件系统操作需要使用C++17标准。
阅读全文