MATLAB - 分割图像将块转换为灰度
MATLAB - Splitting an image converts the blocks into grayscale
我正在尝试使用 MATLAB 将随机大小的图像转换为四个相等的块。我正在使用 "for" 循环来创建块。我面临的问题是这些块正在转换为灰度,而我希望这些块保留其原始形式,即 RGB 通道。这是我使用的代码:
clear all;
img1 = imread('ABC.png');
[rs, cols, colorchan] = size(img1);
rnew = int32(rs/2);
cnew = int32(cols/2);
for i = 1:4
for j = 1:4
imgij = img1((i-1)*rnew+1:i*rnew, (j-1)*cnew+1:j*cnew);
figure();
imshow(imgij);
%do some other stuff here%
end
end
我是 MATLAB 的新手,这是我自己能做的最好的。有人可以告诉我如何保留父图像每个块的原始形式吗?任何帮助将不胜感激。
您只考虑了图像的宽度和高度。但实际上对于彩色图像,颜色是 Matlab 中的第三维。试试下面的代码。
img = imread('onion.png');
figure; imshow(img);
w = size(img,2); % width of the original image
h = size(img,1); % height of the original image
wm = floor(w/2); % middle of the width
hm = floor(h/2); % middle of the height
figure;
imgtl = img(1:hm,1:wm,:); % top left image
subplot(2,2,1); imshow(imgtl);
imgtr = img(1:hm,wm+1:w,:); % top right image
subplot(2,2,2); imshow(imgtr);
imgbl = img(hm+1:h,1:wm,:); % bottom left image
subplot(2,2,3); imshow(imgbl);
imgbr = img(hm+1:h,wm+1:w,:); % bottom right image
subplot(2,2,4); imshow(imgbr);
原图:
分区图像:
我正在尝试使用 MATLAB 将随机大小的图像转换为四个相等的块。我正在使用 "for" 循环来创建块。我面临的问题是这些块正在转换为灰度,而我希望这些块保留其原始形式,即 RGB 通道。这是我使用的代码:
clear all;
img1 = imread('ABC.png');
[rs, cols, colorchan] = size(img1);
rnew = int32(rs/2);
cnew = int32(cols/2);
for i = 1:4
for j = 1:4
imgij = img1((i-1)*rnew+1:i*rnew, (j-1)*cnew+1:j*cnew);
figure();
imshow(imgij);
%do some other stuff here%
end
end
我是 MATLAB 的新手,这是我自己能做的最好的。有人可以告诉我如何保留父图像每个块的原始形式吗?任何帮助将不胜感激。
您只考虑了图像的宽度和高度。但实际上对于彩色图像,颜色是 Matlab 中的第三维。试试下面的代码。
img = imread('onion.png');
figure; imshow(img);
w = size(img,2); % width of the original image
h = size(img,1); % height of the original image
wm = floor(w/2); % middle of the width
hm = floor(h/2); % middle of the height
figure;
imgtl = img(1:hm,1:wm,:); % top left image
subplot(2,2,1); imshow(imgtl);
imgtr = img(1:hm,wm+1:w,:); % top right image
subplot(2,2,2); imshow(imgtr);
imgbl = img(hm+1:h,1:wm,:); % bottom left image
subplot(2,2,3); imshow(imgbl);
imgbr = img(hm+1:h,wm+1:w,:); % bottom right image
subplot(2,2,4); imshow(imgbr);
原图:
分区图像: