Sure! Below is the translation of the provided Python code into C++. I've made use of OpenCV's C++ API and standard C++ libraries to replace the functionality provided by NumPy and other Python-specific libraries.
// Include necessary headers
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <filesystem> // C++17 feature; if not available, use alternative methods
#include <cmath>
#include <algorithm>
namespace fs = std::filesystem; // For filesystem operations
// Function definitions
cv::Mat gradientx(const cv::Mat& img) {
// Compute gradient in x-direction using larger Sobel kernel
cv::Mat grad_x;
cv::Sobel(img, grad_x, CV_32F, 1, 0, 31);
return grad_x;
}
cv::Mat gradienty(const cv::Mat& img) {
// Compute gradient in y-direction using larger Sobel kernel
cv::Mat grad_y;
cv::Sobel(img, grad_y, CV_32F, 0, 1, 31);
return grad_y;
}
bool checkMatch(const std::vector<int>& lineset) {
std::vector<int> linediff(lineset.size() - 1);
std::adjacent_difference(lineset.begin(), lineset.end(), linediff.begin());
linediff.erase(linediff.begin()); // Remove the first element which is not valid
int x = 0;
int cnt = 0;
for (const auto& line : linediff) {
if (std::abs(line - x) < 5) {
cnt++;
} else {
cnt = 0;
x = line;
}
}
return cnt == 5;
}
std::vector<int> pruneLines(std::vector<int> lineset, int image_dim, int margin = 20) {
// Remove lines near the margins
lineset.erase(
std::remove_if(lineset.begin(), lineset.end(), [margin, image_dim](int x) { return x <= margin || x >= image_dim - margin; }),
lineset.end());
if (lineset.empty()) {
return lineset;
}
std::vector<int> linediff(lineset.size() - 1);
std::adjacent_difference(lineset.begin(), lineset.end(), linediff.begin());
linediff.erase(linediff.begin()); // Remove the first element which is not valid
int x = 0;
int cnt = 0;
int start_pos = 0;
for (size_t i = 0; i < linediff.size(); ++i) {
int line = linediff[i];
if (std::abs(line - x) < 5) {
cnt++;
if (cnt == 5) {
size_t end_pos = i + 2;
return std::vector<int>(lineset.begin() + start_pos, lineset.begin() + end_pos);
}
} else {
cnt = 0;
x = line;
start_pos = i;
}
}
return lineset;
}
std::vector<double> skeletonize_1d(const std::vector<double>& arr) {
std::vector<double> _arr = arr;
for (size_t i = 0; i < _arr.size() - 1; ++i) {
if (_arr[i] <= _arr[i + 1]) {
_arr[i] = 0;
}
}
for (size_t i = _arr.size() - 1; i > 0; --i) {
if (_arr[i - 1] > _arr[i]) {
_arr[i] = 0;
}
}
return _arr;
}
std::tuple<std::vector<int>, std::vector<int>, bool> getChessLines(const std::vector<double>& hdx, const std::vector<double>& hdy,
double hdx_thresh, double hdy_thresh, const cv::Size& image_shape) {
// Generate Gaussian window
int window_size = 21;
double sigma = 8.0;
cv::Mat gaussMat = cv::getGaussianKernel(window_size, sigma, CV_64F);
std::vector<double> gausswin(gaussMat.begin<double>(), gaussMat.end<double>());
// Threshold signals
std::vector<double> hdx_thresh_binary(hdx.size());
std::vector<double> hdy_thresh_binary(hdy.size());
std::transform(hdx.begin(), hdx.end(), hdx_thresh_binary.begin(),
[hdx_thresh](double val) { return val > hdx_thresh ? 1.0 : 0.0; });
std::transform(hdy.begin(), hdy.end(), hdy_thresh_binary.begin(),
[hdy_thresh](double val) { return val > hdy_thresh ? 1.0 : 0.0; });
// Blur signals using convolution with Gaussian window
std::vector<double> blur_x(hdx_thresh_binary.size());
std::vector<double> blur_y(hdy_thresh_binary.size());
cv::filter2D(hdx_thresh_binary, blur_x, -1, gausswin);
cv::filter2D(hdy_thresh_binary, blur_y, -1, gausswin);
// Skeletonize signals
std::vector<double> skel_x = skeletonize_1d(blur_x);
std::vector<double> skel_y = skeletonize_1d(blur_y);
// Find line positions
std::vector<int> lines_x;
std::vector<int> lines_y;
for (size_t i = 0; i < skel_x.size(); ++i) {
if (skel_x[i] > 0) lines_x.push_back(static_cast<int>(i));
}
for (size_t i = 0; i < skel_y.size(); ++i) {
if (skel_y[i] > 0) lines_y.push_back(static_cast<int>(i));
}
// Prune lines
lines_x = pruneLines(lines_x, image_shape.width);
lines_y = pruneLines(lines_y, image_shape.height);
// Check if lines match expected pattern
bool is_match = (lines_x.size() == 7) && (lines_y.size() == 7) &&
checkMatch(lines_x) && checkMatch(lines_y);
return {lines_x, lines_y, is_match};
}
std::vector<cv::Mat> getChessTiles(const cv::Mat& img, const std::vector<int>& lines_x, const std::vector<int>& lines_y) {
int stepx = static_cast<int>(std::round(static_cast<double>(lines_x[1] - lines_x[0])));
int stepy = static_cast<int>(std::round(static_cast<double>(lines_y[1] - lines_y[0])));
// Pad the image if necessary
int padl_x = 0, padr_x = 0, padl_y = 0, padr_y = 0;
if (lines_x.front() - stepx < 0) {
padl_x = std::abs(lines_x.front() - stepx);
}
if (lines_x.back() + stepx > img.cols - 1) {
padr_x = lines_x.back() + stepx - img.cols + 1;
}
if (lines_y.front() - stepy < 0) {
padl_y = std::abs(lines_y.front() - stepy);
}
if (lines_y.back() + stepy > img.rows - 1) {
padr_y = lines_y.back() + stepy - img.rows + 1;
}
cv::Mat img_padded;
cv::copyMakeBorder(img, img_padded, padl_y, padr_y, padl_x, padr_x, cv::BORDER_REPLICATE);
std::vector<int> setsx{lines_x.front() - stepx + padl_x};
std::vector<int> setsy{lines_y.front() - stepy + padl_y};
for (const auto& x : lines_x) setsx.push_back(x + padl_x);
setsx.push_back(lines_x.back() + stepx + padl_x);
for (const auto& y : lines_y) setsy.push_back(y + padl_y);
setsy.push_back(lines_y.back() + stepy + padl_y);
std::vector<cv::Mat> squares;
for (int j = 0; j < 8; ++j) {
for (int i = 0; i < 8; ++i) {
int x1 = setsx[i];
int x2 = setsx[i + 1];
int y1 = setsy[j];
int y2 = setsy[j + 1];
// Adjust sizes to ensure squares are of equal size
if ((x2 - x1) != stepx) {
x2 = x1 + stepx;
}
if ((y2 - y1) != stepy) {
y2 = y1 + stepy;
}
cv::Mat square = img_padded(cv::Range(y1, y2), cv::Range(x1, x2));
squares.push_back(square);
}
}
return squares;
}
void process_image(const std::string& image_path, const std::string& output_dir) {
// Load the image
cv::Mat image = cv::imread(image_path);
if (image.empty()) {
std::cout << "Failed to load image: " << image_path << std::endl;
return;
}
// Convert to grayscale
cv::Mat gray;
cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
// Preprocessing
cv::Mat equ;
cv::equalizeHist(gray, equ);
cv::Mat norm_image;
equ.convertTo(norm_image, CV_32F, 1.0 / 255.0);
// Compute the gradients
cv::Mat grad_x = gradientx(norm_image);
cv::Mat grad_y = gradienty(norm_image);
// Clip the gradients
cv::Mat Dx_pos = cv::max(grad_x, 0);
cv::Mat Dx_neg = cv::max(-grad_x, 0);
cv::Mat Dy_pos = cv::max(grad_y, 0);
cv::Mat Dy_neg = cv::max(-grad_y, 0);
// Compute the Hough transform
cv::Mat sum_Dx_pos, sum_Dx_neg, sum_Dy_pos, sum_Dy_neg;
cv::reduce(Dx_pos, sum_Dx_pos, 0, cv::REDUCE_SUM, CV_64F);
cv::reduce(Dx_neg, sum_Dx_neg, 0, cv::REDUCE_SUM, CV_64F);
cv::reduce(Dy_pos, sum_Dy_pos, 1, cv::REDUCE_SUM, CV_64F);
cv::reduce(Dy_neg, sum_Dy_neg, 1, cv::REDUCE_SUM, CV_64F);
double norm_factor_x = static_cast<double>(norm_image.rows * norm_image.rows);
double norm_factor_y = static_cast<double>(norm_image.cols * norm_image.cols);
std::vector<double> hough_Dx;
std::vector<double> hough_Dy;
for (int i = 0; i < sum_Dx_pos.cols; ++i) {
double val = (sum_Dx_pos.at<double>(0, i) * sum_Dx_neg.at<double>(0, i)) / norm_factor_x;
hough_Dx.push_back(val);
}
for (int i = 0; i < sum_Dy_pos.rows; ++i) {
double val = (sum_Dy_pos.at<double>(i, 0) * sum_Dy_neg.at<double>(i, 0)) / norm_factor_y;
hough_Dy.push_back(val);
}
// Adaptive thresholding
int a = 1;
bool is_match = false;
std::vector<int> lines_x;
std::vector<int> lines_y;
while (a < 5) {
double threshold_x = *std::max_element(hough_Dx.begin(), hough_Dx.end()) * (static_cast<double>(a) / 5.0);
double threshold_y = *std::max_element(hough_Dy.begin(), hough_Dy.end()) * (static_cast<double>(a) / 5.0);
std::tie(lines_x, lines_y, is_match) = getChessLines(hough_Dx, hough_Dy, threshold_x, threshold_y, norm_image.size());
if (is_match) {
break;
} else {
a++;
}
}
if (is_match) {
std::cout << "7 horizontal and vertical lines found, slicing up squares" << std::endl;
std::vector<cv::Mat> squares = getChessTiles(gray, lines_x, lines_y);
std::cout << "Tiles generated: (" << squares[0].rows << "x" << squares[0].cols << ") * " << squares.size() << std::endl;
// Extract filename and FEN (assuming filename is FEN)
std::string img_filename = fs::path(image_path).filename().string();
std::string fen = fs::path(img_filename).stem().string();
std::string img_save_dir = fs::path(output_dir) / fen;
if (!fs::exists(img_save_dir)) {
fs::create_directories(img_save_dir);
std::cout << "Created dir " << img_save_dir << std::endl;
}
std::string letters = "ABCDEFGH";
for (size_t i = 0; i < squares.size(); ++i) {
std::string filename = fen + "_" + letters[i % 8] + std::to_string((i / 8) + 1) + ".png";
std::string save_path = fs::path(img_save_dir) / filename;
if (i % 8 == 0) {
std::cout << "#" << i << ": saving " << save_path << "..." << std::endl;
}
// Resize to 32x32 and save
cv::Mat resized;
cv::resize(squares[i], resized, cv::Size(32, 32), 0, 0, cv::INTER_AREA);
cv::imwrite(save_path, resized);
}
} else {
std::cout << "No squares to save for " << image_path << std::endl;
}
}
// Main code
int main() {
// Specify input and output directories
std::string input_folder = "train_images"; // Replace with your input folder
std::string output_folder = "tiles"; // Replace with your desired output folder
// Create output directory if it doesn't exist
fs::create_directories(output_folder);
// Process images in the input folder
for (const auto& entry : fs::directory_iterator(input_folder)) {
std::string filename = entry.path().filename().string();
std::string extension = entry.path().extension().string();
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
if (extension == ".png" || extension == ".jpg" || extension == ".jpeg") {
std::string image_path = entry.path().string();
process_image(image_path, output_folder);
}
}
return 0;
}
Explanation:
-
Including Headers: We include the necessary headers for OpenCV and standard C++ functionality. The <filesystem> library is used for directory and file operations (ensure your compiler supports C++17).
-
Namespace Alias: We create a namespace alias fs for std::filesystem to shorten code.
-
Function Translations:
- gradientx and gradienty: Translated using OpenCV's C++ API.
- checkMatch: Uses standard C++ algorithms to compute differences and compare.
- pruneLines: Manipulates vectors to remove margins and find consistent line intervals.
- skeletonize_1d: Simulates the skeletonization process in 1D arrays.
- getChessLines: Processes gradient histograms to detect the chessboard lines.
- getChessTiles: Extracts the 64 chessboard squares from the image using the detected lines.
- process_image: Orchestrates the image processing steps, including loading the image, preprocessing, gradient computation, Hough transformation, line detection, and saving the tiles.
-
Main Function:
- Uses
std::filesystem to iterate over files in the input directory.
- Processes each image that matches the specified extensions.
Note:
Usage:
- Compile the code with your preferred C++ compiler linked against OpenCV libraries.
- Update the
input_folder and output_folder paths as needed.
- Run the program to process the images in the specified input directory.
Example Compilation Command:
g++ -std=c++17 -o chess_tile_extractor chess_tile_extractor.cpp `pkg-config --cflags --libs opencv4`
Replace opencv4 with opencv if you're using an older version of OpenCV.