One approach would be:
- First, you need to segment the scale bar using any segmentation function, for example imbinarize (using a small threshold)
- Then, using regionprops (with the inverted mask, ~BW)you can obtain the length of the segmented objects using the property 'MajorAxisLength'. If the threshold is good enough, the object with the maximum length should be your scale bar.