-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosanalysis.m
61 lines (47 loc) · 2.33 KB
/
rosanalysis.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
clc
close all
% Specify the folder containing the immunofluorescence images
folder = 'positive-con';
% Get a list of all image files in the folder
fileList = dir(fullfile(folder, '*.tif'));
% Loop over each image file
for i = 1:numel(fileList)
% Read the image
filename = fullfile(folder, fileList(i).name);
image = imread(filename);
% Convert the image to grayscale
grayImage = im2gray(image);
% Threshold the image to create a binary mask
threshold = graythresh(grayImage);
binaryImage = imbinarize(grayImage, threshold);
% Clean up the binary mask (optional)
% Perform morphological operations such as erosion or dilation to remove noise or refine the mask
cleanBinaryImage = bwareaopen(binaryImage, 500); % Remove small objects
% Measure the properties of the binary mask regions
props = regionprops(cleanBinaryImage, grayImage, 'Area', 'MeanIntensity');
% Calculate the product of area and average intensity
product = [props.Area]' .* [props.MeanIntensity]';
% Calculate the weigthed pixel intensity
weighted = ([props.Area]' .* [props.MeanIntensity]')./[props.Area]';
% Display the binary mask with overlaid region properties
overlayedImage = imoverlay(image, cleanBinaryImage, 'g'); % Green overlay
figure;
imshow(overlayedImage);
title(['Masked Image ', num2str(i)]);
% Display the average intensity, area, and product for each region
for j = 1:numel(props)
fprintf('Region %d:\n', j);
fprintf(' Average Intensity: %.2f\n', props(j).MeanIntensity);
fprintf(' Area: %d pixels\n', props(j).Area);
fprintf(' Product: %.2f\n', product(j));
fprintf(' Weighted Intensity: %.2f\n', weighted(j));
end
% Create a table with the measurements and product
measurements = table([props.Area]', [props.MeanIntensity]', product, weighted, 'VariableNames', {'Area', 'AverageIntensity', 'TotalPixelValue','WeightedPixelValue'});
% Generate a unique filename for each image
[~, name, ~] = fileparts(filename);
outputFilename = fullfile(folder, [name, '_measurements.xlsx']);
% Save the table to an Excel file
writetable(measurements, outputFilename);
disp(['Measurements saved to ', outputFilename]);
end