MATLAB draw image, indexed image of 8 colors, 8 color boxes of color and index on first figure, on second figure draw 0-7 indexes of image as image

Viewed 89

OK, we have an image and (0,1,2,3,4,5,6,7) - as images, we need to show image that we have got, next right, the image that got redrawn by numbers (0,1,2,3,4,5,6,7), third on right, is pallete (0,1,2,3,4,5,6,7) and boxes with color.

a) Upload JPEG image. Choose amount rows M and amount columns N for drawing grid. Change image size for required size of grid.

b) Convert image .JPEG in indexed image with 8 colors and show it up as on picture.

c) Prepare image that shows color card.

d) Prepare image that shows grid and color numbers.

m = 80;
n = 60;
im = imresize(imread('1задание.jpg'), [m n] * 10, 'nearest');
small_im = imresize(im, [m n], 'nearest');
[X, map] = rgb2ind(small_im, 8);
big_small_im = im2uint8(ind2rgb(imresize(X, [m n] * 10, 'nearest'), map));
figure;
imshow([im ones(m * 10, 50, 3) * 255 big_small_im ones(m * 10, 50, 3) * 255 ... 
   generate_cool_map(map, m * 10)]);
digits = [];
for i = 0 : 7
   digit = imread([int2str(i) '.png']);
   digits = [digits digit];
end
pixel_s = 43;
final_im = im2uint8(ones(m * pixel_s, n * pixel_s, 3) * 255);
for i = 1 : m
   for j = 1 : n
       final_im((i - 1) * pixel_s + 1 : i * pixel_s, j * pixel_s, :) = zeros(pixel_s, 1, 3);
       final_im(j * pixel_s, (j - 1) * pixel_s + 1 : j * pixel_s, :) = zeros(pixel_s, 1, 3);
       final_im((i - 1) * pixel_s + 2 : (i - 1) * pixel_s + 2 + 39+4, (j - 1) * pixel_s + 2 : (j - 1) * pixel_s + 2 + 26, :) = digits(:, X(i, j) * 27 + 1 : (X(i, j) + 1) * 27, :);
   end
end
figure;
imshow(final_im);

function res = generate_cool_map(map, s)
   color_size = floor(s / 8);
   m_map = zeros(8, 1, 3);
   for i = 1 : 8
       m_map(i, 1, :) = map(i, :);
   end
   res = imresize(m_map, [s, color_size], 'nearest');
   res(:, 1:2, :) = zeros(s, 2, 3);
   res(:, color_size - 1 : color_size, :) = zeros(s, 2, 3);
   for i = 0 : 7
       res(i * color_size + 1 : i * color_size + 2, :, :) = zeros(2, color_size, 3);
       res(i * color_size + 1 + floor(color_size / 2) : i * color_size + 2 + ...
       floor(color_size / 2),1:7,:) = zeros(2,7,3);
       res(i * color_size + 1 + floor(color_size / 2) : i * color_size + 2 + ...
       floor(color_size / 2), color_size - 6 : color_size, :) = zeros(2,7,3);
   end
   res(s - 1 : s, :, :) = zeros(2, color_size, 3);
   res = [res ones(s, floor(color_size / 3), 3) * 255];
   digits = [];
   for i = 0 : 7
       digit = imread([int2str(i) '.png']);
       digits = [digits digit];
   end
   res = im2uint8(res);
   for i = 0 : 7
       res(i * color_size + floor(color_size / 3) : i * color_size + ...
       floor(color_size / 3) + 39+4, color_size + 6 : color_size + 6 + 26, :) ...
       = digits(:, i * 27 + 1 : (i + 1) * 27, :);
   end
end

Task how shall look first figure:

Task how shall look first figure

I attempted but it seems here is some mistake. :c

Task how shall look second figure:

Task how shall look second figure

1 Answers

Figure 1: Resizing and Reducing Unique Colour Count

With the current amount of implementation details here is what I came up with. Resizing the image can be done using the imresize() function and 'nearest' neighbour interpolation. To reduce the number of colours the imapprox() function is used to limit the number of unique colours to 8. Reconstruction of the new image can be done using New_Image_Data (the colour card keys) and New_Colour_Map (the colour card values).

Resizing and Reducing Colour Palette

m = 50;
n = 60;

Original_Image = imread('peppers.png');
subplot(1,2,1); imshow(Original_Image);
title("Original Image");

Number_Of_Rows = m;
Number_Of_Columns = n;

%Resizing image%
Resized_Image = imresize(Original_Image,[m n],'nearest');

%Reducing the amount of colours%
Maximum_Intensity = 255;
[Image_Data,Colour_Map] = rgb2ind(Resized_Image,Maximum_Intensity);
[New_Image_Data,New_Colour_Map] = imapprox(Image_Data,Colour_Map,8);
subplot(1,2,2); imshow(New_Image_Data,New_Colour_Map);
title("Resized Image with 8 Colours");
Colour_Bar = colorbar;
set(Colour_Bar,'YTick',(0:7));

Figure 2: Plotting the Grid of Values

Colour Card Values

%Plotting the grid of card colour values%
Figure_2 = figure(2);
clf;
Figure_2.Position = [0 0 700 700];
x = (0:Number_Of_Rows);
y = (0:Number_Of_Columns);
[X,Y] = meshgrid(x,y);
Z = ones(length(x),length(y)).';
mesh(Y,X,Z);
axis off
hold on
view(0,90);
Image_Data = flip(New_Image_Data);
title("Colour Card Values/Keys");
for Row_Index = 1: Number_Of_Rows
   for Column_Index = 1: Number_Of_Columns 
   
   text(Column_Index-0.5,Row_Index-0.5,num2str(Image_Data(Row_Index,Column_Index)));
    
   end
end

Ran using MATLAB R2019b

Related