% Initialize video paths and load the video videoFile = 'FilePath\Movie.avi'; outputClusterVideo = 'FilePath\Clusters_Detected.avi'; outputLargeParticlesVideo = 'FilePath\LargeParticles_Detected.avi'; videoReader = VideoReader(videoFile); frameRate = videoReader.FrameRate; % Create VideoWriter objects to save output videos clusterVideoWriter = VideoWriter(outputClusterVideo, 'Grayscale AVI'); largeParticleVideoWriter = VideoWriter(outputLargeParticlesVideo, 'Grayscale AVI'); clusterVideoWriter.FrameRate = frameRate; largeParticleVideoWriter.FrameRate = frameRate; open(clusterVideoWriter); open(largeParticleVideoWriter); % Create invisible figures for visualization (won't pop up) fig1 = figure('Visible', 'off'); % For clusters fig2 = figure('Visible', 'off'); % For large particles % Define structuring elements for smoothing seSmall = strel('disk', 3); % Smaller smoothing for small features seLarge = strel('disk', 10); % Larger smoothing for larger clusters % Parameters for frame selection startFrame =1; % Example start frame endFrame =3036; % Example end frame % Move to the start frame videoReader.CurrentTime = (startFrame - 1) / frameRate; frameCount = startFrame; % Process each frame in the video while hasFrame(videoReader) && frameCount <= endFrame % Read the current frame frame = readFrame(videoReader); grayFrame = rgb2gray(frame); % Convert to grayscale if needed % Calculate average intensity of the frame avgIntensity = mean(grayFrame(:)); n22=avgIntensity/255; % Stretch intensity of each frame minIntensity = double(min(grayFrame(:))); maxIntensity = double(max(grayFrame(:))); % Rescale the intensities to [0, 255] grayFrame = uint8(255 * (double(grayFrame) - minIntensity) / (maxIntensity - minIntensity)); disp(['Processing Frame: ' num2str(videoReader.CurrentTime * frameRate)]); %% 1. Detect Clusters Using Lower Variance Threshold flatgrayFrame = imflatfield(grayFrame, 50); % Radius of 20 pixels for correction smoothgrayFrame = imgaussfilt(flatgrayFrame,1.5); % Radius of 20 pixels for correction % Thresholding could be applied if needed (e.g., 0.5 as a threshold) threshold = 0.36; % Set threshold for converting back to binary ClusterMask=imbinarize(smoothgrayFrame,threshold); ClusterMask=imcomplement(ClusterMask); ClusterMask = bwareaopen(ClusterMask,2); ClusterMask = imfill(ClusterMask, "holes"); ClusterMask = bwareaopen(ClusterMask, 40); % Display and write the binary frame for clusters figure(fig1); imshow(ClusterMask); drawnow; writeVideo(clusterVideoWriter, uint8(ClusterMask) * 255); % Save binary frame %% 2. Detect Large Particles Using Higher Variance Threshold varianceThresholdHigh =1400/n22; binaryLargeParticles = imbinarize(localVariance, varianceThresholdHigh / max(localVariance(:))); filteredLargeParticles = bwareaopen(binaryLargeParticles, 2); filteredLargeParticles = imfill(filteredLargeParticles, "holes"); smoothedLargeParticles = imclose(filteredLargeParticles, seSmall); % Convert the averaged image to binary (optional) % Thresholding could be applied if needed (e.g., 0.5 as a threshold) threshold = 0.7; % Set threshold for converting back to binary LargeParticleMask=imbinarize(grayFrame,threshold); LargeParticleMask=imcomplement(LargeParticleMask); LargeParticleMask = bwareaopen(LargeParticleMask,2); LargeParticleMask=imcomplement(LargeParticleMask); % Display and write the binary frame for large particles figure(fig2); imshow(LargeParticleMask); drawnow; writeVideo(largeParticleVideoWriter, uint8(LargeParticleMask) * 255); % Save binary frame % % Move to the next frame frameCount = frameCount + 1; end % Close video writers and figures close(clusterVideoWriter); close(largeParticleVideoWriter); close(fig1); close(fig2); disp('Processing completed. Videos saved successfully.');