Can we measure yarn twist from image with image processing

Hi Everyone.
Can we measure number of yarn twist from picture.
For example in picture one yarn twist number with is shown with red arrow 7 or 8. We can see thick place is yarn twist. Can we write a code for this? Or it isnt possible?
<<
<<
>>
>>

 Accepted Answer

It's possible. First I'd call imopen() to get rid of the stray threads.
grayImage = imopen(grayImage, true(9));
binaryImage = grayImage > 128; % or whatever.
Then extract the biggest remaining blob using the attached function. Then I'd get the x,y coordinates of the edges of the main, bulk yarn.
boundaries = bwboundaries(binaryImage');
Be sure to split out the two different sides of the yarn. Then call fft() or pwelch() to get the dominant spatial frequency. Give it a try. If you need to come back, then attach the original image plus your attempt at coding up my suggestions.

11 Comments

If I use bwboundaries then it gives me a cell 3*1. How can I use fft or pwelch for boundaries or binaryImage?
The original image is below:
So I cant do it furthermore.
You forgot to attach your script.
ali
ali on 18 Jan 2015
Edited: ali on 18 Jan 2015
All of your suggest ı ımplement then. how can ı split and apply fft or pwelch?
It gives me cell or image? So fft or pwelch doesnt support this?
But ı dont know how can ı going on after boundaries = bwboundaries(binaryImage');
I can't split with code like that
L2(~binaryImage) = 0;
imshow(label2rgb(L2, 'jet', [.7 .7 .7], 'shuffle'))
Another code
D = bwdist(biggestBlob);
DL = watershed(D);
bgm = DL == 0;
binaryImage=binaryImage-bgm;
imshow(biggestBlob);
but it doesn't split the biggest one .
OK, I did some of it for you. But I really think you should do some of it yourself, don't you? Save the code below in test.m and run it.
function test()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\ali\Documents\Images';
baseFileName = '1-a.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 3, 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, 3, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image.
binaryImage = grayImage > 128;
subplot(2, 3, 3);
imshow(binaryImage)
title('Binary Image', 'FontSize', fontSize);
% Get rid of stray horizontal fibers.
binaryImage = medfilt2(binaryImage, [19, 1]);
%---------------------------------------------------------------------------
% Extract the largest area using our custom function ExtractNLargestBlobs().
% This is the meat of the demo!
numberToExtract = 1;
biggestBlob = ExtractNLargestBlobs(binaryImage, numberToExtract);
%---------------------------------------------------------------------------
subplot(2, 3, 4);
imshow(biggestBlob)
title('Largest Blob Extracted and Cleaned Up', 'FontSize', fontSize);
% Find width as a function of row
verticalProfile = sum(biggestBlob, 2);
subplot(2, 3, 5);
area(verticalProfile, 'FaceColor', 'b')
title('Yarn Width', 'FontSize', fontSize);
xlabel('Row or line number', 'FontSize', fontSize);
ylabel('Width', 'FontSize', fontSize);
msgbox('Done with demo!');
%==============================================================================================
% Function to return the specified number of largest or smallest blobs in a binary image.
% If numberToExtract > 0 it returns the numberToExtract largest blobs.
% If numberToExtract < 0 it returns the numberToExtract smallest blobs.
% Example: return a binary image with only the largest blob:
% binaryImage = ExtractNLargestBlobs(binaryImage, 1);
% Example: return a binary image with the 3 smallest blobs:
% binaryImage = ExtractNLargestBlobs(binaryImage, -3);
function binaryImage = ExtractNLargestBlobs(binaryImage, numberToExtract)
try
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
blobMeasurements = regionprops(labeledImage, 'area');
% Get all the areas
allAreas = [blobMeasurements.Area];
if numberToExtract > length(allAreas);
% Limit the number they can get to the number that are there/available.
numberToExtract = length(allAreas);
end
if numberToExtract > 0
% For positive numbers, sort in order of largest to smallest.
% Sort them.
[sortedAreas, sortIndexes] = sort(allAreas, 'descend');
elseif numberToExtract < 0
% For negative numbers, sort in order of smallest to largest.
% Sort them.
[sortedAreas, sortIndexes] = sort(allAreas, 'ascend');
% Need to negate numberToExtract so we can use it in sortIndexes later.
numberToExtract = -numberToExtract;
else
% numberToExtract = 0. Shouldn't happen. Return no blobs.
binaryImage = false(size(binaryImage));
return;
end
% Extract the "numberToExtract" largest blob(a)s using ismember().
biggestBlob = ismember(labeledImage, sortIndexes(1:numberToExtract));
% Convert from integer labeled image into binary (logical) image.
binaryImage = biggestBlob > 0;
catch ME
errorMessage = sprintf('Error in function ExtractNLargestBlobs().\n\nError Message:\n%s', ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
%
Thanks for attention
The last figure how can we figure out it?
It is verticalprofile or not?
If we want to do this with fft, can we use fft or pwelch function instead of it.
Yes, it is the vertical profile and gives the widths as you go down line by line. Try to imagine the graph turned 90 degrees for better correspondence to the image.
If you want to look for the dominant frequency you can take the fft and look for a spike at some frequency.
Alternatively you can use findpeaks() and try to find a peak or valley spacing that is periodic in the spatial domain.
Why are you doing this project?
Thanks for attention.
just try to find twist with processing instead of eye. But some of the obtained results not good. It depends on yarn's color or something else. I ll try different codes for it.
No, I meant, like is it for your senior project, or your Masters thesis, or Ph.D. dissertation, or has your company or university been contracted to develop a turnkey system by a textile manufacturer?
ali
ali on 20 Jan 2015
Edited: ali on 20 Jan 2015
yeah something like that. ıf we will obtain good results maybe we use that for article but some of the yarns image so bad and threshold value so versatile.
Maybe we have to use double thresholding for images?

Sign in to comment.

More Answers (0)

Asked:

ali
on 18 Jan 2015

Edited:

ali
on 20 Jan 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!