0% found this document useful (0 votes)
15 views12 pages

Final Project Report

The project report details the development of a MATLAB algorithm for solving a puzzle by reconstructing an original image from its pieces using image processing techniques. The methodology includes noise removal, cropping, and correlation-based matching to optimally align the pieces, resulting in a successfully reconstructed image. Team members contributed to various aspects of the project, including image pre-processing, piece cropping, and code implementation.

Uploaded by

Vessah Abdganyr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Final Project Report

The project report details the development of a MATLAB algorithm for solving a puzzle by reconstructing an original image from its pieces using image processing techniques. The methodology includes noise removal, cropping, and correlation-based matching to optimally align the pieces, resulting in a successfully reconstructed image. Team members contributed to various aspects of the project, including image pre-processing, piece cropping, and code implementation.

Uploaded by

Vessah Abdganyr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ISLAMIC UNIVERSITY OF TECHNOLOGY

Department of EEE

Digital Signal Processing Lab

EEE 4702

PROJECT REPORT:

PROJECT TITLE: Solving Puzzle using Matlab


Group Number:

Team Members:

Shaheed Rahman: 200021336

Tanzim Noor Tanmoy: 200021338

Mahee Khan: 200021342

M Sadman Aster: 200021344

Vessah Gbetnkom Njimboket Abdganyr: 200021364

Problem Statement:
Solving a puzzle using MATLAB requires accurately determining the correct spatial arrangement of puzzle
pieces to reconstruct the original image. The primary challenge is to develop an algorithm that can correctly
identify and align the puzzle pieces using image processing techniques.

Solution Abstract:
The project involves multiple steps to achieve an optimal solution. First, the original image is converted to
gray scale and denoised. Then, puzzle pieces are identified, cropped, and arranged optimally. Using correlation-
based matching techniques, the pieces are assembled back into the original image. This approach ensures
efficient puzzle solving using MATLAB's image processing capabilities.

Detailed Methodology:
Step 1: Noise Removal and Conversion of the Original Image

• The original image is converted to gray scale.


• Noise is removed using a median filter.

• This prepares the image for further processing.

Step 2: Evaluation of Similarities and Optimal Placement of Pieces

• The gray scale image is divided into 16 pieces (4×4 grid).

• Puzzle pieces are cropped to remove excess white space.

• Cropped pieces are arranged in a 4×4 grid for comparison.

Step 3: Reconstruction of the Full Image in Gray scale

• Puzzle pieces are matched to their corresponding positions using correlation-based techniques.

• The best-matching pieces are stored and arranged into the final image grid.

Step 4: Final Reconstituted Image

• The best-matched pieces are assembled to construct the complete image.

Code:

MATLAB Code:

% Load an image directly from the working


directory tic
fileName = 'noisy_colorful_image.jpg'; % Replace 'example.jpg' with your
image file name
inputImage = imread(fileName);

% Display the original


image figure;
imshow(inputImage);
title('Original Image');

% Convert to grayscale if the image is


RGB if size(inputImage, 3) == 3
grayImage = rgb2gray(inputImage);
else
grayImage = inputImage;
end

% Denoising using a median filter


denoisedImage = medfilt2(grayImage, [3
3]);

% Display the denoised


image figure;
imshow(denoisedImage);
title('Denoised Image');

% Load the image


fileName = '10.jpg'; % Replace with your image file
name inputImage = imread(fileName);

% Convert the image to grayscale if it's RGB


grayImage = rgb2gray(inputImage);

% Threshold the grayscale image to create a binary mask


binaryMask = grayImage < 220; % Assuming whitespace is pure white (255)

% Find the bounding box of the non-white


regions [row, col] = find(binaryMask);
rowMin =
min(row); rowMax
= max(row);
colMin =
min(col); colMax
= max(col);

% Crop the original image using the bounding box


croppedImage = inputImage(rowMin:rowMax,
colMin:colMax, :);

% Display the original and cropped


images figure;
imshow(inputImage);
title('Original Image');

figure;
imshow(croppedImage);

% Loop through images from 1.jpg to 16.jpg


for i = 1:16
% Generate the file name for the input
image inputFileName = sprintf('%d.jpg', i);

% Load the image


inputImage = imread(inputFileName);

% Convert the image to grayscale if it's


RGB grayImage = rgb2gray(inputImage);

% Threshold the grayscale image to create a binary


mask binaryMask = grayImage < 220; % Adjust threshold
if needed

% Find the bounding box of the non-white


regions [row, col] = find(binaryMask);
rowMin =
min(row); rowMax
= max(row);
colMin =
min(col); colMax
= max(col);

% Crop the original image using the bounding box


croppedImage = inputImage(rowMin:rowMax,
colMin:colMax, :);

% Generate the output file name


outputFileName = sprintf('%d_cropped.jpg', i);

% Save the cropped image


imwrite(croppedImage,
outputFileName);

% Display progress

% Load the image


inputImage =
denoisedImage

% Get the size of the image


[height, width, channels] = size(inputImage);

% Define the grid size


(4x4) rows = 4;
cols = 4;
cellHeight = floor(height / rows);
cellWidth = floor(width / cols);

% Loop through the grid to extract each sub-


image for i = 1:rows
for j = 1:cols
% Calculate the boundaries of the current grid
cell rowStart = (i-1) * cellHeight + 1;
rowEnd = min(i * cellHeight, height); % Ensure no out-of-bounds
colStart = (j-1) * cellWidth + 1;
colEnd = min(j * cellWidth, width); % Ensure no out-of-bounds

% Extract the sub-image


subImage = inputImage(rowStart:rowEnd, colStart:colEnd, :);

% Save the sub-image with a unique name


outputFileName = sprintf('subImage_%d_%d.jpg',
i, j); imwrite(subImage, outputFileName);

% Optional: Display the sub-image (for verification)


% figure; imshow(subImage); title(sprintf('Sub-image %d,%d', i, j));
end
end

disp('Image successfully divided into a 4x4 grid!');

% Parameters
rows = 4; % Grid rows
cols = 4; % Grid
columns
numImages = rows * cols; % Total number of segments (16)

% Load the grid segments (1st set)


gridImages = cell(rows, cols); % Store the grid
images for i = 1:rows
for j = 1:cols
fileName = sprintf('subImage_%d_%d.jpg', i, j); % File names from
the grid if isfile(fileName)
gridImages{i, j} =
imread(fileName); else
error('File %s not found. Ensure all 16 grid images are
available.',
fileName);
end
end
end
% Load the second set of 16 images
secondSetImages = cell(1, numImages); % Store second set of
fileName = sprintf('%d_cropped.jpg', k); % File names for the
second set if isfile(fileName)
secondSetImages{k} =
imread(fileName); else
error('File %s not found. Ensure all 16 second set images are
available. ', fileName);
end
end

% Initialize reconstructed grid


reconstructedGrid = cell(rows, cols);

% Compare and find the best match for each grid


position for i = 1:rows
for j = 1:cols
gridSegment = gridImages{i, j}; % Current grid segment
maxCorr = -Inf; % Initialize maximum correlation
bestMatchIndex = -1; % Initialize best match index

% Compare with all second set


images for k = 1:numImages
% Resize the second set image to match the grid segment size
resizedImage = imresize(secondSetImages{k}, [size(gridSegment,
1),
size(gridSegment, 2)]);

% Compute correlation (convert to grayscale for fair


comparison) corrValue = corr2(im2gray(gridSegment),
im2gray(resizedImage));

% Update the best


match if corrValue >
maxCorr
maxCorr = corrValue;
bestMatchIndex = k;
end
end

% Assign the best-matching image to the reconstructed grid


reconstructedGrid{i, j} =
imresize(secondSetImages{bestMatchIndex}, ...
en [size(gridSegment,1),size(gridSegment,2)]);
end d

% Combine the reconstructed grid into a single image


finalImage = zeros(size(gridImages{1, 1}, 1) * rows, size(gridImages{1, 1}, 2) * cols,
3, 'uint8');
for i =
1:rows
for j = 1:cols
rowStart = (i-1) * size(gridImages{1, 1}, 1) +
1; rowEnd = i * size(gridImages{1, 1}, 1);
colStart = (j-1) * size(gridImages{1, 1}, 2) +
1; colEnd = j * size(gridImages{1, 1}, 2);
finalImage(rowStart:rowEnd, colStart:colEnd, :) = reconstructedGrid{i,
j};
end
end

% Display the reconstructed image


figure;
imshow(finalImage);
title('Reconstructed Image from Second Set');

% Save the final image


imwrite(finalImage, 'reconstructed_image.jpg');
disp('Reconstructed image saved as
reconstructed_image.jpg.');

% The program section to time.

Result Analysis:

• The project successfully reconstructed the image from puzzle pieces.

• The correlation-based matching provided accurate results.

• Some minor errors in alignment occurred due to lighting and noise variations

Original Noisy and colorful image: Gray-scale denoised image:


(1) ( 2)

A piece of puzzle before (2) and after (1) the cropping and rotation operation :

All pieces of the puzzle after the cropping and rotation operation :
Reconstituted image: obtained by placing the puzzle pieces in the identified optimal positions

Final Reconstructed Image:


Original Image:

Conclusion:

This project successfully implemented an image-based puzzle-solving algorithm by applying various image
processing techniques. We did encounter some challenges such as differences in puzzle piece dimensions that
required resizing, also matching errors due to noise and lighting on the image. Future improvements could
involve advanced AI-based image recognition techniques for more precise matching.

Team Contribution:

Each member contributed to different aspects of the project:

• Shaheed Rahman: Image pre-processing and noise removal.

• Tanzim Noor Tanmoy: Puzzle piece cropping and rotation operations.

• Mahee Khan: Grid segmentation and correlation-based matching.

• M. Sadman Aster: Code implementation and debugging.

• Vessah Gbetnkom Njimboket Abdganyr: Final reconstruction and report documentation.


Referencing and Citations:

You might also like