How do I detect the image area in an ultrasound picture using numpy/openCV?

Viewed 41

I have a dataset of medical ultrasounds in which I'm trying to detect the orientation of the probe (highlighted in red). I've attached sample images below:

enter image description here enter image description here enter image description here

I attempted several methods which were all unsuccessful and not even close. Short of training a quick neural network, I've run out of ideas. Any help would be appreciated!

Methods I've tried:

1 -Using numpy, I tried taking two horizontal slices from both the top and bottom of the image to determine the ratio of black to other (white and gray) colors. If the black content is higher in two successive slices, then the peak is oriented towards that side of the image. A sample is shown:

blacks = np.sum(dcm1[:, int(0.25*dcm1.shape[1]), :] == 0)
grays = np.sum(dcm1[:, int(0.25*dcm1.shape[1])] > 150)
ratio = blacks/grays

2-I attempted to pass the image through adaptive thresholding algorithm and Canny filter to determine the proportion of black to white pixels from the top half and bottom of the image. I thought that the blacker half correspond to the side of the peak. This method was not accurate either.

enter image description here

3-I attempted to detect the black triangles in the image. My reasoning was that the peak of the black triangle always points away from the orientation. Unfortunately, I couldn't even get this method to recognize the black triangle.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
i = 0
# list for storing names of shapes
for contour in contours:
    if i == 0:
        i = 1
        continue
    # cv2.approxPloyDP() function to approximate the shape
    approx = cv2.approxPolyDP(
    contour, 0.01 * cv2.arcLength(contour, True), True)
    cv2.drawContours(img, [contour], 0, (0, 0, 255), 5)

enter image description here

1 Answers

I was able to reliably detect orientation using HoughCircles to detect the encircled P denoting the probe on the image.

    src = dcm2[0] # DICOM Pixel Array
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    gray = cv2.medianBlur(gray, 5)
    rows = gray.shape[0]
    circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
                               param1=100, param2=30,
                               minRadius=1, maxRadius=30)

    
    if circles is not None:
        circles = np.uint16(np.around(circles))
        for i in circles[0, :]:
            center = (i[0], i[1])
            cv2.circle(src, center, 1, (0, 100, 100), 3)
            radius = i[2]
            cv2.circle(src, center, radius, (255, 0, 255), 3)
    
    
    cv2.imshow("detected circles", src)
    cv2.waitKey(0)
Related