diff --git a/Assignment_2/Image2Graph.m b/Assignment_2/Image2Graph.m new file mode 100644 index 0000000..5d81366 --- /dev/null +++ b/Assignment_2/Image2Graph.m @@ -0,0 +1,33 @@ +function myAffinityMat = Image2Graph (imIn) +%Image2Graph produces a graph, in the form of a matrix, from an image +% Usage myAffinityMat = Image2Graph (imIn), where: +% Inputs +% - imIn is the input image +% Output +% - myAffinityMat is the graph produced + + % Initializes helper variables + imageWidth = size(imIn, 2); + imageHeight = size(imIn, 1); + + % Initializes output matrix +% myAffinityMat = zeros(imageWidth * imageHeight, imageWidth * ... +% imageHeight); + +% for row = 1:imageHeight +% for column = 1:imageWidth +% % Calculates distance matrix for the current element +% diff = imIn - imIn(row, column, :); +% dist = sum(diff .^ 2, 3) .^ 0.5; +% +% % Calculates affinity matrix +% myAffinityMat((row - 1) * imageWidth + column, :) = ... +% reshape((1 ./ exp(dist)).', 1, []); +% end +% end + + som = reshape(imIn, 1, [], 3); + diff = repmat(som, imageWidth * imageHeight, 1, 1) - permute(som, [2 1 3]); + dist = sum(diff .^ 2, 3) .^ 0.5; + myAffinityMat = 1 ./ exp(dist); +end diff --git a/Assignment_2/Image2Graph.m~ b/Assignment_2/Image2Graph.m~ new file mode 100644 index 0000000..aed0513 --- /dev/null +++ b/Assignment_2/Image2Graph.m~ @@ -0,0 +1,36 @@ +function myAffinityMat = Image2Graph (imIn) +%Image2Graph produces a graph, in the form of a matrix, from an image +% Usage myAffinityMat = Image2Graph (imIn), where: +% Inputs +% - imIn is the input image +% Output +% - myAffinityMat is the graph produced + + imageWidth = size(imIn, 2); + imageHeight = size(imIn, 1); + imageChannels = size(imIn, 3); + + %myAffinityMat = 1 / exp(norm(imIn(:, :))); + myAffinityMat = zeros(imageWidth * imageHeight, imageWidth * imageHeight); + + for row = 1:imageHeight + for column = 1:imageWidth + diff = imIn - imIn(row, column, :); + %diffT = permute(diff, [2 1 3]); + + dist = sum(diff .^ 2, 3) .^ 0.5; + +% dist = zeros(imageWidth, imageHeight); +% for channel = 1:imageChannels +% dist = dist + sqrt(sum (diff(:, :, channel) * diffT(:, :, channel))); +% dist = dist + sqrt(diff(:, :, channel) * diffT(:, :, channel)); +% end + + som = 1 ./ exp(dist); + som2 = reshape(som, 1, []); + som2 = reshape(som.', 1, []); + + myAffinityMat((row - 1) * imageWidth + column, :) = reshape((1 ./ exp(dist), 1, []); + end + end +end diff --git a/Assignment_2/README.md b/Assignment_2/README.md new file mode 100644 index 0000000..1f12eeb --- /dev/null +++ b/Assignment_2/README.md @@ -0,0 +1,7 @@ +# Second deliverable + +In the second deliverable we were tasked to implement a number of functions that: +- produce a graph representation of an image +- do image segmentation using spectral clustering +- do image segmentation using the normalized cuts (N-cuts) method, in both an imperative and recursive way +- execute a series of experiments to test the results of our code \ No newline at end of file diff --git a/Assignment_2/aff_d2a.png b/Assignment_2/aff_d2a.png new file mode 100644 index 0000000..6872028 Binary files /dev/null and b/Assignment_2/aff_d2a.png differ diff --git a/Assignment_2/dip_hw_2.mat b/Assignment_2/dip_hw_2.mat new file mode 100644 index 0000000..8883aba Binary files /dev/null and b/Assignment_2/dip_hw_2.mat differ