You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to find the inner distance between the 2 white lines
2 views (last 30 days)
Show older comments
Hi,someone can give me a hand to make a script related to the problem?
Practically ,I want to get the distance column by column from the lowest white border of the upper part to the highest white border of the lower part.
I thank you in advance.

1 Comment
Image Analyst
on 17 Jan 2021
There are several ways to solve this. Do you want a vector of gaps (one gap width for every column), OR the average height (which actually doesn't need you to determine the gap on a column-by-column basis believe it or not).
But make it easy for us to help you, not hard. Attach the actual image either in a PNG file or a .mat file. We don't want the screenshot with tick labels, tick marks, surrounding gray background, etc. If you give all all that junk, it makes it hard for us because we need to take several steps to get rid of them and get down to the actual image.
Accepted Answer
Matt Gaidica
on 17 Jan 2021
Edited: Matt Gaidica
on 17 Jan 2021
The only thing you don't state is what to do in the case around x=400 where white pixels do not demarcate the "top". One way to make the algorithm below work is to modify the array so that there is always white on top and bottom, as I do with C.
B = imbinarize(im2gray(imread('borderProblem.png')));
% force white on top and bottom
C = [ones(1,size(B,2));B;ones(1,size(B,2))];
maxDist = zeros(1,size(B,2));
for iCol = 1:size(B,2)
d = diff(C(:,iCol));
edgeIdxs = find(d ~= 0);
maxDist(iCol) = max(diff(edgeIdxs));
end
close all
figure;
subplot(211);
imshow(C);
subplot(212);
plot(maxDist,'k');
xlim(size(maxDist));
ylim([1,size(B,1)]);
xlabel('col');
ylabel('dist');

19 Comments
Francesco Muoio
on 19 Jan 2021
Hi Matt ,thank you for the solution.
I have two more things to ask:
-Is the distance obtained in pixels? if yes, how can I convert it to millimeters
-I would need a code that loads a series of jpg photos (such as the binarized one) from a windows folder, which performs the code you provided me and then saves these figures to me in another folder (perhaps using the imwrite command to save the file)
Thanks for your help.
Matt Gaidica
on 19 Jan 2021
Edited: Matt Gaidica
on 19 Jan 2021
Yes, in pixels. You would need some type of ruler or ground truth measurement in the frame of the image for the conversion: do you know the image dimensions in millimeters?
It would be something like this:
% code example for conversion
knownPx = 10;
knownMm = 100;
pixelAmt = 10;
px2mm = pixelAmt / (knownPx / knownMm);
% to read/write images
readPath = "/to/your/images";
savePath = "/save/here"
filelist = dir(fullfile(readPath,"*.jpg"));
for iFile = 1:numel(filelist)
thisFile = fullfile(readPath,filelist(iFile).name);
B = imbinarize(im2gray(imread(thisFile)));
% do more stuff
imwrite(B,fullfile(savePath,filelist(iFile).name));
end
Francesco Muoio
on 22 Jan 2021
Hi Matt, I tried to write this code that works well enough only that in the end it fails to save the images shown on the screen(I have attached the file_2.m).
The code asks to load the input images(these are attached) taken from an input folder ,calculates the distances and then has to save the output images (shown on screen) in another output folder.
P.S. the images are all in jpg format.
Thanks again for the help!!!
Matt Gaidica
on 22 Jan 2021
Look at this chunk of code.
- You were opening a figure, then closing all, which made h go away. (You don't necessarily need close all if you close(h) explicity, which I added below).
- Use saveas when saving figures and the figure handle.
close all
h=figure;
s(1)=subplot(211);
imshow(C);
s(2)=subplot(212);
plot(maxDist,'r','LineWidth',2);
xlim(size(maxDist));
ylim([1,size(B,1)]);
xlabel('columns','FontSize',20,'FontWeight','bold','Color','k');
ylabel('distances','FontSize',20,'FontWeight','bold','Color','k');
grid on
grid minor
box off
%and write it to file
image_file_1_full_name_to_save2=fullfile(folder_name_to_save2,image_files(i).name);
saveas(h,image_file_1_full_name_to_save2)
close(h);
% imwrite(C,image_file_1_full_name_to_save2)
msg=sprintf('%d out of %d files were processed', i, N);
waitbar(i/N,h_waitbar,msg)
if i==N,pause(0.5),end
Francesco Muoio
on 22 Jan 2021
Matt,you are phenomenal !!!
Thank you very much!
I should complete my last point of the thesis which consists in obtaining a single output graph representing the difference between n single output graphs.
Do you have any idea how to implement such code?
Sorry if i still bother you
Matt Gaidica
on 22 Jan 2021
Great work! I'm not sure I understand, could you draw out or depict what you want in your final figure?
Francesco Muoio
on 22 Jan 2021
Matt to be clearer, I have to write a code, even in the same script, which averages those graphs always in terms of column by column distances between those white lines (the single output graph must be similar to that of those single graphs)
Matt Gaidica
on 22 Jan 2021
You'll just want to save that plot data in a matrix over your looping function, then create your final figure at the end:
allData = [];
% prototype of your current function
for ii = 1:n
% do computation and make figure
allData(ii,:) = lineData; % save data
end
% plot all data
figure;
plot(mean(allData));
Francesco Muoio
on 23 Jan 2021
Hi Matt, I tried to implement your piece of code in my script but it gives me error: Undefined function or variable 'lineData'.
You could kindly try to implement it in the file_2.m?
Matt Gaidica
on 24 Jan 2021
Edited: Matt Gaidica
on 24 Jan 2021
Francesco, the way this forum works is that we help you with snippets of your code. If you want help, please show what code you tried using the code block formatter.
Francesco Muoio
on 24 Jan 2021
Edited: Francesco Muoio
on 24 Jan 2021
Matt di you have any idea how to write new code that realizes what I said?
Matt Gaidica
on 25 Jan 2021
You have a loop:
for i=1:N
Then you are plotting a variable:
plot(maxDist,'r','LineWidth',2);
So if you want to 'compile' all your maxDist data, initialize an array above the for loop
all_maxDist = [];
Then fill it inside of the loop after your plot
all_maxDist(i,:) = maxDist;
Then after your i loop, you can do
plot(mean(all_maxDist));
Francesco Muoio
on 25 Jan 2021
I understand now.
Matt, If I have about 200 images in the input folder, how can I get an average graph (plot mean),according to that cycle for,for every 10 iterations of images?
Matt Gaidica
on 26 Jan 2021
n = 10;
for i=1:N
if mod(i-1,n);
all_maxDist = [];
end
% do calculation here
% ...
if mod(i-1,n);
h = plot(mean(all_maxDist));
% save h
close(h);
end
end
Or just compile the array as I had it and create a loop afterwards:
n = 10;
for ii=1:n:N
h = plot(mean(all_maxDist(ii:ii+n-1)));
% save h
close(h);
end
Francesco Muoio
on 26 Jan 2021
Matt,sorry if I keep bothering you but I can't solve this last point of the thesis.
I implemented that code in the script and it eventually saves me in the output folder equal images.
Always considering the same four input images that I attach to you and making average plots(plot mean) every 2 images(for example, because in reality I have to do this operation every 10 images out of 200 input images), I find 2 identical graphs in the output folder.
Could you take a look at the script I am attaching to you?
I would like to understand where I'm wrong.
Also can you check if the for loop on the average plot has been written well?
I do not know how to thank you!!!
More Answers (1)
Matt Gaidica
on 27 Jan 2021
I'm not sure this is entirely correct, but you had things improperly placed.
6 Comments
Francesco Muoio
on 27 Jan 2021
Matt thank you for fixing the script but the problem remains that the average plotting function does not do its duty.
Thank you very much for your support anyway. I'll try to find the solution on this final part.
Francesco Muoio
on 28 Jan 2021
Edited: Francesco Muoio
on 28 Jan 2021
Matt,however I have slightly modified the script.
Always considering the 4 input images and starting the simulation, I would like to find at the end an average curve of all those output graphs.
You could take a look?
Thanks
Matt Gaidica
on 28 Jan 2021
This was explained above. Compile all the data in your loop (e.g., using all_maxDist) then compute the mean at the end.
Francesco Muoio
on 28 Jan 2021
Finally I get a graph with the various distance curves and the average graph. How can I extract the average chart and plot it on another chart? What command can I use?
Matt Gaidica
on 29 Jan 2021
If you compile all of it in your main loop using all_maxDist then you just create a new figure for that variable. I would seek out some of the MATLAB tutorials on matrices and loops, this is fundamental stuff. I'm going to unsubscribe here, I think you need to start a new thread based on very specific problems you're having.
See Also
Categories
Find more on Denoising and Compression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)