Как найти и выделить самую яркую область изображения в Matlab?

Дорогие,

Я хотел бы попросить вас о поддержке. Моя цель — найти самую яркую область изображения 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

К сожалению, я борюсь с тем, как продолжить график точек, которые должны накладываться на серое изображение.

Не будете ли вы так любезны и поможете мне закончить код, пожалуйста?


person Tomáš    schedule 15.04.2021    source источник


Ответы (1)


Я бы порекомендовал вам прочитать о логическом индексировании в MATLAB — это очень мощная концепция, которая позволяет вам пропустить большую часть того, что вы пытаетесь сделать, сглаживая массивы и создавая отдельные массивы индексов с помощью meshgrid. Вот статья, посвященная логическому индексированию внизу, например.

Я изменил и добавил ваш код, чтобы он выполнял работу, используя логическое индексирование. Я тестировал это в 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
person Brionius    schedule 16.04.2021
comment
Дорогой Бриониус, большое спасибо. Это именно то, что мне нужно. Я обязательно прочитаю статью, чтобы улучшить свой навык Matlab. - person Tomáš; 16.04.2021