Lab08
Lab08
Lab 08
Submitted To:
Professor Dr. Ahmed Wasif Reza
Department of Computer Science and Engineering
East West University
Submitted By:
Korobi Sarker
ID: 2020-1-60-161
Solution:
image = imread('Picture1.png'); % Replace with your image file
figure;
subplot(2,4,1);
imshow(image);
title('Original Image');
grayImage = rgb2gray(image);
globalThresh = graythresh(grayImage);
globalBinaryImage = imbinarize(grayImage, globalThresh);
subplot(2,4,3);
imshow(globalBinaryImage);
title('Global Thresholding');
blockSize = 50;
variableBinaryImage = adaptiveThresholding(grayImage, blockSize);
subplot(2,4,4);
imshow(variableBinaryImage);
title('Variable Thresholding');
Solution:
image = imread('Picture1.png');
subplot(1,3,1);
imshow(image);
title('Original Image');
grayImage = rgb2gray(image);
pixelStack = [seedPoint];
while ~isempty(pixelStack)
currentPixel = pixelStack(end, :);
pixelStack(end, :) = [];
for i = 1:size(neighbors, 1)
neighborRow = currentPixel(1) + neighbors(i, 1);
neighborCol = currentPixel(2) + neighbors(i, 2);
if neighborRow > 0 && neighborRow <= rows && neighborCol > 0 && neighborCol <= cols
if ~outputMask(neighborRow, neighborCol) % If not already segmented
intensityDiff = abs(inputImage(neighborRow, neighborCol) - seedValue);
if intensityDiff <= threshold
outputMask(neighborRow, neighborCol) = true;
pixelStack = [pixelStack; neighborRow, neighborCol];
end
end
end
end
end
end
function outputMask = regionSplitAndMerge(inputImage, threshold)
inputImage = double(inputImage);
[rows, cols] = size(inputImage);
outputMask = zeros(rows, cols); % Initialize mask
regions = splitRegion(inputImage, 1, rows, 1, cols, threshold);
outputMask = mergeRegions(regions, inputImage, threshold);
end
function regions = splitRegion(inputImage, rStart, rEnd, cStart, cEnd, threshold)
subImage = inputImage(rStart:rEnd, cStart:cEnd);
intensityRange = range(subImage(:));
if intensityRange > threshold && (rEnd - rStart > 1) && (cEnd - cStart > 1)
midRow = floor((rStart + rEnd) / 2);
midCol = floor((cStart + cEnd) / 2);
regions1 = splitRegion(inputImage, rStart, midRow, cStart, midCol, threshold);
regions2 = splitRegion(inputImage, rStart, midRow, midCol+1, cEnd, threshold);
regions3 = splitRegion(inputImage, midRow+1, rEnd, cStart, midCol, threshold);
regions4 = splitRegion(inputImage, midRow+1, rEnd, midCol+1, cEnd, threshold);
regions = [regions1; regions2; regions3; regions4];
else
regions = [rStart, rEnd, cStart, cEnd];
end
end
function outputMask = mergeRegions(regions, inputImage, threshold)
[rows, cols] = size(inputImage);
outputMask = zeros(rows, cols);
for i = 1:size(regions, 1)
rStart = regions(i, 1);
rEnd = regions(i, 2);
cStart = regions(i, 3);
cEnd = regions(i, 4);
subImage = inputImage(rStart:rEnd, cStart:cEnd);
meanIntensity = mean(subImage(:));
if meanIntensity > threshold
outputMask(rStart:rEnd, cStart:cEnd) = 1;
end
end
end