% Read the image
img = imread('image.jpg'); % Replace with your image file name
% Convert the image to grayscale if it is in color (optional)
img_gray = rgb2gray(img); % Use this only if the image is in RGB
% Resize the image to 450x514 (adjust if needed)
img_resized = imresize(img_gray, [450, 514]);
% Get the size of the image
[rows, cols] = size(img_resized);
% Open the MIF file to write
mif_file = fopen('image_data.mif', 'w');
% Write the header for the MIF file
fprintf(mif_file, 'DEPTH = %d;\n', rows * cols);
fprintf(mif_file, 'WIDTH = 8;\n'); % 8 bits per pixel for grayscale
fprintf(mif_file, 'ADDRESS_RADIX = DEC;\n');
fprintf(mif_file, 'DATA_RADIX = HEX;\n');
fprintf(mif_file, 'CONTENT BEGIN\n');
% Write the pixel data into the MIF file
for i = 1:rows
for j = 1:cols
% Get the pixel value at (i, j)
pixel_value = img_resized(i, j);
% Convert the pixel value to hexadecimal (8 bits)
hex_value = sprintf('%02X', pixel_value);
% Write the address and value to the MIF file
address = (i - 1) * cols + j - 1; % Linear address calculation
fprintf(mif_file, '
%d : %s;\n', address, hex_value);
end
end
% End the MIF file
fprintf(mif_file, 'END;\n');
% Close the MIF file
fclose(mif_file);
disp('MIF file has been generated.');