Detect black pixel horizontal edge and measure area under it.

I am trying to write a script to automatically read the water level off a gauge from time lapse images. I have got the following script to:- 1) load image 2) rotate so gauge is horizontal 3) crop so only gauge is left in image 4) threshold it so it is black and white (gauge numbers and markings black, water black and gauge background is white)
The result of this code is the image below.
"I = imread('C:\Timelapse\test files\IMAG0471.jpg'); figure('name','Original'), imshow(I)
Ir = imrotate(I,10); figure('name','rotated'), imshow (Ir)
cropped = imcrop (Ir, [2325 450 45 800]); figure('name','cropped'), imshow (cropped)
thresh = 50; gauge = im2bw (cropped,thresh/255); figure('name','Thresh'),imshow(gauge)"
I have spent a long time trying to work out how to get matlab to detect the horizontal extent of the large black area. Once I have this value it should be quite straight forward I imagine to relate the area/size/length of black to different depths.
I would really appreciate any help and thank you very much for your time.

 Akzeptierte Antwort

You forgot to attach your original image. And that looks like it's vertical to me, not horizontal. But one way to find the height of the big black portion at the bottom is to sum the image horizontally and look for where it's half way.
verticalProfile = mean(binaryImage, 2); % Get mean horizontally.
indexOfWaterLevel = find(mean > 0.5, 1, 'last');
Then you need some kind of spatial calibration that tells you what index corresponds to what water depth.

2 Kommentare

Thank you very much for your response Image Analyst. The gauge is vertical you are correct, I think I meant to write that the water and scale is horizontal...
The code appears to work well and extracts a value which can be calibrated in the future.
I am now struggling to get the script to:-
1) Run the same code for each .jpg file in a folder. I have tried this approach and was wondering if there might be a better way or if there are errors in this code?
"files = dir('*.JPG');
for k = 1:numel(files)
I = imread(files(k).name);
ETC
end"
2) Output the indexOfWaterLevel value, file name, file date and file time for each image to an excel or text file.
fileID = fopen('C:\Timelapse\Timelapse NF\depth2.txt', 'w');
fprintf(fileID, '%d\r\n', indexOfWaterLevel);
This outputs the indexOfWaterLevel value but each time the script is run it overwrites the old value and I haven't been able to get the file name, date or time to be recorded with files.name/date command.
Thank you very much for your time and already helping me with the code to measure the water depth.
2. Use the "append" mode of fopen so it doesn't destroy the existing file.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David
David am 14 Feb. 2014

0 Stimmen

Thank you, all is working well with the code after a few more additional tweaks and changes :)

Kategorien

Gefragt:

am 27 Jan. 2014

Beantwortet:

am 14 Feb. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by