I am doing a project that when I give 20×20 pixel images of handwritten digit program predict what digit it is. Program worka well but my teacher asked about a interface where user write his/her digits directly in to MATLAB and then press a button (or enter) so program predict what digit is it. At that point of the project I need to open a figure that has a 20×20 gray background and user will write one digit on it as we write in paint or photoshop with pencil tool then program takes that 20×20 image and process it. I couldn't figure out that part. All helps are appreciated
MATLAB: How to draw with mouse as we do in paint
figureguiimage processingImage Processing Toolbox
Related Solutions
It's just 3 lines. Just mask the original image:
binaryImage = grayImage > 150;maskedImage = zeros(size(grayImage)); % Initialize.
maskedImage(binaryImage) = grayImage(binaryImage);
See the full demo below:
% Demo by ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'coins.png';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
subplot(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') % Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);subplot(2, 2, 2); bar(pixelCount);grid on;title('Histogram of original image', 'FontSize', fontSize);xlim([0 grayLevels(end)]); % Scale x axis manually.
yl = ylim();line([150 150], [0 yl(2)], 'Color', 'r');% Threshold at 150
binaryImage = grayImage > 150;% Display the original gray scale image.subplot(2, 2, 3);imshow(binaryImage, []);title('Binary Image', 'FontSize', fontSize);% Mask the image
maskedImage = zeros(size(grayImage)); % Initialize.% Assign values from grayImage that are > 150.
maskedImage(binaryImage) = grayImage(binaryImage);% Display the original gray scale image.subplot(2, 2, 4);imshow(maskedImage, []);title('Masked Image', 'FontSize', fontSize);
With your data, my demo would become this:
% Create the original knot points.
xInitial = [1.6 1.1 .6 .1 .4 .9 1.4 1.9];yInitial = [1 1.5 2 2.5 3 3.5 4 4.5];lengthX = length(xInitial);% flip sideways so there is only 1 y for every x
x = yInitial;y = xInitial;% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);grid on;set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;newXSamplePoints = linspace(min(x), max(x), lengthX * samplingRateIncrease);smoothedY = spline(x, y, newXSamplePoints);% Now flip back
ySmooth = newXSamplePoints;xSmooth = smoothedY;% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');title('Spline Interpolation Demo', 'FontSize', 20);legend('Original Points', 'Spline Points');figure;plot(xInitial, yInitial, '-sr', 'LineWidth', 2);% Plot smoothedY and show how the line is% smooth, and has no sharp bends.hold on; % Don't destroy the first curve we plotted.plot(xSmooth, ySmooth, '-ob');grid on;
You can also use a Savitzky-Golay filter to smooth curves, and I attach a demo for that.
Best Answer