Skip to content

Instantly share code, notes, and snippets.

@saikatbsk
Last active August 11, 2018 18:01
Show Gist options
  • Save saikatbsk/dc7dc990e1e4bc9aea392cea350d819b to your computer and use it in GitHub Desktop.
Save saikatbsk/dc7dc990e1e4bc9aea392cea350d819b to your computer and use it in GitHub Desktop.
inputfile = "data/1.bmp";
imsrc = imread(inputfile); % Load an RGB image
imresized = imresize(imsrc, [28 NaN]); % Resize image
imbw = im2bw(imresized, "moments"); % Convert to binary
% Get the positions of the black pixels
x = []; y = [];
for r = 1:28
for c = 1:size(imbw, 2)
if imbw(r, c) == 0
x = [x; r];
y = [y; 28-c];
end
end
end
% plot(x, y, "rx", "MarkerSize", 8); % Plot the data
m = length(x); % Count how many data points we have
X = [ones(m, 1) x]; % Add a column of all ones (intercept term) to x
theta = (pinv(X' * X)) * X' * y; % Calculate theta
% Plot the fitted equation we got from the regression
% hold on;
% plot(X(:, 2), X * theta, '-');
% hold off;
imfinal = imrotate (imsrc, -theta(2), "bicubic", "loose", 0);
[path, name, ext] = fileparts(inputfile);
outpath = fullfile(path, "out");
if ~exist(outpath, "dir")
mkdir(outpath);
end
outfile = fullfile(outpath, strcat(name, ext));
imwrite(imfinal, outfile)
fprintf("Output saved as: %s\n", outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment