L.lucia
L.lucia

Reputation: 39

remove the cross marks in the image in python

I'm working on an image processing project which need to remove the cross marks in the ultrasound image first. I tried all kinds of filters in OpenCV. When the kernel is large, it may remove the marks, but it lost a lot of detail and too blurring. Here is one example: enter image description here

Upvotes: 0

Views: 958

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207345

Here's an approach - I'll leave you to fill in the details at the end. I'm basically creating a white cross on a black background and using "Template Matching" to find such things in your ultrasound image:

#!/usr/bin/env python3

import numpy as np
import cv2

# Make a white cross (+ sign) on a black background
cross = np.zeros((10,10), np.uint8)
cross[..., [4,5]] = 255
cross[[4,5], ...] = 255

The cross now looks like this:

array([[  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
       [255, 255, 255, 255, 255, 255, 255, 255, 255, 255],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255, 255,   0,   0,   0,   0]], dtype=uint8)

Carrying on with the code:

# Load ultrasound image
im = cv2.imread('ultrasound.jpg', cv2.IMREAD_GRAYSCALE)

# Look for crosses
res = cv2.matchTemplate(im, cross, cv2.TM_CCORR_NORMED)

# Contrast stretch
norm = cv2.normalize(res, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
cv2.imwrite("result.png", res)

That gives this:

enter image description here

You can then find the peaks using thresholding like this:

enter image description here

Draw crosses centred on those points and finally use in-painting to fill them.

Upvotes: 3

Related Questions