【发布时间】:2021-09-27 09:41:48
【问题描述】:
我正在使用 Octave 5.2 并且可以使用 meshgrid 和 reshape 从 RGB 值数组创建 640 x 480 x 3 的图像,但有更好的方法吗? (代码在下面)我尝试使用 cat 和 imresize with nearest 但数组是 640x480 而不是 640x480x3 并且它会创建黑色方块,因为数组不是 640x480x3 格式可以这样工作周围获取彩色条形图像?。
f(:,:,1)=[255;0;0;0;0];
f(:,:,2)=[0;255;0;0;255];
f(:,:,3)=[0;0;255;0;2];
num_of_colors=numel(f(:,:,1));
img_resize_height=640
img_resize_height_tmp=round(img_resize_height/num_of_colors); %create the height wanted
%1) create size of array wanted
[r_im_tmp_x r_im_tmp_y]=meshgrid((f(:,:,1)),1:img_resize_height_tmp)
[g_im_tmp_x g_im_tmp_y]=meshgrid((f(:,:,2)),1:img_resize_height_tmp);
[b_im_tmp_x b_im_tmp_y]=meshgrid((f(:,:,3)),1:img_resize_height_tmp);
%2) reshape grid to evenly space out colors (in one column)
r_resize_tmp=reshape(r_im_tmp_x,[1,numel(r_im_tmp_x)])';
g_resize_tmp=reshape(g_im_tmp_x,[1,numel(g_im_tmp_x)])';
b_resize_tmp=reshape(b_im_tmp_x,[1,numel(b_im_tmp_x)])';
%3 make array size wanted 480
img_resize_len=480;
r_resize_tmp2=repmat(r_resize_tmp,([1,img_resize_len]));
g_resize_tmp2=repmat(g_resize_tmp,([1,img_resize_len]));
b_resize_tmp2=repmat(b_resize_tmp,([1,img_resize_len]));
img_resize_rgb(:,:,1)=r_resize_tmp2;
img_resize_rgb(:,:,2)=g_resize_tmp2;
img_resize_rgb(:,:,3)=b_resize_tmp2;
figure(1);
imshow(img_resize_rgb);
它创建的图像正确似乎有一种更简单/更好的编码方式。
我尝试使用imresize 命令来做同样的事情来改进代码。 (见下面的代码)。
pkg load image
f(:,:,1)=[255;0;0;0;0];
f(:,:,2)=[0;255;0;0;255];
f(:,:,3)=[0;0;255;0;2];
height_wanted=640;
width_wanted=480;
repmat_rgb=cat(2,f,f); %add another column to array to get imresize to work
reshaped_output = imresize(repmat_rgb, [height_wanted, width_wanted],'nearest'); %reshape swatch to large output
imshow(reshaped_output);
创建的图像不正确并且是黑白的(很可能是由于阵列是 640x480 而不是 640x480x3(我该如何解决这个问题?)
【问题讨论】:
标签: arrays image-processing multidimensional-array octave rgb