【问题标题】:MATLAB - Splitting an image converts the blocks into grayscaleMATLAB - 分割图像将块转换为灰度
【发布时间】:2015-03-22 11:54:11
【问题描述】:

使用 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 image-processing


    【解决方案1】:

    您只考虑了图像的宽度和高度。但实际上对于彩色图像,颜色是 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);
    

    原图:

    分区图像:

    【讨论】:

    • 谢谢。有效。但是当我在这一行的末尾添加颜色维度时: imgij = img1((i-1)*rnew+1:irnew, (j-1)*cnew+1:jcnew ,:); Matlab 给了我这个错误:“索引超出矩阵维度。”你也能帮我理解一下吗?
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2012-11-04
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多