Harry Spratt
Harry Spratt

Reputation: 305

How to detect black strip at bottom of photo

In my experiment I am vibrating a liquid droplet on a platform. I am recording this as a video and splitting it into frames. I am preforming analysis on each image to detect the max height. I have encountered and don't know how to fix it. To avoid missing the vibration I started recording prior to the platform being elevated. However this means that the starting analysis will be wrong. Attached are three photos, they how the droplet before the platform is raised, during and after. How do I create code so that the analysis starts from the last image?.

enter image description here

enter image description here

enter image description here

As you can see a black strip (the platform) emerges when its high enough and from this point I would like to start analysing the images. Any idea how to do this?

Upvotes: 1

Views: 110

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 208033

You could look at the mean of the last few rows:

import cv2
import numpy as np

# First look at top image
im = cv2.imread('im1.jpg', cv2.IMREAD_GRAYSCALE)

In [4]: np.mean(im, axis=1)
Out[4]: 
array([247.975    , 247.43125  , 247.7265625, 247.4      , 247.6734375,
       247.215625 , 247.8046875, 247.3515625, 247.840625 , 247.30625  ,
       247.7359375, 247.2234375, 247.909375 , 247.5125   , 247.675    ,
       247.1359375, 247.6296875, 247.209375 , 247.6734375, 246.971875 ,
       247.225    , 246.2      , 247.1890625, 246.7765625, 247.3453125,
       246.5234375, 247.021875 , 246.3984375, 247.1640625, 246.534375 ,
       247.128125 , 246.5578125, 247.103125 , 246.24375  , 246.7765625,
       246.215625 , 246.6296875, 246.0125   , 246.61875  , 245.8796875,
       246.2921875, 245.36875  , 246.14375  , 245.325    , 246.115625 ,
       245.2296875, 245.8515625, 244.7640625, 245.4890625, 244.7859375,
       245.2859375, 244.584375 , 245.503125 , 244.721875 , 245.2671875,
       244.253125 , 244.9625   , 244.271875 , 245.0296875, 244.296875 ,
       244.8609375, 244.0125   , 244.7265625, 244.028125 , 244.6828125,
       243.740625 , 244.44375  , 243.634375 , 244.265625 , 243.459375 ,
       244.18125  , 243.634375 , 244.078125 , 242.8640625, 243.884375 ,
       242.853125 , 243.70625  , 242.734375 , 243.6734375, 242.703125 ,
       243.1078125, 242.0828125, 242.8734375, 242.0203125, 242.796875 ,
       241.7375   , 242.5453125, 241.6734375, 242.4765625, 241.359375 ,
       241.8078125, 240.9734375, 241.68125  , 240.68125  , 241.5765625,
       240.51875  , 241.290625 , 239.8859375, 241.1515625, 240.303125 ,
       240.6671875, 239.471875 , 240.6328125, 239.590625 , 240.425    ,
       239.2234375, 240.1359375, 238.6109375, 239.83125  , 238.8015625,
       239.4890625, 238.109375 , 239.0203125, 237.7515625, 239.03125  ,
       237.54375  , 238.8890625, 237.0203125, 238.121875 , 236.7265625,
       238.021875 , 236.7765625, 237.4734375, 236.1390625, 237.00625  ,
       235.4421875, 236.503125 , 235.16875  , 236.225    , 234.5078125,
       235.653125 , 234.2296875, 235.2703125, 233.8703125, 234.7765625,
       233.2578125, 234.3015625, 232.8125   , 233.65625  , 232.5765625])

# Now middle image
im = cv2.imread('im2.jpg', cv2.IMREAD_GRAYSCALE)

In [6]: np.mean(im, axis=1)
Out[6]: 
array([248.4484375, 247.9515625, 248.3890625, 247.740625 , 248.1921875,
       247.784375 , 248.2546875, 247.8      , 248.4859375, 248.1625   ,
       248.346875 , 248.0359375, 248.459375 , 248.0578125, 248.3703125,
       247.6703125, 248.2484375, 247.7390625, 248.2421875, 247.625    ,
       247.8671875, 246.8984375, 247.9203125, 247.4515625, 247.96875  ,
       247.4421875, 247.759375 , 247.35625  , 247.6328125, 247.0765625,
       247.640625 , 247.03125  , 247.471875 , 246.8921875, 247.425    ,
       246.8640625, 247.428125 , 246.6671875, 246.9984375, 246.36875  ,
       246.9984375, 246.1828125, 246.840625 , 245.9703125, 246.6703125,
       245.76875  , 246.475    , 245.63125  , 246.2546875, 245.38125  ,
       245.925    , 245.2734375, 246.0703125, 245.3453125, 245.8375   ,
       245.003125 , 245.6484375, 244.921875 , 245.721875 , 245.0171875,
       245.4640625, 244.8609375, 245.346875 , 244.4671875, 245.2953125,
       244.1890625, 245.0828125, 244.4078125, 245.1140625, 244.3296875,
       245.0859375, 244.140625 , 244.8703125, 244.0625   , 244.7203125,
       243.7546875, 244.0890625, 243.390625 , 244.2859375, 243.528125 ,
       244.146875 , 243.1578125, 243.63125  , 242.7078125, 243.54375  ,
       242.50625  , 243.1765625, 242.0296875, 242.85     , 241.7484375,
       242.83125  , 241.4703125, 242.359375 , 241.28125  , 241.5578125,
       239.6859375, 239.41875  , 236.5140625, 235.7140625, 232.715625 ,
       231.453125 , 228.85625  , 228.2890625, 225.70625  , 225.578125 ,
       223.128125 , 223.28125  , 221.3203125, 221.275    , 219.3453125,
       219.640625 , 217.990625 , 218.0625   , 216.3671875, 216.7203125,
       214.6125   , 215.578125 , 214.1921875, 214.5140625, 212.746875 ,
       213.4328125, 211.5109375, 212.0515625, 210.2828125, 211.1859375,
       209.55625  , 210.04375  , 208.24375  , 208.8765625, 206.9515625,
       207.8078125, 205.946875 , 206.3328125, 204.490625 , 205.3      ,
       203.3125   , 203.965625 , 202.3953125, 202.3453125, 201.309375 ])

# Now bottom image
In [7]: im = cv2.imread('im3.jpg', cv2.IMREAD_GRAYSCALE)

In [8]: np.mean(im, axis=1)
Out[8]: 
array([248.203125 , 247.6140625, 248.059375 , 247.2109375, 247.7140625,
       247.165625 , 247.9      , 247.4703125, 247.9765625, 247.4140625,
       247.8140625, 247.3265625, 247.978125 , 247.4875   , 247.8921875,
       247.2265625, 247.85     , 247.384375 , 247.815625 , 247.034375 ,
       247.5046875, 247.021875 , 247.471875 , 247.3234375, 247.55625  ,
       246.784375 , 247.315625 , 246.5890625, 247.215625 , 246.6171875,
       247.0921875, 246.2375   , 246.8484375, 246.2421875, 246.775    ,
       246.1015625, 246.5640625, 245.9515625, 246.359375 , 245.7875   ,
       246.221875 , 245.5765625, 246.125    , 245.44375  , 246.003125 ,
       245.2921875, 245.78125  , 244.875    , 245.53125  , 244.809375 ,
       245.353125 , 244.6875   , 245.2078125, 244.590625 , 245.1921875,
       244.6359375, 245.115625 , 244.4421875, 244.909375 , 244.190625 ,
       245.00625  , 244.21875  , 244.80625  , 243.8765625, 244.48125  ,
       243.6921875, 244.3265625, 243.328125 , 244.221875 , 243.328125 ,
       244.3125   , 243.2703125, 244.0359375, 243.1765625, 243.8828125,
       242.915625 , 243.578125 , 242.7171875, 243.484375 , 242.4375   ,
       243.3546875, 242.1515625, 242.9203125, 241.709375 , 242.7453125,
       241.6140625, 242.2765625, 241.053125 , 241.4171875, 238.809375 ,
       238.3046875, 235.1765625, 233.91875  , 230.5703125, 229.5609375,
       226.9453125, 226.5015625, 224.       , 223.8453125, 222.1015625,
       221.4171875, 219.3171875, 219.7140625, 217.846875 , 218.2203125,
       216.3890625, 217.015625 , 215.3625   , 215.6015625, 214.0890625,
       214.909375 , 213.403125 , 213.903125 , 212.0515625, 212.9796875,
       211.321875 , 212.3      , 210.8703125, 211.7203125, 209.890625 ,
       210.671875 , 209.10625  , 209.9515625, 208.38125  , 209.23125  ,
       207.7328125, 208.48125  , 206.71875  , 207.465625 , 205.721875 ,
       206.4921875, 204.6359375, 205.0859375, 201.6921875, 199.509375 ,
       184.6015625, 164.1765625, 129.8203125, 101.5859375,  92.6609375]) <--- LAST ROWS ARE DARKER

Upvotes: 2

Lior Cohen
Lior Cohen

Reputation: 5755

You did not mention what are your accuracy requirements and what are the edge-case frames you need to deal with, but according to your examples this should be simple:

  1. turn the image into binary by thresholding it to the the expected value between your black and bright gray-levels.
  2. Morphologically close the tiny black "holes" you might be left with.
  3. On the last row find where is the first 0 and last 0 column and subtract.

Upvotes: 0

Related Questions