You can do this job by using FindContours
- If Image is color convert Gray by using cvtColor.
- Convert the image to black and white , by using cv2.threshold, cv2.adaptiveThreshold.
- Be sure that the forground digist are white and the background is black
- Use findcontours to find all regions in the image
- Sort All regions from left to right.
- Loop on all regions and find the avarage width and avarage hight , you can ignore very small and very big regions.
- Loop again and Strart to collect the regions into line ,
- take region if it it's larger than Threshold1 a and less than Threshold2 add the region to line , Threshold1 and Threshold2 can be calculated from Avarage say Threshold1 > 0.7 Avarage and Threshold2 < 1.3 Avarage
- If the image contain more than one line , you can add new line when the traget region doe not intersect with any previouse lines , it the target region intersect with line just add the region to the line and update the line rectangle by merging the line and traget region
- Now you have all lines in the image and all regions inside every line