MATLAB: Issues with bwskel skeleton

bwskelImage Processing ToolboxMATLAB

Hello,
I wanted to use bwskel to get a line down this image of a printed line. However, I am getting small line chunks of the outline instead. What am I doing wrong? The code I used is below and I attached the image.
img= imread('C:\Users\aluce\Documents\random lines\25g 1 10 PSI 5 MMS.jpg'); %put in image directly from FLIR camer
imgray = rgb2gray(img); % turn color image to greyscale
[imgray, rect] = imcrop(imgray); %opens up image, drag crop box, double click to select, rect coorinates use for crop
%%
BW = im2bw(imgray,0.8); % convert to binary image
%imshow(BW)
out = bwskel(BW);
imshow(out)

Best Answer

Andrew:
After you binarized the image, there were some small holes in it that gave loops in the skeleton. I took the largest blob (to get rid of a noise speck) and then filled the holes in the remaining blob. Corrected code:
img= imread('25g 1 10 PSI 5 MMS.jpg'); %put in image directly from FLIR camer
imgray = rgb2gray(img); % turn color image to greyscale
subplot(2, 2, 1);
imshow(imgray, []);
% [imgray, rect] = imcrop(imgray); %opens up image, drag crop box, double click to select, rect coorinates use for crop
BW = im2bw(imgray, 0.8); % convert to binary image
% Take largest blob only.
BW = bwareafilt(BW, 1);
% Fill holes.
BW = imfill(BW, 'holes');
subplot(2, 2, 2);
imshow(BW, []);
%imshow(BW)
minBranchLength = round(sum(BW(:)) / 2)
skelImage = bwskel(BW, 'MinBranchLength', minBranchLength);
subplot(2, 2, 3);
imshow(skelImage)
g = gcf;
g.WindowState = 'maximized'