Making mosaic images with ffmpeg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

/*
* This does not literally comrpess the image
* But it shows how the image would look like after compressing and decomressing it, useful
* to see how different algorithms afect the image.
*/
#include <image_io.hpp>
#include <utilities.hpp>
#include <wvlt/wavelet.hpp>
#include <boost/filesystem.hpp>
extern "C" {
#include <libavformat/avformat.h>
}
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
av_register_all();
auto image = to_raw_rgb_image(crop_to_square(open_image("image.jpg")), 512, 512);
std::vector<double> vector(make_u(image.width() * image.height()));
// for every color
for(unsigned int i = 0; i < 3; ++i){
for(unsigned int n = 0; n < make_u(image.width() * image.height()); ++n){
vector[n] = 2.0 * image.data[3*n + i] / double(255) - 1.0;
}
wvlt::wavelet_2D(vector.data(), make_u(image.width()), make_u(image.height()));
auto copy = vector;
for(auto & x : copy) x = std::abs(x);
unsigned int n_coefficients[] = {20, 20, 20};
std::nth_element(copy.begin(), copy.begin() + n_coefficients[i], copy.end(), std::greater<double>());
for(auto & x : vector){
if(std::abs(x) < copy[n_coefficients[i]]) x = 0;
}
wvlt::unwavelet_2D(vector.data(), make_u(image.width()), make_u(image.height()));
for(unsigned int n = 0; n < make_u(image.width() * image.height()); ++n){
image.data[3*n + i] = to_uint8_t(0.5 * vector[n] + 0.5);
}
}
save_as_jpg(image.frame, "output.jpg");
}