I have a small bit of code and I wonder if I can make it more readable. I would appreciate it if some people could take a look at it and give me some feedback. I especially wonder if there is a beter way of doing this:
cv::Mat image = loadImage(path.string());
if (image.data == NULL) { continue; }
This is the bigger code:
cv::Mat Watermarker::loadImage(const std::string& path)
{
if (!validFile(path))
{
if (!std::filesystem::is_directory(path))
{
std::cout << path << " is invalid\n";
}
return cv::Mat{};
}
cv::Mat image = cv::imread(path, cv::IMREAD_UNCHANGED);
if (image.data == NULL)
{
std::cout << "Not enough permissions to read file " << path << "\n";
return cv::Mat{};
}
return image;
}
void Watermarker::mark(const std::string& targetDirectory, const std::string& watermarkPath)
{
cv::Mat watermarkImage = loadImage(watermarkPath);
if (watermarkImage.data == NULL) { return; }
// Loop through all the files in the target directory
for (const std::filesystem::path& path : std::filesystem::directory_iterator(targetDirectory))
{
cv::Mat image = loadImage(path.string());
if (image.data == NULL) { continue; }
cv::resize(watermarkImage, watermarkImage, image.size());
// Give the images 4 channels
cv::cvtColor(image, image, cv::COLOR_RGB2RGBA);
cv::cvtColor(watermarkImage, watermarkImage, cv::COLOR_RGB2RGBA);
// Add the watermark to the original image
cv::addWeighted(watermarkImage, 0.3, image, 1, 0.0, image);
saveImage(targetDirectory + "/watermarked_images/" + path.filename().string(), image);
}
}
::loadImage()
and::mark()
, documenting comments should work miracles. Please heed What (not) to do when someone answers.) \$\endgroup\$