Detect pregnancy test results using OpenCV [closed]

Pretty much looking for what it says 😁

What I'm working with is a little different, but the end result is the same. Given an image, I'm wondering how I can count the lines (1 = negative, 2 = positive) to determine the results of the test.

I've tried the same thing using TensorFlow with a large sample and I'm getting mixed results, and considering the project currently implements OpenCV for some other functionality, it would make sense to try to refactor this portion to use it as well.

The images being detected could have differing angles, lighting, etc. The lines could also be a different color. Looking to avoid using Python specific items (such as Numpy) as the final code needs to be converted to C++ and Java, but given some python examples as a starting point would be great!

I've been looking high and low for some sort of relevant example and haven't come up with anything. Any help is greatly appreciated!!

I don't have exact images I can share currently, but these should provide similar enough approximations to get to the desired results:

negative result positive result


I'm having good results by thresholding on the red line(s) using InRange. Then do some morphology to make sure the lines are contiguous blobs. Finally, use findContours to count the number of blobs.

import cv2

img = cv2.imread('ptest1.png')

img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

img = cv2.inRange(img, (150, 60, 100), (255, 255, 255))

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (11,11))

img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel, 6)

img, contours, hier = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

print('number of lines in test is ', len(contours)/2 )