以下是将给定的Python代码转换为C++函数的示例:
#include <opencv2/opencv.hpp>
#include <vector>
cv::Mat fillHole(const cv::Mat& srcBw) {
cv::Mat temp = srcBw.clone();
// Floodfill from point (0, 0)
cv::floodFill(temp, cv::Point(0, 0), cv::Scalar(255));
// Invert floodfilled image
temp = ~temp;
cv::imshow("temp", temp);
// Invert floodfilled image
cv::Mat out = srcBw | temp;
return out;
}
std::vector<cv::Rect> getRoi(const cv::Mat& im_out, const cv::Mat& img) {
std::vector<cv::Rect> roi_list;
cv::Mat labels, stats, centroids;
int num_labels = cv::connectedComponentsWithStats(im_out, labels, stats, centroids);
for (int t = 1; t < num_labels; ++t) {
int x = stats.at<int>(t, cv::CC_STAT_LEFT);
int y = stats.at<int>(t, cv::CC_STAT_TOP);
int w = stats.at<int>(t, cv::CC_STAT_WIDTH);
int h = stats.at<int>(t, cv::CC_STAT_HEIGHT);
int area = stats.at<int>(t, cv::CC_STAT_AREA);
if (area < 500)
continue;
float cx = centroids.at<double>(t, 0);
float cy = centroids.at<double>(t, 1);
// 标出中心位置
circle(img, cv:Point(static_cast<int>(cx), static_cast<int>(cy)), 2, CV_RGB(0, 255, 0), 2);
// 画出外接矩形
rectangle(img, cv::Point(x, y), cv::Point(x + w, y + h), CV_RGB(0, 255, 0), 2);
// 保存roi的坐标和长宽
roi_list.push_back(cv::Rect(x, y, w, h));
}
return roi_list;
}
cv::Mat colorImgShow(const cv::Mat& im_out, int num_labels, const cv::Mat& labels) {
std::vector<cv::Vec3b> colors(num_labels);
for (int i = 0; i < num_labels; ++i) {
colors[i] = cv::Vec3b(rand() % 256, rand() % 256, rand() % 256);
}
colors[0] = cv::Vec3b(0, 0, 0);
cv::Mat image(im_out.size(), CV_8UC3);
for (int row = 0; row < im_out.rows; ++row) {
for (int col = 0; col < im_out.cols; ++col) {
int label = labels.at<int>(row, col);
image.at<cv::Vec3b>(row, col) = colors[label];
}
}
return image;
}
void saveRoi(const cv::Mat& src,const std::vector<cv::Rect>& roi_list) {
for (size_t i = 0; i < roi_list.size(); ++i) {
const cv::Rect& rect = roi_list[i];
cv::Mat roi = src(rect);
std:string filename = "money_roi/roi_" + std::to_string(i) + ".jpg";
cv::imwrite(filename, roi);
std::cout << "No." << std::setfill('0') << std::setw(2) << i << " Finished!" << std::endl;
}
}
void processMoneyImage(const std::string& filename) {
// Load image
cv::Mat img = cv::imread(filename);
// Preprocessing
cv::Mat im_in;
cvtColor(img, im_in, CV_BGR2GRAY);
cv::threshold(im_in, im_in, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
// Invert the foreground and background
im_in = ~im_in;
// Call fillHole function
cv::Mat im_out = fillHole(im_in);
// Get ROIs using connected components analysis
std::vector<cv::Rect> roi_list = getRoi(im_out, img);
// Visualize connected components
cv:Mat color_image = colorImgShow(im_out, roi_list.size(), labels);
// Save ROIs
saveRoi(img, roi_list);
// Display images.
cv:imshow("Thresholded Image", im_in);
cv:imshow("Colorful Image", color_image);
cv:imshow("Foreground", img);
// Write output images to files if needed
...
cv:waitKey(0);
}
int main() {
std:string filename = "money.png";
processMoneyImage(filename);
return 0;
}
请注意,上述代码可能需要根据您的项目设置进行进一步调整和优化。还需要包含必要的OpenCV库头文件,并确保在编译时链接相应的OpenCV库。
内容由零声教学AI助手提供,问题来源于学员提问




