I am attempting to extract the absolute differences image between two input images and then enhance the intensity (brightness) channel in HSV color space with the given times. The workflow is like below.
The processing of a batch of files is considered. Thus, the input is path strings input_path1
and input_path2
. The third parameter is the times for multiplying on intensity channel. As the result, with the input paths ./1/
and ./2/
, the image pair ./1/1.png
and ./2/1.png
will be taken as the inputs and the next image pair is ./1/2.png
and ./2/2.png
.
The experimental implementation
template<typename T>
void difference_and_enhancement(std::string input_path1, std::string input_path2, T enhancement_times,
std::string output_path, bool output_leading_zero, std::string filename_extension = ".png")
{
// Clear output
std::filesystem::remove_all(output_path);
std::filesystem::create_directory(output_path);
for (std::size_t i = 1; i <= 1000; i++)
{
std::string filename1 = input_path1 + std::to_string(i) + filename_extension;
if (std::filesystem::is_regular_file(filename1) == false)
{
std::cout << filename1 << " is not a regular file." << "\n";
return;
}
std::string filename2 = input_path2 + std::to_string(i) + filename_extension;
if (std::filesystem::is_regular_file(filename2) == false)
{
std::cout << filename2 << " is not a regular file." << "\n";
return;
}
std::cout << "Read " << filename1 << "\n";
auto image1 = cv::imread(filename1, cv::ImreadModes::IMREAD_COLOR);
std::cout << "Read " << filename2 << "\n";
auto image2 = cv::imread(filename2, cv::ImreadModes::IMREAD_COLOR);
char buff[100];
if (output_leading_zero)
{
snprintf(buff, sizeof(buff), "%s%03d%s", output_path.c_str(), i, filename_extension.c_str());
}
else
{
snprintf(buff, sizeof(buff), "%s%d%s", output_path.c_str(), i, filename_extension.c_str());
}
std::cout << "Output filename = " << buff << "\n";
// Difference
cv::Mat diffImage;
cv::absdiff(image1, image2, diffImage);
cv::Mat img_hsv;
cvtColor(diffImage, img_hsv, CV_RGB2HSV);
std::vector<cv::Mat> hsv_channels;
cv::split(img_hsv, hsv_channels);
cv::Mat h_image = hsv_channels[0];
cv::Mat s_image = hsv_channels[1];
cv::Mat v_image = hsv_channels[2];
cv::Mat output_hsv;
std::vector<cv::Mat> hsv_channels_output;
hsv_channels_output.push_back(h_image);
hsv_channels_output.push_back(s_image);
v_image.forEach<double>([&](double element, const int* position) { return element * static_cast<decltype(element)>(enhancement_times); });
hsv_channels_output.push_back(v_image);
cv::merge(hsv_channels_output, output_hsv);
cv::Mat output;
cvtColor(output_hsv, output, CV_HSV2RGB);
std::cout << "size = " << output.size() << "\n";
cv::imwrite(std::string(buff), output);
}
}
The usage example
int main()
{
difference_and_enhancement(
"./1/",
"./2/",
100,
"./OutputImages/",
false);
return 0;
}
All suggestions are welcome.