Clear Filters
Clear Filters

Image segmentation with different lighting conditions (Color Thresholder)

7 views (last 30 days)
I want to segment 72 images but they have different lighting conditions. I guess I have to set the thresholds for almost every single image. Of course, this is time consuming. After the segmentation I will estimate the tyre contact area through the known area of the DINA4 format.
Could you please give me some recommendations for the Color Threshold settings?
The images area attached
Thanks in advance
  2 Comments
Image Analyst
Image Analyst on 12 Dec 2023
Don't zip them up. Just attach them individually. I, for some reason, can't extract the images from the zip file - it throws 2 different errors depending on how I try to extract them.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 13 Dec 2023
Try this. I tested it only with the first image. See attached demo to convert to pixels. Through you could do that automatically by finding the black rectangle and getting its area.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
markerSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = pwd;
baseFileName = 'IMG_2856_r.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get size
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Threshold the image.
[binaryImage,maskedRGBImage] = createMask(rgbImage);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
impixelinfo;
axis('on', 'image');
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Call a closing to fill in and connect some small blobs.
se = true(5);
binaryImage = imclose(binaryImage, se);
% Invert it and remove things touching the border.
% This assumes the tire track should be completely contained
% by a white border.
binaryImage = imclearborder(~binaryImage);
% Take the largest blob.
binaryImage = bwareafilt(binaryImage, 1);
% Fill holes.
binaryImage = imfill(binaryImage, 'holes');
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
impixelinfo;
axis('on', 'image');
title('Final Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get area weighted by shape of outline.
area1 = bwarea(binaryImage)
% Get area as a simple pixel count.
props = regionprops(binaryImage, 'Area');
area2 = props.Area
% Display the original image.
subplot(2, 2, 4);
imshow(rgbImage);
axis('on', 'image');
hold on;
% Get the boundary of the mask.
boundaries = bwboundaries(binaryImage);
% Display the outline over the original image.
visboundaries(boundaries, 'Color','r', 'LineWidth',3);
caption = sprintf('Area = %.1f pixels', area2)
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 13-Dec-2023
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 0.206;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.268;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!