SOS! PLEASE SAVE MY CODE:

2 views (last 30 days)
Chanille
Chanille on 9 Mar 2023
Edited: Chanille on 14 Mar 2023
In my code I have the following issues: 1. the count measurement is not accurately outputting the count of blobs in the zones of the image. 2. When I normalize the count the count becomes decimal values, how can I fix this? 3. I want to make sure that the code is not measuring any of the large black areas in the image. Can someone please help me fix this, thanks!
  1 Comment
Walter Roberson
Walter Roberson on 9 Mar 2023
We are not going to read through hundreds of lines code mentally modeling everything that could possibly go wrong. We need some of your images, and you have to describe the difference between what you get and what you want for each one.

Sign in to comment.

Accepted Answer

Kevin Holly
Kevin Holly on 9 Mar 2023
Instead of trying to figure out how you calculated the areas. I used the following approach to find the masks:
fig = figure;
fontSize = 12;
% directoryInfo = dir('*.tif');
allFileNames = {'C1_L1_WDa.jpg'};
numfiles = length(allFileNames);
% Define the pixel size in square microns
pixelSize = 1;
numZones = 12;
for m = 1:numfiles
fprintf('Processing file #%d of %d : "%s".\n', m, numfiles, allFileNames{m});
MyRGBImage = fullfile(pwd, allFileNames{m});
% Read in image.
imageData = imread(MyRGBImage);
[rows, columns, numberOfColorChannels] = size(imageData);
if numberOfColorChannels > 1
grayImage = rgb2gray(imageData); % Convert to color.
end
% Apply background subtraction.
grayImage = imtophat(grayImage, strel('disk', 100));
%the lower this number the less lipid droplets
% Apply contrast correction.
grayImage = imadjust(grayImage);
% Apply Gaussian filtering.
grayImage = imgaussfilt(grayImage, 3);
% Apply thresholding.
thresholdValue = graythresh(grayImage);
binaryImage = imbinarize(grayImage, thresholdValue);
% Remove black elongated shapes.
binaryImage = bwareaopen(binaryImage, 200);
binaryImage = imclearborder(binaryImage);
binaryImage = imfill(binaryImage, 'holes');
% mask
% Threshold the image to create a binary mask
gray_img = rgb2gray(imageData); % Convert to color.
threshold = 5;
mask = gray_img > threshold;
% Remove small objects
min_size = 1000;
mask = bwareaopen(mask, min_size);
% Fill holes
mask = imfill(mask, 'holes');
% Erode and dilate the mask to remove noise and smooth edges
se = strel('disk', 5);
mask = imerode(mask, se);
mask = imdilate(mask, se);
close;
figure;
% ZZ: Right now for each single image, a separate window (figure)
% will be created to plot the related info, and the former figure will
% be closed.
subplot(3, 4, 4);
imshow(mask);
title('Functional Mask, thres = 5');
% Display the organelle segmented image.
subplot(3, 4, 2);
imshow(binaryImage, []);
axis on;
title('Organelle Segmented Image', 'FontSize', fontSize);
% Display the original image.
subplot(3, 4, 1);
imshow(imageData, []);
axis on;
title('Original Image', 'FontSize', fontSize);
% Set up figure properties:
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
sgtitle(sprintf('Analysis of Organelles in Image "%s"', allFileNames{m}), 'FontSize', 28);
% % % Locate the center
% % promptMessage = sprintf('Click at the center of the periportal region');
% % titleBarCaption = 'Continue?';
% % buttonText = questdlg(promptMessage, titleBarCaption, 'OK', 'Quit', 'OK');
% % if strcmpi(buttonText, 'Quit')
% % return;
% % end
% % [x,y] = ginput(1);
x = 4592.86363636364;
y = 705.954545454545;
% Find out what the max distance will be by computing the distance to each corner.
distanceToUL = sqrt((1-y)^2 + (1-x)^2);
distanceToUR = sqrt((1-y)^2 + (columns-x)^2);
distanceToLL = sqrt((rows-y)^2 + (1-x)^2);
distanceToLR= sqrt((rows-y)^2 + (columns-x)^2);
maxDistance = ceil(max([distanceToUL, distanceToUR, distanceToLL, distanceToLR]));
% Calculate the radius of each zone
radius = linspace(0, maxDistance, numZones+1);
subplot(3, 4, 3);
imshow(mask, []);
axis on;
title('Bin drawing', 'FontSize', fontSize);
hold on;
line([x, x], [1, rows], 'Color', 'r', 'LineWidth', 2);
line([1, columns], [y, y], 'Color', 'r', 'LineWidth', 2);
I added this section below
% Preallocate
zonemask = zeros(size(mask,1),size(mask,2),numZones+1);
fig2 = figure;
imshow(mask)
figure(fig2)
c(1) = drawcircle("Center",[x, y],"Radius",radius(1),"LineWidth",1,"Color","b","Visible","off");
% Draw the circles for each zone
for ii = 2:numZones+1
figure(fig2)
c(ii) = drawcircle("Center",[x, y],"Radius",radius(ii),"LineWidth",1,"Color","b","Visible","off");
zonemask(:,:,ii) = createMask(c(ii))-createMask(c(ii-1));
viscircles([x, y], radius(ii), 'LineStyle', '--', 'LineWidth', 1);
total_intensity(ii) = sum(sum(gray_img.*uint8(mask).*uint8(zonemask(:,:,ii)))); % or replace binaryImage with mask
total_zone_pixels(ii) = sum(sum(uint8(mask).*uint8(zonemask(:,:,ii))));
end
mean_intensity_zone = total_intensity./total_zone_pixels
end
Processing file #1 of 1 : "C1_L1_WDa.jpg".
mean_intensity_zone = 1×13
NaN 20.2110 18.5171 18.3373 16.1967 20.3758 21.0019 24.8756 23.1034 23.8200 22.3985 23.9102 16.3538
Verify Zones
figure
tiledlayout(5,3)
nexttile
imshow(zonemask(:,:,1))
nexttile
imshow(double(mask).*zonemask(:,:,1))
nexttile
imshow(gray_img.*uint8(mask).*uint8(zonemask(:,:,1)))
for ii = 2:5
nexttile
imshow(zonemask(:,:,ii))
nexttile
imshow(double(mask).*zonemask(:,:,ii))
nexttile
imshow(gray_img.*uint8(mask).*uint8(zonemask(:,:,ii)))
end
Below I made a colored version with the original image.
figure
mask2(:,:,1) = uint8(mask).*uint8(zonemask(:,:,ii));
mask2(:,:,2) = mask2(:,:,1);
mask2(:,:,3) = mask2(:,:,1);
imshow(imageData.*mask2)
  10 Comments
Walter Roberson
Walter Roberson on 12 Mar 2023
An "XY Problem" is when a person asks to do something (X) because they think it will help them reach their goals, but their goals turn out to be something else (Y) that turns out not to need to use X after-all.
Suppose for example you were asking how to get epoxy to stick to anodized aluminum, but it turned out that your real question was about how to fasten down the piece of anodized aluminum. You would (in this example) have decided that you needed to "epoxy" to solve your problem, but "epoxy" turned out to be a distraction, and if you had stated from the beginning what you needed to fasten to what, then perhaps someone could have suggested bolts or welding or crazy glue.
Chanille
Chanille on 13 Mar 2023
Thanks. My question was answered!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!