C++
Clean Code for Computer Vision using OpenCV and C++
When writing clean code using the OpenCV library in C++, here are some additional principles to follow:
Avoid using magic numbers: Instead of using hardcoded values, use named constants or named variables for numbers that have a specific meaning.
Keep functions focused on OpenCV operations: Limit the number of lines of code in functions and make sure each function focuses on performing OpenCV operations.
Use clear and descriptive names for OpenCV functions: When calling OpenCV functions, use names that are clear and descriptive of what the function does.
Use OpenCV data structures appropriately: Familiarize yourself with the different OpenCV data structures, like cv::Mat, cv::Point, etc., and use the appropriate one for each task.
Error handling: Make sure to check the return value of OpenCV functions and handle errors appropriately.
Make use of OpenCV's high-level functions: Whenever possible, use OpenCV's high-level functions instead of lower-level functions to simplify code and reduce the amount of boilerplate code.
Keep track of the image size: Make sure to keep track of the size of images, especially when performing operations like resizing, as this can affect the results.
These examples demonstrate how following good coding practices and paying attention to the specific features of the OpenCV library can help you write clean, efficient, and effective code.
By following these principles, you can write clean and maintainable code that makes effective use of the OpenCV library.
Here are several examples of clean code in OpenCV C++:
Meaningful variable names:
cv::Mat original_image = cv::imread("image.jpg");
cv::Mat resized_image;
cv::resize(original_image, resized_image, cv::Size(), 0.5, 0.5, cv::INTER_AREA);
Use of high-level functions:
cv::Mat src = cv::imread("image.jpg");
cv::Mat dst;
cv::GaussianBlur(src, dst, cv::Size(3,3), 0);
Error handling:
cv::Mat src = cv::imread("image.jpg");
if(src.empty()) {
std::cout << "Error: Could not load image" << std::endl;
return -1;
}
Use of descriptive function names:
cv::Mat src
Appropriate use of OpenCV data structures:
cv::Mat src = cv::imread("image.jpg");
std::vector<cv::Point2f> corners;
cv::goodFeaturesToTrack(src, corners, 100, 0.01, 10);
Reusable functions:
cv::Mat src = cv::imread("image.jpg");
cv::Mat gray;
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
cv::Mat sharpen_image(const cv::Mat& image) {
cv::Mat result;
cv::GaussianBlur(image, result, cv::Size(0,0), 3);
cv::addWeighted(image, 1.5, result, -0.5, 0, result);
return result;
}
cv::Mat sharpened = sharpen_image(gray);
Clear and concise comments:
// Load the source image
cv::Mat src = cv::imread("image.jpg");
// Convert the image to grayscale
cv::Mat gray;
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
// Threshold the image to create a binary image
cv::Mat thresholded;
cv::threshold(gray, thresholded, 128, 255, cv::THRESH_BINARY);
doc
this code shows information about image
code
doc
this code shows information about image