1. Load Sample Image (Serves as background reference)
This example uses the reference image example.jpg.
refImage = imread("example.jpg");
This image is of an empty glass with a resolution of 640 x 480.
Alternatively, you can capture this image using the Acquire Webcam Image task.
2. Set up Output Window (Plot GUI)
Set up the figure to plot the sludge level changes in real time.
fig = figure("Name","Sludge level tracker","Visible","off");
t = tiledlayout(2,2,"Parent",fig);
title("Reference Image","Parent",ax1);
imshow(refImage,"Parent",ax1);
title('Sludge level vs Time',"Parent",ax2);
xlabel('Time (in s)',"Parent",ax2);
ylabel('Sludge level (normalized)',"Parent",ax2);
title("Sludge Level","Parent",ax3);
h = animatedline(ax2,"Color","red","LineWidth",3);
webcamLETFig = figure("Name","Acquire Webcam Image Output");
3. Plot Sludge Level Change
To determine the change in sludge level, use image subtraction. Subtract each sample image from the reference image. To capture the sample images,use the Acquire Webcam Image task.
Ensure the following in the Live Task -
Select a webcam from the webcams connected to your computer.
Clear the Show image check box to prevent displaying the image in each loop iteration.
Turn off the Autorun feature if you want the live task to execute only when the entire script is executed. To disable autorun, click the autorun icon.
Select Live preview to view the live video from the webcam while adjusting webcam properties.
Capture images using the Acquire Webcam Image task in a loop. You can vary the time for which the loop executes by entering the time in seconds in the textbox.
Subtract each sampleImage from the refImage to get the diffImage.
Extract the the RGB components from diffImage and convert the image to grayscale to reduce the noise in the image.
Find the mean pixel intensity value across each row of the image. The mean pixel intensity is used to find the image pixel corresponding to the top of the sludge level.
Define a window size and a threshold for the intensity by moving the slider.
Find the difference in the instensities between the n+10th pixel and nth pixel in the windowSize. 'n' is the row index and 10 is the window size used in this example. If the intensity of the pixel value is greater than the threshold intensity, store the pixel value as the sludge level.
Plot the sludge level against time.
cam = webcam("TOSHIBA Web Camera - FHD",...
"Resolution","640x480",...
"WhiteBalanceMode","auto",...
"BacklightCompensation",0);
diffImage = refImage - sampleImage;
greyDiffImage = zeros(dims(1),dims(2),'uint8');
greyDiffImage(row,col) = (0.2989 * R(row,col)) + (0.5870 * G(row,col)) + (0.1140 * B(row,col));
rowMean = zeros(1,dims(1));
rowMean(row) = mean(greyDiffImage(row,:));
sludgeLevelPixel = dims(1);
for rowIndex = 1:length(rowMean)-windowSize
if rowMean(rowIndex+windowSize) - rowMean(rowIndex) > intensityThreshold
sludgeLevelPixel = rowIndex;
imshow(sampleImage,"Parent",ax3);
quiver(dims(2)/2,sludgeLevelPixel,0,dims(1)-sludgeLevelPixel,...
"Color","yellow","LineWidth",3,'Parent',ax3);
addpoints(h,tStamp,abs(1 - (sludgeLevelPixel/dims(1))));
for k = 1 : length(sludgeLevelPixel)
fprintf('(x(%d), y(%d)) = (%.4f, %.4f)\n',...
k, k, sludgeLevelPixel(k), tStamp(k));