Detect distance between text lines

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
kaliber
Posts: 5
Joined: 2015-11-13T01:29:10-07:00
Authentication code: 1151

Detect distance between text lines

Post by kaliber »

Hello
Hey I was wondering if anyone could help me with this problem.

Basically I have to measure the space between text lines.

Example image: I have to calculate the y-coordinates of the (virtual) red rectangles (right image).
The background is always transparent; the text color is always black (left image).

Image

No matter if two lines character can collapse

For drawing up this text I use the `caption:` feature.

I know that `--debug annotate` can returns some glyph metrics, but this option cannot supply me the values to get the rid.

Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect distance between text lines

Post by fmw42 »

Please always provide your ImageMagick version and platform when asking questions on this forum.


You can average your image to one column and threshold. Then convert to txt: format and look for the lengths of the white regions

Input:
Image

Since your image has red in it, I have to first remove your red.

Code: Select all

convert lines.png -fuzz 50% -fill white -opaque red -auto-level -negate -threshold 20% -negate lines1.png
Image

Now, scale to 1 column and threshold to black and white

Code: Select all

convert lines1.png -scale 1x! -auto-level -negate -threshold 0 -negate lines2.png
Image

Now convert to txt: format and analyze the stretches of white, i.e. gray(255) as white vs gray(0) as black

Code: Select all

convert lines2.png txt:

Code: Select all

# ImageMagick pixel enumeration: 1,299,65535,gray
0,0: (65535,65535,65535)  #FFFFFF  gray(255)
0,1: (65535,65535,65535)  #FFFFFF  gray(255)
0,2: (65535,65535,65535)  #FFFFFF  gray(255)
0,3: (65535,65535,65535)  #FFFFFF  gray(255)
0,4: (65535,65535,65535)  #FFFFFF  gray(255)
0,5: (65535,65535,65535)  #FFFFFF  gray(255)
0,6: (65535,65535,65535)  #FFFFFF  gray(255)
0,7: (65535,65535,65535)  #FFFFFF  gray(255)
0,8: (65535,65535,65535)  #FFFFFF  gray(255)
0,9: (65535,65535,65535)  #FFFFFF  gray(255)
0,10: (65535,65535,65535)  #FFFFFF  gray(255)
0,11: (65535,65535,65535)  #FFFFFF  gray(255)
0,12: (65535,65535,65535)  #FFFFFF  gray(255)
0,13: (65535,65535,65535)  #FFFFFF  gray(255)
0,14: (65535,65535,65535)  #FFFFFF  gray(255)
0,15: (65535,65535,65535)  #FFFFFF  gray(255)
0,16: (65535,65535,65535)  #FFFFFF  gray(255)
0,17: (65535,65535,65535)  #FFFFFF  gray(255)
0,18: (65535,65535,65535)  #FFFFFF  gray(255)
0,19: (65535,65535,65535)  #FFFFFF  gray(255)
0,20: (65535,65535,65535)  #FFFFFF  gray(255)
0,21: (65535,65535,65535)  #FFFFFF  gray(255)
0,22: (65535,65535,65535)  #FFFFFF  gray(255)
0,23: (65535,65535,65535)  #FFFFFF  gray(255)
0,24: (65535,65535,65535)  #FFFFFF  gray(255)
0,25: (65535,65535,65535)  #FFFFFF  gray(255)
0,26: (65535,65535,65535)  #FFFFFF  gray(255)
0,27: (65535,65535,65535)  #FFFFFF  gray(255)
0,28: (65535,65535,65535)  #FFFFFF  gray(255)
0,29: (65535,65535,65535)  #FFFFFF  gray(255)
0,30: (0,0,0)  #000000  gray(0)
0,31: (0,0,0)  #000000  gray(0)
0,32: (0,0,0)  #000000  gray(0)
0,33: (0,0,0)  #000000  gray(0)
0,34: (0,0,0)  #000000  gray(0)
0,35: (0,0,0)  #000000  gray(0)
0,36: (0,0,0)  #000000  gray(0)
0,37: (0,0,0)  #000000  gray(0)
0,38: (0,0,0)  #000000  gray(0)
0,39: (0,0,0)  #000000  gray(0)
0,40: (0,0,0)  #000000  gray(0)
0,41: (0,0,0)  #000000  gray(0)
0,42: (0,0,0)  #000000  gray(0)
0,43: (0,0,0)  #000000  gray(0)
0,44: (0,0,0)  #000000  gray(0)
0,45: (0,0,0)  #000000  gray(0)
0,46: (0,0,0)  #000000  gray(0)
0,47: (0,0,0)  #000000  gray(0)
0,48: (0,0,0)  #000000  gray(0)
0,49: (0,0,0)  #000000  gray(0)
0,50: (0,0,0)  #000000  gray(0)
0,51: (0,0,0)  #000000  gray(0)
0,52: (0,0,0)  #000000  gray(0)
0,53: (0,0,0)  #000000  gray(0)
0,54: (0,0,0)  #000000  gray(0)
0,55: (0,0,0)  #000000  gray(0)
0,56: (0,0,0)  #000000  gray(0)
0,57: (0,0,0)  #000000  gray(0)
0,58: (0,0,0)  #000000  gray(0)
0,59: (0,0,0)  #000000  gray(0)
0,60: (0,0,0)  #000000  gray(0)
0,61: (0,0,0)  #000000  gray(0)
0,62: (0,0,0)  #000000  gray(0)
0,63: (0,0,0)  #000000  gray(0)
0,64: (0,0,0)  #000000  gray(0)
0,65: (0,0,0)  #000000  gray(0)
0,66: (0,0,0)  #000000  gray(0)
0,67: (0,0,0)  #000000  gray(0)
0,68: (0,0,0)  #000000  gray(0)
0,69: (0,0,0)  #000000  gray(0)
0,70: (0,0,0)  #000000  gray(0)
0,71: (0,0,0)  #000000  gray(0)
0,72: (0,0,0)  #000000  gray(0)
0,73: (0,0,0)  #000000  gray(0)
0,74: (0,0,0)  #000000  gray(0)
0,75: (0,0,0)  #000000  gray(0)
0,76: (0,0,0)  #000000  gray(0)
0,77: (0,0,0)  #000000  gray(0)
0,78: (0,0,0)  #000000  gray(0)
0,79: (0,0,0)  #000000  gray(0)
0,80: (0,0,0)  #000000  gray(0)
0,81: (0,0,0)  #000000  gray(0)
0,82: (0,0,0)  #000000  gray(0)
0,83: (65535,65535,65535)  #FFFFFF  gray(255)
0,84: (65535,65535,65535)  #FFFFFF  gray(255)
0,85: (65535,65535,65535)  #FFFFFF  gray(255)
0,86: (65535,65535,65535)  #FFFFFF  gray(255)
0,87: (65535,65535,65535)  #FFFFFF  gray(255)
0,88: (65535,65535,65535)  #FFFFFF  gray(255)
0,89: (65535,65535,65535)  #FFFFFF  gray(255)
0,90: (65535,65535,65535)  #FFFFFF  gray(255)
0,91: (65535,65535,65535)  #FFFFFF  gray(255)
0,92: (65535,65535,65535)  #FFFFFF  gray(255)
0,93: (65535,65535,65535)  #FFFFFF  gray(255)
0,94: (65535,65535,65535)  #FFFFFF  gray(255)
0,95: (65535,65535,65535)  #FFFFFF  gray(255)
0,96: (65535,65535,65535)  #FFFFFF  gray(255)
0,97: (65535,65535,65535)  #FFFFFF  gray(255)
0,98: (65535,65535,65535)  #FFFFFF  gray(255)
0,99: (65535,65535,65535)  #FFFFFF  gray(255)
0,100: (65535,65535,65535)  #FFFFFF  gray(255)
0,101: (65535,65535,65535)  #FFFFFF  gray(255)
0,102: (65535,65535,65535)  #FFFFFF  gray(255)
0,103: (65535,65535,65535)  #FFFFFF  gray(255)
0,104: (65535,65535,65535)  #FFFFFF  gray(255)
0,105: (65535,65535,65535)  #FFFFFF  gray(255)
0,106: (65535,65535,65535)  #FFFFFF  gray(255)
0,107: (65535,65535,65535)  #FFFFFF  gray(255)
0,108: (65535,65535,65535)  #FFFFFF  gray(255)
0,109: (65535,65535,65535)  #FFFFFF  gray(255)
0,110: (65535,65535,65535)  #FFFFFF  gray(255)
0,111: (65535,65535,65535)  #FFFFFF  gray(255)
0,112: (65535,65535,65535)  #FFFFFF  gray(255)
0,113: (65535,65535,65535)  #FFFFFF  gray(255)
0,114: (65535,65535,65535)  #FFFFFF  gray(255)
0,115: (65535,65535,65535)  #FFFFFF  gray(255)
0,116: (65535,65535,65535)  #FFFFFF  gray(255)
0,117: (65535,65535,65535)  #FFFFFF  gray(255)
0,118: (0,0,0)  #000000  gray(0)
0,119: (0,0,0)  #000000  gray(0)
0,120: (0,0,0)  #000000  gray(0)
0,121: (0,0,0)  #000000  gray(0)
0,122: (0,0,0)  #000000  gray(0)
0,123: (0,0,0)  #000000  gray(0)
0,124: (0,0,0)  #000000  gray(0)
0,125: (0,0,0)  #000000  gray(0)
0,126: (0,0,0)  #000000  gray(0)
0,127: (0,0,0)  #000000  gray(0)
0,128: (0,0,0)  #000000  gray(0)
0,129: (0,0,0)  #000000  gray(0)
0,130: (0,0,0)  #000000  gray(0)
0,131: (0,0,0)  #000000  gray(0)
0,132: (0,0,0)  #000000  gray(0)
0,133: (0,0,0)  #000000  gray(0)
0,134: (0,0,0)  #000000  gray(0)
0,135: (0,0,0)  #000000  gray(0)
0,136: (0,0,0)  #000000  gray(0)
0,137: (0,0,0)  #000000  gray(0)
0,138: (0,0,0)  #000000  gray(0)
0,139: (0,0,0)  #000000  gray(0)
0,140: (0,0,0)  #000000  gray(0)
0,141: (0,0,0)  #000000  gray(0)
0,142: (0,0,0)  #000000  gray(0)
0,143: (0,0,0)  #000000  gray(0)
0,144: (0,0,0)  #000000  gray(0)
0,145: (0,0,0)  #000000  gray(0)
0,146: (0,0,0)  #000000  gray(0)
0,147: (0,0,0)  #000000  gray(0)
0,148: (0,0,0)  #000000  gray(0)
0,149: (0,0,0)  #000000  gray(0)
0,150: (0,0,0)  #000000  gray(0)
0,151: (0,0,0)  #000000  gray(0)
0,152: (0,0,0)  #000000  gray(0)
0,153: (0,0,0)  #000000  gray(0)
0,154: (0,0,0)  #000000  gray(0)
0,155: (0,0,0)  #000000  gray(0)
0,156: (0,0,0)  #000000  gray(0)
0,157: (0,0,0)  #000000  gray(0)
0,158: (0,0,0)  #000000  gray(0)
0,159: (0,0,0)  #000000  gray(0)
0,160: (0,0,0)  #000000  gray(0)
0,161: (0,0,0)  #000000  gray(0)
0,162: (0,0,0)  #000000  gray(0)
0,163: (0,0,0)  #000000  gray(0)
0,164: (0,0,0)  #000000  gray(0)
0,165: (0,0,0)  #000000  gray(0)
0,166: (0,0,0)  #000000  gray(0)
0,167: (0,0,0)  #000000  gray(0)
0,168: (0,0,0)  #000000  gray(0)
0,169: (0,0,0)  #000000  gray(0)
0,170: (0,0,0)  #000000  gray(0)
0,171: (0,0,0)  #000000  gray(0)
0,172: (0,0,0)  #000000  gray(0)
0,173: (65535,65535,65535)  #FFFFFF  gray(255)
0,174: (65535,65535,65535)  #FFFFFF  gray(255)
0,175: (65535,65535,65535)  #FFFFFF  gray(255)
0,176: (65535,65535,65535)  #FFFFFF  gray(255)
0,177: (65535,65535,65535)  #FFFFFF  gray(255)
0,178: (65535,65535,65535)  #FFFFFF  gray(255)
0,179: (65535,65535,65535)  #FFFFFF  gray(255)
0,180: (65535,65535,65535)  #FFFFFF  gray(255)
0,181: (65535,65535,65535)  #FFFFFF  gray(255)
0,182: (65535,65535,65535)  #FFFFFF  gray(255)
0,183: (65535,65535,65535)  #FFFFFF  gray(255)
0,184: (65535,65535,65535)  #FFFFFF  gray(255)
0,185: (65535,65535,65535)  #FFFFFF  gray(255)
0,186: (65535,65535,65535)  #FFFFFF  gray(255)
0,187: (65535,65535,65535)  #FFFFFF  gray(255)
0,188: (65535,65535,65535)  #FFFFFF  gray(255)
0,189: (65535,65535,65535)  #FFFFFF  gray(255)
0,190: (65535,65535,65535)  #FFFFFF  gray(255)
0,191: (65535,65535,65535)  #FFFFFF  gray(255)
0,192: (65535,65535,65535)  #FFFFFF  gray(255)
0,193: (65535,65535,65535)  #FFFFFF  gray(255)
0,194: (65535,65535,65535)  #FFFFFF  gray(255)
0,195: (65535,65535,65535)  #FFFFFF  gray(255)
0,196: (65535,65535,65535)  #FFFFFF  gray(255)
0,197: (65535,65535,65535)  #FFFFFF  gray(255)
0,198: (65535,65535,65535)  #FFFFFF  gray(255)
0,199: (65535,65535,65535)  #FFFFFF  gray(255)
0,200: (65535,65535,65535)  #FFFFFF  gray(255)
0,201: (65535,65535,65535)  #FFFFFF  gray(255)
0,202: (65535,65535,65535)  #FFFFFF  gray(255)
0,203: (65535,65535,65535)  #FFFFFF  gray(255)
0,204: (65535,65535,65535)  #FFFFFF  gray(255)
0,205: (65535,65535,65535)  #FFFFFF  gray(255)
0,206: (65535,65535,65535)  #FFFFFF  gray(255)
0,207: (0,0,0)  #000000  gray(0)
0,208: (0,0,0)  #000000  gray(0)
0,209: (0,0,0)  #000000  gray(0)
0,210: (0,0,0)  #000000  gray(0)
0,211: (0,0,0)  #000000  gray(0)
0,212: (0,0,0)  #000000  gray(0)
0,213: (0,0,0)  #000000  gray(0)
0,214: (0,0,0)  #000000  gray(0)
0,215: (0,0,0)  #000000  gray(0)
0,216: (0,0,0)  #000000  gray(0)
0,217: (0,0,0)  #000000  gray(0)
0,218: (0,0,0)  #000000  gray(0)
0,219: (0,0,0)  #000000  gray(0)
0,220: (0,0,0)  #000000  gray(0)
0,221: (0,0,0)  #000000  gray(0)
0,222: (0,0,0)  #000000  gray(0)
0,223: (0,0,0)  #000000  gray(0)
0,224: (0,0,0)  #000000  gray(0)
0,225: (0,0,0)  #000000  gray(0)
0,226: (0,0,0)  #000000  gray(0)
0,227: (0,0,0)  #000000  gray(0)
0,228: (0,0,0)  #000000  gray(0)
0,229: (0,0,0)  #000000  gray(0)
0,230: (0,0,0)  #000000  gray(0)
0,231: (0,0,0)  #000000  gray(0)
0,232: (0,0,0)  #000000  gray(0)
0,233: (0,0,0)  #000000  gray(0)
0,234: (0,0,0)  #000000  gray(0)
0,235: (0,0,0)  #000000  gray(0)
0,236: (0,0,0)  #000000  gray(0)
0,237: (0,0,0)  #000000  gray(0)
0,238: (0,0,0)  #000000  gray(0)
0,239: (0,0,0)  #000000  gray(0)
0,240: (0,0,0)  #000000  gray(0)
0,241: (0,0,0)  #000000  gray(0)
0,242: (0,0,0)  #000000  gray(0)
0,243: (0,0,0)  #000000  gray(0)
0,244: (0,0,0)  #000000  gray(0)
0,245: (0,0,0)  #000000  gray(0)
0,246: (0,0,0)  #000000  gray(0)
0,247: (0,0,0)  #000000  gray(0)
0,248: (0,0,0)  #000000  gray(0)
0,249: (0,0,0)  #000000  gray(0)
0,250: (0,0,0)  #000000  gray(0)
0,251: (0,0,0)  #000000  gray(0)
0,252: (0,0,0)  #000000  gray(0)
0,253: (0,0,0)  #000000  gray(0)
0,254: (0,0,0)  #000000  gray(0)
0,255: (0,0,0)  #000000  gray(0)
0,256: (0,0,0)  #000000  gray(0)
0,257: (0,0,0)  #000000  gray(0)
0,258: (0,0,0)  #000000  gray(0)
0,259: (0,0,0)  #000000  gray(0)
0,260: (0,0,0)  #000000  gray(0)
0,261: (65535,65535,65535)  #FFFFFF  gray(255)
0,262: (65535,65535,65535)  #FFFFFF  gray(255)
0,263: (65535,65535,65535)  #FFFFFF  gray(255)
0,264: (65535,65535,65535)  #FFFFFF  gray(255)
0,265: (65535,65535,65535)  #FFFFFF  gray(255)
0,266: (65535,65535,65535)  #FFFFFF  gray(255)
0,267: (65535,65535,65535)  #FFFFFF  gray(255)
0,268: (65535,65535,65535)  #FFFFFF  gray(255)
0,269: (65535,65535,65535)  #FFFFFF  gray(255)
0,270: (65535,65535,65535)  #FFFFFF  gray(255)
0,271: (65535,65535,65535)  #FFFFFF  gray(255)
0,272: (65535,65535,65535)  #FFFFFF  gray(255)
0,273: (65535,65535,65535)  #FFFFFF  gray(255)
0,274: (65535,65535,65535)  #FFFFFF  gray(255)
0,275: (65535,65535,65535)  #FFFFFF  gray(255)
0,276: (65535,65535,65535)  #FFFFFF  gray(255)
0,277: (65535,65535,65535)  #FFFFFF  gray(255)
0,278: (65535,65535,65535)  #FFFFFF  gray(255)
0,279: (65535,65535,65535)  #FFFFFF  gray(255)
0,280: (65535,65535,65535)  #FFFFFF  gray(255)
0,281: (65535,65535,65535)  #FFFFFF  gray(255)
0,282: (65535,65535,65535)  #FFFFFF  gray(255)
0,283: (65535,65535,65535)  #FFFFFF  gray(255)
0,284: (65535,65535,65535)  #FFFFFF  gray(255)
0,285: (65535,65535,65535)  #FFFFFF  gray(255)
0,286: (65535,65535,65535)  #FFFFFF  gray(255)
0,287: (65535,65535,65535)  #FFFFFF  gray(255)
0,288: (65535,65535,65535)  #FFFFFF  gray(255)
0,289: (65535,65535,65535)  #FFFFFF  gray(255)
0,290: (65535,65535,65535)  #FFFFFF  gray(255)
0,291: (65535,65535,65535)  #FFFFFF  gray(255)
0,292: (65535,65535,65535)  #FFFFFF  gray(255)
0,293: (65535,65535,65535)  #FFFFFF  gray(255)
0,294: (65535,65535,65535)  #FFFFFF  gray(255)
0,295: (65535,65535,65535)  #FFFFFF  gray(255)
0,296: (65535,65535,65535)  #FFFFFF  gray(255)
0,297: (65535,65535,65535)  #FFFFFF  gray(255)
0,298: (65535,65535,65535)  #FFFFFF  gray(255)
You will have to do some scripting to get the heights of the white gaps.
kaliber
Posts: 5
Joined: 2015-11-13T01:29:10-07:00
Authentication code: 1151

Re: Detect distance between text lines

Post by kaliber »

Thank you very much it. Scaling to 1x solves my headaches.

Great :)
Post Reply