// 5. Regularize to a perfect circle
Path regularize_circle(const Path& XY) {
double xc, yc, r;
if (!kasa_circle_fitting(XY, xc, yc, r)) {
return XY;
}
code
Path regularized;
int num_points = 100; // Higher number for smoother circle
for (int i = 0; i < num_points; ++i) {
double theta = 2 * CV_PI * i / num_points;
double x = xc + r * std::cos(theta);
double y = yc + r * std::sin(theta);
regularized.emplace_back(cv::Point2d(x, y));
}
regularized.emplace_back(regularized.front());
return regularized;
}
// 6. Regularize to a perfect ellipse
Path regularize_ellipse(const Path& XY) {
if (XY.size() < 5) {
std::cerr << "Not enough points to fit an ellipse." << std::endl;
return XY;
}
code
cv::Mat points_mat(XY.size(), 1, CV_32FC2);
for (size_t i = 0; i < XY.size(); ++i) {
points_mat.at<cv::Point2f>(i, 0) = cv::Point2f(static_cast<float>(XY[i].x), static_cast<float>(XY[i].y));
}
cv::RotatedRect ellipse;
try {
ellipse = cv::fitEllipse(points_mat);
}
catch (const cv::Exception& e) {
std::cerr << "Ellipse fitting failed: " << e.what() << std::endl;
return XY;
}
Path regularized;
int num_points = 200; // Higher number for smoother ellipse
for (int i = 0; i < num_points; ++i) {
double theta = 2 * CV_PI * i / num_points;
double x = ellipse.center.x + (ellipse.size.width / 2.0) * std::cos(theta);
double y = ellipse.center.y + (ellipse.size.height / 2.0) * std::sin(theta);
regularized.emplace_back(cv::Point2d(x, y));
}
regularized.emplace_back(regularized.front());
return regularized;
// 19. Main Function
int main(int argc, char* argv[]) {
// Handle command-line arguments for flexibility (optional)
std::string input_csv = "problems/isolated.csv";
std::string output_csv = "regularized_shapes.csv";
code
// Optional: Parse command-line arguments
if (argc >= 2) {
input_csv = argv[1];
}
if (argc >= 3) {
output_csv = argv[2];
}
// Read CSV and find global y_max
double y_max_global = std::numeric_limits<double>::lowest();
Paths path_XYs = read_csv(input_csv, y_max_global);
if (path_XYs.empty()) {
std::cerr << "No paths found in the CSV file." << std::endl;
return -1;
}
// Find global x_min, y_min, x_max, y_max
double x_min = std::numeric_limits<double>::max();
double y_min = std::numeric_limits<double>::max();
double x_max = std::numeric_limits<double>::lowest();
double y_max = std::numeric_limits<double>::lowest();
for (const auto& path : path_XYs) {
for (const auto& pt : path) {
if (pt.x < x_min) x_min = pt.x;
if (pt.y < y_min) y_min = pt.y;
if (pt.x > x_max) x_max = pt.x;
if (pt.y > y_max) y_max = pt.y;
}
}
// Detect and regularize shapes
Paths regularized_XYs;
std::vector<std::string> detected_shapes;
for (const auto& path : path_XYs) {
auto [shape, reg_XY] = detect_and_regularize_shape(path);
regularized_XYs.push_back(reg_XY);
detected_shapes.push_back(shape);
}
// Calculate scaling and padding
double scale_x = (1000.0 - 2.0 * 50) / (x_max - x_min); // padding = 50
double scale_y = (1000.0 - 2.0 * 50) / (y_max - y_min);
double scale = std::min(scale_x, scale_y);
int padding = 50;
double SCALE_FACTOR = 4.0; // Scaling factor for high-resolution images
// Plot shapes and calculate symmetry lines
plot_shapes(path_XYs, regularized_XYs, detected_shapes, x_min, y_min, x_max, y_max, scale, padding, SCALE_FACTOR);
// Write regularized shapes to CSV
polylines2csv(regularized_XYs, output_csv);
std::cout << "Regularized shapes have been written to " << output_csv << std::endl;
return 0;
}
(base) kartik@Kartiks-MacBook-Air-2 MINOR % g++ -std=c++17 -o shh reularisation.cpp pkg-config --cflags --libs opencv4
(base) kartik@Kartiks-MacBook-Air-2 MINOR % ./shh
dyld[44750]: Library not loaded: /opt/homebrew/opt/protobuf/lib/libprotobuf.28.1.0.dylib
Referenced from: <133067C5-2657-3E82-8140-E18D02092456> /opt/homebrew/Cellar/opencv/4.10.0_8/lib/libopencv_dnn.4.10.0.dylib
Reason: tried: '/opt/homebrew/opt/protobuf/lib/libprotobuf.28.1.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/protobuf/lib/libprotobuf.28.1.0.dylib' (no such file), '/opt/homebrew/opt/protobuf/lib/libprotobuf.28.1.0.dylib' (no such file), '/opt/homebrew/Cellar/protobuf/28.3/lib/libprotobuf.28.1.0.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/protobuf/28.3/lib/libprotobuf.28.1.0.dylib' (no such file), '/opt/homebrew/Cellar/protobuf/28.3/lib/libprotobuf.28.1.0.dylib' (no such file)Library not loaded: /opt/homebrew/opt/openexr/lib/libOpenEXR-3_2.31.dylib
Referenced from: /opt/homebrew/Cellar/opencv/4.10.0_8/lib/libopencv_imgcodecs.4.10.0.dylib
Reason: tried: '/opt/homebrew/opt/openexr/lib/libOpenEXR-3_2.31.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/openexr/lib/libOpenEXR-3_2.31.dylib' (no such file), '/opt/homebrew/opt/openexr/lib/libOpenEXR-3_2.31.dylib' (no such file), '/opt/homebrew/Cellar/openexr/3.3.1/lib/libOpenEXR-3_2.31.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/openexr/3.3.1/lib/libOpenEXR-3_2.31.dylib' (no such file), '/opt/homebrew/Cellar/openexr/3.3.1/lib/libOpenEXR-3_2.31.dylib' (no such file)
zsh: abort ./shh
The error messages you're encountering indicate that OpenCV is attempting to load specific dynamic libraries (libprotobuf and libOpenEXR) that are either missing or mismatched in terms of version on your system. This is a common issue when dependencies are updated or removed, leading to broken links in applications that rely on them.
Here's a step-by-step guide to resolve these issues:
1. Update Homebrew
First, ensure that Homebrew is up to date. This helps in fetching the latest package information and avoids potential conflicts.
bash
brew update
brew upgrade
2. Reinstall Missing Dependencies
The errors specifically mention libprotobuf and libOpenEXR. Let's reinstall them to ensure they're correctly set up.
a. Reinstall Protobuf
bash
brew reinstall protobuf
b. Reinstall OpenEXR
bash
brew reinstall openexr
3. Reinstall OpenCV
After ensuring that protobuf and openexr are correctly installed, reinstall OpenCV to ensure it links against the correct versions of these dependencies.
bash
brew reinstall opencv
Alternatively, if you encounter issues, you can uninstall and then install OpenCV again:
bash
brew uninstall opencv
brew install opencv
4. Verify Library Paths
Ensure that the required dynamic libraries exist in the expected directories.
a. Check Protobuf Library
bash
ls /opt/homebrew/opt/protobuf/lib/
Look for libprotobuf.28.1.0.dylib. If it's not present, it's possible that the version has been updated. You might see a different version number like libprotobuf.XX.X.X.dylib.
b. Check OpenEXR Library
bash
ls /opt/homebrew/opt/openexr/lib/
Similarly, verify the presence of libOpenEXR-3_2.31.dylib. If it's missing, check for available versions.
5. Create Symbolic Links (If Necessary and Safe)
⚠️ Caution: Creating symbolic links to different versions can lead to unexpected behavior if there are significant changes between versions. Proceed only if you're confident that the newer version is backward compatible.
a. For Protobuf
If the expected version is missing but a newer version exists, you can create a symbolic link:
bash
cd /opt/homebrew/opt/protobuf/lib/
ln -s libprotobuf.X.X.X.dylib libprotobuf.28.1.0.dylib
Replace X.X.X with the actual version number present in the directory.
b. For OpenEXR
Similarly, for OpenEXR:
bash
cd /opt/homebrew/opt/openexr/lib/
ln -s libOpenEXR-3_2.X.dylib libOpenEXR-3_2.31.dylib
Replace X with the actual minor version number.
Again, use this method with caution.
6. Update Dynamic Library Links
After ensuring that all necessary libraries are present, refresh the dynamic linker cache.
bash
sudo update_dyld_shared_cache
Note: On macOS, update_dyld_shared_cache is typically run automatically. Manual execution is rarely needed, and using sudo might not be required or could be restricted depending on your macOS version.
7. Verify Installation
Check if OpenCV can now find the required libraries.
bash
ldd ./shh
Note: On macOS, you can use otool instead of ldd:
bash
otool -L ./shh
This command will list all the dynamic libraries that your executable is linked against and show if any are missing.
8. Rebuild Your Application
After ensuring that all dependencies are correctly installed and linked, try rebuilding your application.
Ensure that the filename is correctly spelled (regularisation.cpp vs. reularisation.cpp).
9. Run the Application
Execute your program to verify that the issue is resolved.
bash
./shh
Additional Tips
Check for Multiple OpenCV Installations: Ensure that there's only one installation of OpenCV on your system to prevent conflicts.
bash
brew list | grep opencv
Environment Variables: While it's generally not recommended to modify DYLD_LIBRARY_PATH on macOS due to System Integrity Protection (SIP), ensure that your environment isn't pointing to outdated or incorrect library paths.
Use Virtual Environments: Consider using containerization tools like Docker to manage and isolate dependencies, especially for complex projects.
Consult Homebrew Doctors: If you continue to face issues, running brew doctor can provide valuable insights.
bash
brew doctor
Follow the recommendations provided by the command.
By following these steps, you should be able to resolve the dynamic library loading issues and successfully run your C++ application that utilizes OpenCV for shape regularization and detection.
If you continue to experience problems after trying these solutions, please provide additional details or error messages so that I can assist you further.