Skip to content

Instantly share code, notes, and snippets.

@vatbub
Last active July 2, 2021 13:55
Show Gist options
  • Save vatbub/181f46ec50137ce1abcd1785c9c71421 to your computer and use it in GitHub Desktop.
Save vatbub/181f46ec50137ce1abcd1785c9c71421 to your computer and use it in GitHub Desktop.
A Matlab/Octave program that generates all possible gray images for a given image size.
clc
clear all
close all
format long
imageProperties.height = 2;
imageProperties.width = 2;
output_folder = 'image_output';
% only used for estimations, not for the actual image calculation
imageProperties.bits = 4;
size_of_single_image_in_bytes = 90;
maximumPixelValue = (2 ^ imageProperties.bits) - 1;
estimations.imageCount = (maximumPixelValue + 1) ^ (imageProperties.width * imageProperties.height);
estimations.totalSize.bytes = estimations.imageCount * size_of_single_image_in_bytes;
estimations.totalSize.kb = estimations.totalSize.bytes / 1024;
estimations.totalSize.mb = estimations.totalSize.kb / 1024;
estimations.totalSize.gb = estimations.totalSize.mb / 1024;
estimations.totalSize.tb = estimations.totalSize.gb / 1024;
disp('All gray images:')
formatString = ['%0' num2str(imageProperties.width * imageProperties.height) 'x'];
% use parfor if you have the parallel computing toolbox installed
for image_id = 0:(estimations.imageCount - 1)
% parfor image_id = 0:(estimations.imageCount - 1)
image = zeros(imageProperties.height, imageProperties.width);
imageContents = sprintf(formatString, image_id);
imageIsInteresting = true;
firstValue = imageContents(1, 1);
imageContents = reshape(imageContents, imageProperties.height, imageProperties.width);
for col = 1:imageProperties.width
for row = 1:imageProperties.height
image(row, col) = 255 * hex2dec(imageContents(row, col)) / 15;
if ~imageIsInteresting && imageContents(row, col) ~= firstValue
imageIsInteresting = false;
end
end
end
if imageIsInteresting
disp(num2str(image_id))
end
imwrite(uint8(image), [output_folder '/' num2str(image_id) '.png'])
end
@vatbub
Copy link
Author

vatbub commented Jul 1, 2021

Beware: The amount of generated data is HUGE!

@vatbub
Copy link
Author

vatbub commented Jul 1, 2021

To keep the computation simple and fast, the values a pixel can take only range from 0 to 15 (scaled to 255 for visualization). This makes the computation easy as it allows each image to be represented by a 4-digit hexadecimal number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment