Certainly! Below is the rewritten C++ code that eliminates the use of TensorFlow and replaces it with OpenCV and standard C++ operations. The code maintains the same functionality: it processes images to detect chessboard lines and extracts individual chess tiles.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <filesystem> // Requires C++17
#include <opencv2/opencv.hpp>
// Namespace aliases
namespace fs = std::filesystem;
// Function declarations
cv::Mat gradientx(const cv::Mat& x);
cv::Mat gradienty(const cv::Mat& x);
bool checkMatch(const std::vector<int>& lineset);
std::vector<int> pruneLines(std::vector<int>& lineset, int image_dim, int margin = 20);
std::vector<float> skeletonize_1d(const std::vector<float>& arr);
void getChessLines(const std::vector<float>& hdx, const std::vector<float>& hdy,
float hdx_thresh, float hdy_thresh, cv::Size image_shape,
std::vector<int>& lines_x, std::vector<int>& lines_y, bool& is_match);
std::vector<cv::Mat> getChessTiles(cv::Mat& a, std::vector<int>& lines_x, std::vector<int>& lines_y);
void process_image(const std::string& image_path, const std::string& output_dir);
int main(int argc, char** argv) {
// 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
if (!fs::exists(output_folder)) {
fs::create_directory(output_folder);
}
// Process images in the input folder
for (const auto& entry : fs::directory_iterator(input_folder)) {
if (entry.is_regular_file()) {
std::string filename = entry.path().filename().string();
std::string extension = entry.path().extension().string();
if (extension == ".png" || extension == ".jpg" || extension == ".jpeg") {
std::string image_path = entry.path().string();
process_image(image_path, output_folder);
}
}
}
return 0;
}
// Function implementations
// 1. Compute gradient in x-direction using convolution
cv::Mat gradientx(const cv::Mat& x) {
cv::Mat grad_x;
cv::Mat kernel_x = (cv::Mat_<float>(3, 3) <<
-1, 0, 1,
-1, 0, 1,
-1, 0, 1);
cv::filter2D(x, grad_x, CV_32F, kernel_x);
return grad_x;
}
// 2. Compute gradient in y-direction using convolution
cv::Mat gradienty(const cv::Mat& x) {
cv::Mat grad_y;
cv::Mat kernel_y = (cv::Mat_<float>(3, 3) <<
-1, -1, -1,
0, 0, 0,
1, 1, 1);
cv::filter2D(x, grad_y, CV_32F, kernel_y);
return grad_y;
}
// 3. Check if line set matches expected pattern
bool checkMatch(const std::vector<int>& lineset) {
std::vector<int> linediff(lineset.size() - 1);
for (size_t i = 0; i < lineset.size() - 1; ++i) {
linediff[i] = lineset[i + 1] - lineset[i];
}
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;
}
// 4. Prune lines to remove margins and irregularities
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);
for (size_t i = 0; i < lineset.size() - 1; ++i) {
linediff[i] = lineset[i + 1] - lineset[i];
}
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) {
int 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;
}
// 5. Skeletonize a 1D array
std::vector<float> skeletonize_1d(const std::vector<float>& arr) {
std::vector<float> _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;
}
// 6. Get chessboard lines from Hough transforms
void getChessLines(const std::vector<float>& hdx, const std::vector<float>& hdy,
float hdx_thresh, float hdy_thresh, cv::Size image_shape,
std::vector<int>& lines_x, std::vector<int>& lines_y, bool& is_match) {
// Generate Gaussian window
int window_size = 21;
double sigma = 4.0;
std::vector<double> gausswin(window_size);
int half_size = window_size / 2;
double sum = 0.0;
for (int i = 0; i < window_size; ++i) {
gausswin[i] = std::exp(-0.5 * std::pow((i - half_size) / sigma, 2));
sum += gausswin[i];
}
// Normalize
for (int i = 0; i < window_size; ++i) {
gausswin[i] /= sum;
}
// Threshold signals
std::vector<float> hdx_thresh_binary(hdx.size());
std::vector<float> hdy_thresh_binary(hdy.size());
for (size_t i = 0; i < hdx.size(); ++i) {
hdx_thresh_binary[i] = hdx[i] > hdx_thresh ? 1.0f : 0.0f;
}
for (size_t i = 0; i < hdy.size(); ++i) {
hdy_thresh_binary[i] = hdy[i] > hdy_thresh ? 1.0f : 0.0f;
}
// Blur signals using convolution with Gaussian window
std::vector<float> blur_x(hdx.size(), 0.0f);
std::vector<float> blur_y(hdy.size(), 0.0f);
for (size_t i = 0; i < hdx.size(); ++i) {
double sum_x = 0.0;
for (int j = 0; j < window_size; ++j) {
int idx = i + j - half_size;
if (idx >= 0 && idx < static_cast<int>(hdx.size())) {
sum_x += hdx_thresh_binary[idx] * gausswin[j];
}
}
blur_x[i] = static_cast<float>(sum_x);
}
for (size_t i = 0; i < hdy.size(); ++i) {
double sum_y = 0.0;
for (int j = 0; j < window_size; ++j) {
int idx = i + j - half_size;
if (idx >= 0 && idx < static_cast<int>(hdy.size())) {
sum_y += hdy_thresh_binary[idx] * gausswin[j];
}
}
blur_y[i] = static_cast<float>(sum_y);
}
// Skeletonize signals
std::vector<float> skel_x = skeletonize_1d(blur_x);
std::vector<float> skel_y = skeletonize_1d(blur_y);
// Find line positions
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
is_match = (lines_x.size() == 7) && (lines_y.size() == 7) &&
checkMatch(lines_x) && checkMatch(lines_y);
}
// 7. Extract individual chess tiles
std::vector<cv::Mat> getChessTiles(cv::Mat& a, std::vector<int>& lines_x, std::vector<int>& lines_y) {
int stepx = static_cast<int>(std::round(cv::mean(cv::Mat(lines_x))[0] / (lines_x.size() - 1)));
int stepy = static_cast<int>(std::round(cv::mean(cv::Mat(lines_y))[0] / (lines_y.size() - 1)));
// Pad the image if necessary
int padl_x = 0, padr_x = 0, padl_y = 0, padr_y = 0;
if (lines_x[0] - stepx < 0) padl_x = std::abs(lines_x[0] - stepx);
if (lines_x.back() + stepx > a.cols - 1) padr_x = lines_x.back() + stepx - a.cols + 1;
if (lines_y[0] - stepy < 0) padl_y = std::abs(lines_y[0] - stepy);
if (lines_y.back() + stepy > a.rows - 1) padr_y = lines_y.back() + stepy - a.rows + 1;
cv::Mat a2;
cv::copyMakeBorder(a, a2, padl_y, padr_y, padl_x, padr_x, cv::BORDER_REPLICATE);
std::vector<int> setsx, setsy;
setsx.push_back(lines_x[0] - stepx + padl_x);
for (const auto& x : lines_x) setsx.push_back(x + padl_x);
setsx.push_back(lines_x.back() + stepx + padl_x);
setsy.push_back(lines_y[0] - stepy + padl_y);
for (const auto& y : lines_y) setsy.push_back(y + padl_y);
setsy.push_back(lines_y.back() + stepy + padl_y);
int num_squares = 64;
std::vector<cv::Mat> squares(num_squares);
int idx = 0;
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::Rect rect(x1, y1, x2 - x1, y2 - y1);
squares[idx++] = a2(rect);
}
}
return squares;
}
// 8. Process a single image
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::cerr << "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.0);
cv::Mat Dx_neg = cv::max(-grad_x, 0.0);
cv::Mat Dy_pos = cv::max(grad_y, 0.0);
cv::Mat Dy_neg = cv::max(-grad_y, 0.0);
// Compute the Hough transform
cv::Mat hough_Dx(1, norm_image.cols, CV_32F, cv::Scalar(0));
cv::Mat hough_Dy(1, norm_image.rows, CV_32F, cv::Scalar(0));
for (int x = 0; x < norm_image.cols; ++x) {
float sum_pos = cv::sum(Dx_pos.col(x))[0];
float sum_neg = cv::sum(Dx_neg.col(x))[0];
hough_Dx.at<float>(0, x) = (sum_pos * sum_neg) / (norm_image.rows * norm_image.rows);
}
for (int y = 0; y < norm_image.rows; ++y) {
float sum_pos = cv::sum(Dy_pos.row(y))[0];
float sum_neg = cv::sum(Dy_neg.row(y))[0];
hough_Dy.at<float>(0, y) = (sum_pos * sum_neg) / (norm_image.cols * norm_image.cols);
}
// Convert Hough transforms to vectors
std::vector<float> hough_Dx_vec;
hough_Dx_vec.assign((float*)hough_Dx.datastart, (float*)hough_Dx.dataend);
std::vector<float> hough_Dy_vec;
hough_Dy_vec.assign((float*)hough_Dy.datastart, (float*)hough_Dy.dataend);
// Adaptive thresholding
int a = 1;
bool is_match = false;
std::vector<int> lines_x, lines_y;
while (a < 5) {
float threshold_x = *std::max_element(hough_Dx_vec.begin(), hough_Dx_vec.end()) * static_cast<float>(a) / 5.0f;
float threshold_y = *std::max_element(hough_Dy_vec.begin(), hough_Dy_vec.end()) * static_cast<float>(a) / 5.0f;
getChessLines(hough_Dx_vec, hough_Dy_vec, threshold_x, threshold_y, norm_image.size(), lines_x, lines_y, is_match);
if (is_match) {
break;
} else {
a += 1;
}
}
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
fs::path img_file = fs::path(image_path).filename();
std::string img_filename = img_file.string();
std::string fen = img_filename.substr(0, img_filename.find_last_of("."));
std::string img_save_dir = output_dir + "/" + fen;
if (!fs::exists(img_save_dir)) {
fs::create_directory(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::ostringstream oss;
oss << img_save_dir << "/" << fen << "_" << letters[i % 8] << (i / 8 + 1) << ".png";
if (i % 8 == 0) {
std::cout << "#" << i << ": saving " << oss.str() << "..." << 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(oss.str(), resized);
}
} else {
std::cout << "No squares to save for " << image_path << std::endl;
}
}
Explanation of Changes:
- Removed TensorFlow Headers and Dependencies: All includes and usages of TensorFlow have been removed.
- Replaced TensorFlow Tensors with OpenCV Matrices: Operations previously performed using TensorFlow tensors are now done using OpenCV
cv::Mat objects.
- Rewrote
gradientx and gradienty Functions: These functions now use OpenCV's filter2D function to perform convolution with the specified kernels directly.
- Modified
process_image Function:
- The gradients are calculated using the new
gradientx and gradienty functions.
- Clipping and thresholding operations are performed using OpenCV functions like
cv::max.
- The Hough transforms are computed using OpenCV functions and standard C++ loops.
- Preserved Other Functions: Functions like
checkMatch, pruneLines, skeletonize_1d, getChessLines, and getChessTiles were already implemented using standard C++ and have been kept as is, with minor adjustments where necessary.
- Updated Main Function: Removed TensorFlow session initialization and updated calls to
process_image function accordingly.
Notes:
- Ensure that you have OpenCV installed and properly configured in your development environment.
- The code uses C++17 features (e.g.,
std::filesystem), so make sure your compiler supports C++17.
- Replace
"train_images" with the path to your input images and "tiles" with your desired output directory.
- The code reads images from the input directory, processes them to detect chessboard lines, and extracts individual chess tiles, saving them to the output directory.
Compiling the Code:
To compile the code, use a command similar to the following (adjust paths based on your setup):
g++ -std=c++17 -o chess_tile_extractor main.cpp `pkg-config --cflags --libs opencv4`
Running the Program:
After compiling, run the program:
./chess_tile_extractor
Make sure that the input directory contains images of chessboards and that you have the necessary permissions for reading and writing files.