arrays - 矩阵数组到多维 RGB 图像数组并使用 imresize 重塑图像
问题描述
我正在使用Octave 5.2,可以使用 RGB 值数组创建 640 x 480 x 3 图像meshgrid
,reshape
但是有更好的方法吗?(代码在下面)我尝试使用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(我该如何解决这个问题?)
解决方案
它看起来像是imresize
Octave 图像包实现中的一个错误(在 MATLAB 中代码正在运行)。
当 的输入imresize
是 RGB(3D 矩阵)时,输出也应该是 RGB(3D 矩阵)。
在您的示例中,输出是灰度(2D 矩阵而不是 3D 矩阵)。
这是实施中的错误imresize
!
该功能是“开源”的,我们可以使用调试器对其进行调试。
逐步执行代码(stepping into imresize
)让我们得到以下代码:
elseif (strcmpi (method, "nearest") && all ([int_row_scale int_col_scale]))
## we are matlab incompatible here on purpose. We can the stuff here in 2
## ways. With interp2 or by clever indexing. Indexing is much much faster
## than interp2 but they return different results (the way we are doing it
## at least). Matlab does the same as we are doing if the both columns and
## rows go the same direction but if they increase one and decrease the
## other, then they return the same as if we were using interp2. We are
## smarter and use indexing even in that case but then the results differ
if (int_row_scale == 1)
row_idx = (1:rows (im))(ones (1, scale_rows), :);
elseif (int_row_scale == -1)
row_idx = ceil (linspace (floor (1/(scale_rows * 2)) + 1, inRows, outRows));
endif
if (int_col_scale == 1)
col_idx = (1:columns (im))(ones (scale_cols, 1), :);
elseif (int_col_scale == -1)
col_idx = ceil (linspace (floor (1/(scale_cols * 2)) + 1, inCols, outCols));
endif
im = im(row_idx, col_idx);
该错误位于上述部分的最后一行。
而不是im = im(row_idx, col_idx);
它应该是:
im = im(row_idx, col_idx, :);
如果您不想编辑“图像包”代码,您可以使用以下解决方法:
调整每个颜色通道的大小并连接调整大小的通道。
替换reshaped_output = imresize(repmat_rgb, [height_wanted, width_wanted], 'nearest');
为:
out_r = imresize(repmat_rgb(:, :, 1), [height_wanted, width_wanted], 'nearest');
out_g = imresize(repmat_rgb(:, :, 2), [height_wanted, width_wanted], 'nearest');
out_b = imresize(repmat_rgb(:, :, 3), [height_wanted, width_wanted], 'nearest');
reshaped_output = cat(3, out_r, out_g, out_b);
注意:
的类f
是double
,值的范围应该是 [0, 1]。
上面的所有值1
都1
在使用imshow
.
您可以初始化f
为:
f(:,:,1)=[1;0;0;0;0];
f(:,:,2)=[0;1;0;0;1];
f(:,:,3)=[0;0;1;0;1];
或转换为uint8
:
f(:,:,1)=[255;0;0;0;0];
f(:,:,2)=[0;255;0;0;255];
f(:,:,3)=[0;0;255;0;255];
f = uint8(f);
推荐阅读
- javascript - Discord OGP 嵌入是否考虑了 JavaScript?
- javascript - 如何从函数内部调用函数?
- java - 使用不同的排列和组合更新对象数据结构
- html - 在 Wordpress 中将 URL 传递给 iframe
- arduino - Arduino 顺序软件serial.print() 覆盖自身
- xcode - 为 Apple M1 和较旧的英特尔设备构建 FFTW
- javascript - 关闭浏览器时如何更新数据库?
- mysql - 当我在 MySQL 中删除自己的变量时出错
- kotlin - 聚合根 id 引用字段命名约定
- php - 保存产品元字段以在 WooCommerce 中订购项目元