在 MATLAB 中使用 PixelList 获取 RGB 图像中的像素值

Get pixel values in RGB images using PixelList in MATLAB

我正在尝试从 RGB 图像中的感兴趣区域获取像素强度值。

我在MATLAB中使用regionprops'PixelList'分割了图像并保存了感兴趣区域(ROI),如下图:

在这个例子中,我使用了在 MATLAB 中构建的“onion.png”图像。 (但实际上我有数百张图像,每张图像都有几个 ROI,因此我单独保存 ROI 的原因。)

%SEGMENTATION PROGRAM:

a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');

figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0

% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5; 
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); pause;



roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centriod positions as two colums

%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE

for ww=1:w;
    k=roi2(ww).PixelList;
    save('onion_PL','k'); 
end

如何使用此像素列表获取原始图像中的强度值?更具体地说,我需要获得绿色通道中的像素与红色通道的比率(“grr=rdivide(gg,rr);”),但仅限于标有 PixelList 的感兴趣区域。到目前为止,这是我的代码:

%PL is the PixelList output we got above.

a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');

PL=dir(['*PL.mat']); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m=1:length(PL);
    load(PL(m).name);
    ex=[]; %empty matrix to hold the extracted values
for mm=1:length(k); 

%INSERT ANSWER HERE

end

下一位是错误的,因为它基于整个图像(“a”),但它包含我想在 ROI 中执行的计算

figure; imshow(a,[]);
pause;

nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values 
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);

我是 MATLAB 的新手,所以我的代码不是最好的...任何建议将不胜感激。提前致谢!

您正在寻找的循环似乎很简单:

grr = zeros(nrows, ncols); % Initialize grr with zeros.

for mm = 1:length(k)
    x = k(mm, 1); % Get the X (column) coordinate.
    y = k(mm, 2); % Get the Y (row) coordinate.
    grr(y, x) = gg(y, x) / rr(y, x);
end

一个更有效的解决方案是使用 sub2ind 将 x,y 坐标转换为线性索引:

% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));

% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));

在您给定的代码示例中,有 5 个像素列表。
不知道你要怎么“排列”结果
在我的代码示例中,我将 5 个结果保存到 5 个 mat 文件中。


这是一个可执行代码示例:

close all

%SEGMENTATION PROGRAM:

a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');

figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0

% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5; 
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); %pause;



roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
    roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centroid positions as two columns

%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE

for ww=1:w
    k=roi2(ww).PixelList;
    %save('onion_PL', 'k');
    save(['onion', num2str(ww), '_PL'], 'k'); % Store in multiple files - onion1_PL.mat, onion2_PL.mat, ... onion5_PL.mat
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear % Use clear for testing - the variables are going to be read from the mat file.

%PL is the PixelList output we got above.

a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');

nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values 
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);

PL=dir('*PL.mat'); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m = 1:length(PL)
    load(PL(m).name);
    ex=[]; %empty matrix to hold the extracted values
    
    %for mm=1:length(k)

    %INSERT ANSWER HERE
    grr = zeros(nrows, ncols); % Initialize grr with zeros.
    
    for mm = 1:length(k)
        x = k(mm, 1); % Get the X (column) coordinate.
        y = k(mm, 2); % Get the Y (row) coordinate.
        grr(y, x) = gg(y, x) / rr(y, x);
    end
    
    % Instead of using a loop, it's more efficient to use sub2ind
    if false
        % Convert k to linear indices.
        kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
     
        % Update only the indices in the PixelList.
        grr(kInd) = rdivide(gg(kInd), rr(kInd));
    end
        
    figure;imshow(grr);title(['grr of m=', num2str(m)]) % Show grr for testing.
    save(['grr', num2str(m)], 'grr'); % Save grr for testing.
    imwrite(imadjust(grr, stretchlim(grr)), ['grr', num2str(m), '.png']); % Store grr as image for testing
end

前两个grr矩阵作为图像(用于测试):

grr1.png:

grr2.png: