【问题标题】:How to find and highlight the brightest region an image in Matlab?如何在 Matlab 中找到并突出显示图像中最亮的区域?
【发布时间】:2021-07-10 21:48:12
【问题描述】:

亲爱的,

我想请求您的支持。我的目标是找到 RGB 图像中最亮的区域并在不使用其他工具的情况下突出显示它。请参阅下面的示例。

rgbImage = imread( 'Zoom1_WhiteImage.png' );
imshow(rgbImage);

[rows, columns, numberOfColorChannels] = size(rgbImage)

[x, y] = meshgrid(1:columns, 1:rows);
% Extract the individual red, green, and blue color channels.
% Need to cast to double or else x and y will be clipped to 255 when we concatenate them.
if numberOfColorChannels == 1
    % Leave as gray scale.
    % Get array listing [r, g, b, x, y].  Using (:) will turn all the 2-D arrays into column vectors.
    output = [rgbImage(:), x(:), y(:)];
else
    redChannel = double(rgbImage(:, :, 1));
    greenChannel = double(rgbImage(:, :, 2));
    blueChannel = double(rgbImage(:, :, 3));
    % Get array listing [r, g, b, x, y].  Using (:) will turn all the 2-D arrays into column vectors.
    output = [redChannel(:), greenChannel(:), blueChannel(:), x(:), y(:)];
end

[rows, columns] = find(rgbImage == 155);
imshow(rgbImage);
hold on

不幸的是,我正在为如何继续绘制应该覆盖灰色图像的点而苦苦挣扎。

请您帮我完成代码好吗?

【问题讨论】:

    标签: matlab image-processing rgb grayscale roi


    【解决方案1】:

    我建议您阅读 MATLAB 中的逻辑索引 - 这是一个非常强大的概念,它允许您通过展平数组来跳过您尝试做的大部分事情,并使用 @ 创建单独的索引数组987654322@。例如,Here is an article 解决了底部向下的逻辑索引。

    我已修改并添加到您的代码中,以便它使用逻辑索引来完成这项工作。我在 R2019b 中对此进行了测试。

    rgbImage = imread( 'Zoom1_WhiteImage.png' );
    imshow(rgbImage);
    
    [rows, columns, numberOfColorChannels] = size(rgbImage)
    
    % This is unnecessary - the 'find' command will generate the indices 
    %   without you preparing a matrix of them:
    % [x, y] = meshgrid(1:columns, 1:rows);   
    
    if numberOfColorChannels == 1
        % No need to flatten the image, or combine it with indices:
        %output = [rgbImage(:), x(:), y(:)];
    
        brightness = rgbImage;  % For a 1 channel image, brightness is the same as the original pixel value.
    
    else
    %     redChannel = double(rgbImage(:, :, 1));
    %     greenChannel = double(rgbImage(:, :, 2));
    %     blueChannel = double(rgbImage(:, :, 3));
        % Get array listing [r, g, b, x, y].  Using (:) will turn all the 2-D arrays into column vectors.
    %     output = [redChannel(:), greenChannel(:), blueChannel(:), x(:), y(:)];
    
        % For an RGB image, the brightness can be estimated in various ways, here's one standard formula: 
        brightness = (0.2126*rgbImage(:, :, 1) + 0.7152*rgbImage(:, :, 2) + 0.0722*rgbImage(:, :, 3));
    end
    
    % Establish a brightness threshold - pixels brighter than this value will
    % be highlighted
    threshold = 215;
    
    % Create a zero-filled mask of equal size to the image to hold the
    % information of which pixels met the brightness criterion:
    mask = zeros(rows, columns, 'logical');
    
    % Assign "1" to any mask pixels whose corresponding image pixel met the
    % brightness criterion:
    mask(brightness > threshold) = 1;
    
    figure;
    % Overlay and display mask. imoverlay was introduced in R2017, but the 
    % syntax has changed a bit, so check the version you're using. You can also
    % use imfuse, or imshowpair, or make your own image blending algorithm, 
    % depending on how you want it to look.
    imshow(imoverlay(rgbImage, mask, 'red'));
    hold on
    

    【讨论】:

    • 亲爱的Brionius,非常感谢。这正是我所需要的。我一定会阅读这篇文章来提高我的 matlab 技能。
    猜你喜欢
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多