2

我是 matlab 和编程的新手。我有一个大小为 [640 780] 的 RGB 图像。现在假设我只需要那些红色值超过 100 并且剩余的像素较少的像素我转换为 255。现在我想知道如何将这些需要的像素存储在不同的矩阵中,以便我可以直接使用这些值在原始 RGB 图片中导航以绘制 ROI ???

a = 1; b = 1;
maybe_red = RGB;
for i = 1:sizeim(1)
     for j = 1:sizeim(2)
if RGB(i,j,1) > 100 
        red_trace_x(a) = i;
        red_trace_y(b) = j;
        b = b+1;
    else
        maybe_red(i,j,:) = 1;
    end
end
a = a+1;
end

目前我正在存储xy在单独的数组中。但我想知道如何将这两个x,y值存储在单个矩阵中。

谢谢。!

4

2 回答 2

2

下面生成一个掩码(与原始图像大小相同的逻辑数组),其中红色通道值大于 100 的像素存储为 1,其他像素存储为 0:

img= imread('peppers.jpg');
mask=img(:,:,1)>100;

您可以使用掩码对原始图像进行索引并对其进行更改,find以确定与值为 1 的掩码像素对应的线性索引:

indlin=find(mask);
img2 = img;

您可以直接使用线性索引,例如使红色通道饱和:

img2(indlin) = 255;

或绿色通道:

n = numel(img)/3;
img2(indlin+n) = 255;

从左到右,原始图像、蒙版、红色和绿色:

在此处输入图像描述

编辑

您可以检索数组下标ixiy从线性索引

[ix iy]=ind2sub(size(mask),indlin);
于 2013-09-12T09:39:32.040 回答
1

如果你只存储元素的索引呢?

%% Load a sample image
RGB = imread('football.jpg'); 

b = 1; 
maybe_red = RGB;
red = RGB(:,:,1); % Get the red component

% i will represent the index of the matrix
for i = 1:length(red(:))  % length(red(:)) = rows*cols
    if red(i) > 100 
        red_trace(b) = i;
        b = b+1;
    else
        [r,c] = ind2sub(size(red),i);  % Convert the index to rows and cols
        maybe_red(r,c,:) = 1;
    end
end

imshow(maybe_red);

但这可能更容易做到

red = RGB(:,:,1); % Get the red component of the image
[r,c] = find(red>100);
coord = [r c];
于 2013-09-12T09:30:39.950 回答