how to to let model automatically send the output of each step to be input for next step

2 views (last 30 days)
If I would like to apply anytype of filters at the shown place in the code to be input for next stage to find the threshold how can I make it (how can I type it)?
% Program to compute the mean width of a blob in an image.
clearvars;
close all;
clc;
fontSize = 15;
% Read in original image, with white lightning on black background.
baseFileName = 'bb.jpg';
fullFileName = fullfile(pwd, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = grayImage(:, :, 3); % Take Blue channel.
else
grayImage = grayImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
subplot(2, 3, 1);
imshow(grayImage, [])
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('Original Image', 'FontSize', fontSize);
% apply filter for example gaussian or any thing else
fi = (imgray);
figure, imshow(fi)
subplot(2, 3, 2);
imshow(fi)
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('filtered Image', 'FontSize', fontSize);
% Binarize the image.
% mask = imbinarize(grayImage);
lowThreshold = 0;
highThreshold = 230;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
[lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, fi);
mask = fi >= lowThreshold & fi <= highThreshold
% Fill holes.
mask = imfill(mask, 'holes');
subplot(2, 3, 2);
imshow(mask)
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('Mask', 'FontSize', fontSize)

Answers (1)

Rahul
Rahul on 12 Jul 2023
Hi Yasmin,
According to your description, you want to apply any type of filters on your image and be able to use the output of that as input for your next piece of code.
You can use the imgaussfilt function for optaining the filtered image and the output of that can furthur be used.
clearvars;
close all;
clc;
fontSize = 15;
baseFileName = 'bb.jpg';
fullFileName = fullfile(pwd, baseFileName);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = grayImage(:, :, 3); % Take Blue channel.
else
grayImage = grayImage; % It's already gray scale.
end
subplot(2, 3, 1);
imshow(grayImage, [])
impixelinfo; % Let user mouse around and see values in the status line at the lower right.
title('Original Image', 'FontSize', fontSize);
filteredImage = imgaussfilt(grayImage, 2); % Adjust the standard deviation (sigma) as needed
subplot(2, 3, 2);
imshow(filteredImage)
impixelinfo;
title('Filtered Image', 'FontSize', fontSize);
thresholdValue = graythresh(filteredImage);
mask = imbinarize(filteredImage, thresholdValue);
mask = imfill(mask, 'holes');
subplot(2, 3, 3);
imshow(mask)
impixelinfo;
title('Mask', 'FontSize', fontSize)
I have made changes to your code and used the imgaussfilt as a function to filter. If required you can filtered according to your needs with different functions and attributes.
Thanks.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!