How to find high contrast objects in image?

1 回表示 (過去 30 日間)
Jakub
Jakub 2025 年 3 月 27 日
コメント済み: Walter Roberson 2025 年 3 月 27 日
I have matrix 50x200 and i want script that will go through the matrix and compare values with its surrounding values which gives contrast ratio, very higher results will mean object with higher contrast. Usage is detecting objects in image from luminance analyser detected while driving in car.
  1 件のコメント
Jakub
Jakub 2025 年 3 月 27 日
Each value in matrix is luminance value of each image`s pixel.

サインインしてコメントする。

回答 (1 件)

TED MOSBY
TED MOSBY 2025 年 3 月 27 日
編集済み: TED MOSBY 2025 年 3 月 27 日
Hi Jakub,
The contrast ratio is the absolute difference between the center pixel and the mean of its 8 neighbors divided by the neighbor mean. You can simply loop through the pixels and extract a 3*3 block around each of it, then calculate the contrast.
I have written an example script for your reference:
imageMatrix = rand(50,200); % This is just an example matrix
[m, n] = size(imageMatrix);
contrastMatrix = zeros(m, n);
% Loop through the matrix excluding the boundary pixels
for i = 2:m-1
for j = 2:n-1
% Extract the 3x3 neighborhood centered at (i,j)
neighborhood = imageMatrix(i-1:i+1, j-1:j+1);
% Calculate the sum of the 3x3 block and subtract the center pixel
centerPixel = imageMatrix(i,j);
localMean = (sum(neighborhood(:)) - centerPixel) / 8;
% Compute the contrast ratio
if localMean ~= 0
contrastMatrix(i,j) = abs(centerPixel - localMean) / localMean;
else
contrastMatrix(i,j) = 0;
end
end
end
% Visualize the contrast matrix
figure;
imagesc(contrastMatrix);
colorbar;
title('Contrast Ratio Map');
xlabel('Pixel Column');
ylabel('Pixel Row');
The output appears as below:
Hope this helps!
  1 件のコメント
Walter Roberson
Walter Roberson 2025 年 3 月 27 日
This sounds like a job for
abs(conv2(double(imageMatrix), [1 1 1; 1 -1 1; 1 1 1]/8, 'same'))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeComputer Vision with Simulink についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by