Automating colour segmentation script
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Using the code below, my aim is to get the coordinates of ants that are painted with green blobs.
To do this, I manually draw a shape around the desired paint spot to use as a colour reference to detect similar coloured pixels. I want to be able to do this for one image, and then use the same colour reference for all images in a folder without having to redraw the shape every time.
The code below " ************ " can be run again after reading in a different image, using the first image as a reference. The issue is, I have no idea how to automate this process - I need to automatically read in a new image, and run all the code below *******, saving two files - a text file with the coordinates, and the mask image.
I've tried to create a function to do this, but haven't had any luck yet. Any pointers or advice would be massively appreciated.
% read image, convert to LAB
ant = imread("IMG_4335.GREEN.JPG");
ant = imgaussfilt(ant,2); % introduce gaussian filter, to avoid jagged edges later
antLAB = rgb2lab(ant); % convert RGB to LAB colour space
antL = antLAB(:,:,1); % luminosity
antA = antLAB(:,:,2); % red-green axis
antB = antLAB(:,:,3); % blue-yellow axis
%% Draw shape filled with a paint spot - want to run this for ONE image in a folder,
% to use the values as a reference for ALL images
imshow(ant)
roi = drawpolygon % draw shape to capture the colour spot you want, then press enter
% use the shape above (roi), to get pixel values of drawn area
BWant = createMask(roi); % create a mask using the drawn shape
aROI = antA(BWant); %get the pixel values for A channel
bROI = antB(BWant); % get the pixel values for B channel
meanROI = [mean(aROI) mean(bROI)]; % take mean values for each channel
aMean = meanROI(1); % index above, channel a
bMean = meanROI(2); %index above, channel b
% ************
% read a second image (remove %s), convert to LAB
% ant = imread("SECOND.IMAGE.JPG");
% ant = imgaussfilt(ant,2); % introduce gaussian filter, to avoid jagged edges later
% antLAB = rgb2lab(ant); %convert RGB to LAB colour space
% antL = antLAB(:,:,1); % luminosity
% antA = antLAB(:,:,2); % red-green axis
% antB = antLAB(:,:,3); % blue-yellow axis
%% create distance matrix - diff between avg colour and every pixel
distLab = sqrt((antA - aMean).^2 + (antB - bMean).^2);
% treshold the distance matrix - which pixels are similar to sample colour
mask = distLab < 13; % smaller - stricter threshold
% dilate image, if two dots close, should merge them. Be careful with busy images
se = strel("disk",6); % create disk shaped area to use to dilate image next
dilatedant = imdilate(mask,se); % dilate iamge - to join blotches that should be one blob
maskfilledholes = imfill(dilatedant,"holes"); % fill in holes
maskcleaned = bwareaopen(maskfilledholes,10); % remove pixels smaller than 10
% find coordinates of centroids, write into text file
antmeasurements = regionprops(maskcleaned, "all");
centroids = cat(1,antmeasurements.Centroid); % turn centroids into two columns (x and y coordinates)
dlmwrite("antcoordinates.txt",centroids);
% save output image to filename
imwrite(maskcleaned,"outputantimage.jpg");
4 comentarios
KALYAN ACHARJYA
el 22 de Feb. de 2021
Is your task to count such ants (with green color) or do you have to segments all such ants?
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!