Permalink
Browse files

added READMEs

  • Loading branch information...
Jennifer Guo
Jennifer Guo committed Jan 14, 2014
1 parent 835baa4 commit 1ff2477e6528144515469b34b6dd8a89d831bfdb
@@ -1 +1,9 @@
README
+
+Detect Square works by running adaptivethreshold(), findsquare() and rectify()
+
+adaptivethreshold takes an image taken under bad lighting conditions and turns it into a black white image. The threshold is set adaptively based on the current local window condition. This way we can get a uniform black white picture even if the lightning is different.
+
+findsquare works by detecting the largest boundingbox in the image. We assume that that box is the sudoku square. This will largely work but lead to incorrect results when there is a larger square, for example in 9.jpg. There is also some preprocessing done.
+
+rectify works by finding the 4 corners of the sudoku square and then transforming them to a rectangle. I’m finding the corners by moving the corner inward until I hit black pixels. After transforming I’m also cropping the image based on the original sudoku square size and the position of the corner points.
@@ -55,10 +55,6 @@
% Write the computed images
imwrite(output_image, output_filename);
- else
-
- % Read the previously computed output image
-% output_image = im2double(imread(output_filename));
end
@@ -1 +1,7 @@
-README
+Extract digits works by scanning through the sudoku square with a window approximately the size of one digit field. The main challenge lies in removing the border lines of the window. We achieve a clean extracted digit in 2 steps.
+
+First we shrink the window to an inner rectangle by looking for the largest bounding box inside the window and crop the window down to that size. The function for that is get_rectangle().
+
+We have a second step to clean up any possible remaining black border pixels. shrink_rectangle() accomplishes this by shrinking all 4 edges of the rectangle until the middle 1/3 strip doesn’t contain any black pixels. (Most of the time this works, unless the grid line goes through the middle of the image without touching the border.)
+
+Then we need to determine if the rectangle contains a digit inside. We achieve this by calculating the ratio of black pixels to the whole rectangle area size. For higher accuracy, we divide the window into a 3 x 3 grid and we weigh the center region of the window more.
@@ -1,6 +1,7 @@
-function [ output_struct ] = extractdigits_new( sudoku_image )
+function [ output_struct ] = extractdigits_new( sudoku_image, filename )
%Takes a clean sudoku image and extract the digit portion
% input sudoku_image should be a binary image!
+% filename is the original image's filename without extension.
% The output is a struct with the digit image vector and the
% location vector
@@ -15,8 +16,9 @@
digit_images = {};
digit_location = [];
-% imshow(sudoku_image);
-% hold on;
+ fig = figure('Visible', 'Off');
+ imshow(sudoku_image);
+ hold on;
location = 1; % value from 1~81 to mark which square in sudoku box
index = 1; % value from 1 ~ #of squares with digits.
@@ -47,12 +49,17 @@
% Testing: draw shrinked rectangle in original image
r2 = [r1(1,1) + shrinked_rect(1,1) - 1, r1(1,2) + shrinked_rect(1,2) - 1, ...
shrinked_rect(1,3), shrinked_rect(1,4)];
- %rectangle('position', r2, 'edgecolor', 'g', 'linewidth', 2);
+ rectangle('position', r2, 'edgecolor', 'g', 'linewidth', 2);
end
location = location + 1;
end
end
+
+ set(gca,'position',[0 0 1 1],'units','normalized');
+ output_filename = strcat('../../output/extracted_digits/digits_', filename, '.jpg');
+ print(fig, '-djpeg', '-r300', output_filename);
+
%Construct the struct for return
output_struct.digit_vector = digit_images;
@@ -69,7 +76,7 @@
% used for imcrop then. Positions are relative to input "window"
stat = regionprops(window,'boundingbox');
maxarea = 0;
- maxsquare = 0;
+ maxsquare = [1 1 size(window, 2) size(window, 1)];
for cnt = 1 : numel(stat)
bb = stat(cnt).BoundingBox;
area = bb(3) * bb(4);
@@ -79,6 +86,14 @@
maxsquare = bb;
end
end
+
+ maxsquare = floor(maxsquare);
+ if (maxsquare(1,1) == 0)
+ maxsquare(1,1) = 1;
+ end
+ if (maxsquare(1,2) == 0)
+ maxsquare(1,2) = 1;
+ end
output_rectangle = maxsquare;
end
@@ -101,7 +116,7 @@
% moving top edge down
y_pos = 1;
while (1)
- hline = img(y_pos, h_length:2*h_length-1);
+ hline = img(y_pos, h_length:2*h_length-1); % horizontal line
if (sum(hline) == h_length) % all white
break;
end
@@ -160,7 +175,7 @@
THRESHOLD_RATIO = 0.1;
[h, w] = size(square);
- square = ~square; % let black pixels have value 1
+ square = ~square; % let black pixels have value 1 for summing up
% create a mask by dividing square into 9 regions and have the center
% region valued at 3 and the remaining at 1.

0 comments on commit 1ff2477

Please sign in to comment.