我一直在使用39805-image-to-coe-converter将256x256像素的24位总彩色jpg(每种颜色8位)转换为8位(总)灰度COE文件。我在源图像格式上很灵活,但在目标格式上就不灵活了。我尝试将颜色jpg转换为4位颜色(每个颜色通道4位) COE文件,其中每行都是我的12位像素颜色(RGB)。原因是这些是用12位全彩色管道在FPGA中创建块ram ...红色为4位,蓝色为4位,绿色为4位(共12位)。
例如,输出文件(COE基本上只是一个文本文件),一个纯白色字段将输出(这里是十六进制):
000 (下一行) 000 (下一行) ...
或
fff (下一行) ...
这取决于倒置。
如果有一种方法可以验证输出的COE确实是rad的映像,那么也应该是rad。我还假设使用img = img';可以在必要时反转颜色。
% Image to text conversion
% Read the image from the file
[filename, pathname] = uigetfile('*.bmp;*.tif;*.jpg;*.pgm','Pick an M-file');
img = imread(filename);
img = imresize((img),[256 256]);
[ row col p ] =size(img);
% Next line for testing
%image(img);
% Turns 8 bit into 4 bit (collapses colors - no good)
%img = img ./ 16;
% Converts to 2 channel grascale
%if p == 3
% img = rgb2gray(img);
%end
% What does this do? Create a 65x65 array? Why?
rectImg = img(16:80,16:80);
% noise add. Why would he want to add noise?
%rectImg = imnoise(rectImg,'salt & pepper', 0.02);
img(16:80,16:80) = rectImg;
image(img);
% Image Transpose
%imgTrans = img';
% iD conversion
img1D = img(:);
% Decimal to Hex value conversion
imgHex = dec2hex(img1D);
% New txt file creation
fid = fopen([filename '.coe'], 'wt');
% Hex value write to the txt file
fprintf(fid,'memory_initialization_radix=16;\n');
fprintf(fid,'memory_initialization_vector=\n');
fprintf(fid, '%x\n', img1D);
% Close the txt file
fclose(fid);发布于 2020-11-19 05:14:41
通过归一化为4位(0-15)解决
% normalize to 0..15 per channel
img_12 = uint8(double(img) / 255.0 * 15.0);https://stackoverflow.com/questions/64899910
复制相似问题