-4

I want to remove the letter artifacts "L:CC and Strin" from breast mammography using python. How could I get that done? this is my image

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • Welcome to SO! What have you tried so far? Could you add some of your code? – Let's try Aug 25 '20 at 09:44
  • Possible [duplicate](https://stackoverflow.com/questions/53592055/opencv-remove-text-from-image) – Ahmet Aug 25 '20 at 09:54
  • If the letters are perfectly white (255,255,255) and no other pixels are perfectly white in the image, then just change white to black by thresholding to find the white pixels and use that as a mask. `image[mask=255] = (0,0,0)` – fmw42 Aug 25 '20 at 16:38
  • letters are also of different intensity. not perfectly white. – Hachughtai97 Aug 26 '20 at 06:13

2 Answers2

1

Here is one way to do that in Python/OpenCV.

  • Read the input
  • Convert to grayscale
  • Threshold
  • Dilate as mask
  • Apply mask to change white letters to black
  • Save the results

import cv2
import numpy as np

# read image
img = cv2.imread('mammogram_letters.png')

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# create mask
thresh = cv2.threshold(gray, 247, 255, cv2.THRESH_BINARY)[1]

# dilate mask
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)

# apply change
result = img.copy()
result[mask == 255] = (0,0,0)

# save result
cv2.imwrite("mammogram_letters_thresh.png", thresh)
cv2.imwrite("mammogram_letters_mask.png", mask)
cv2.imwrite("mammogram_letters_blackened.png", result)

# show results
cv2.imshow("THRESH", thresh)
cv2.imshow("MASK", mask)
cv2.imshow("RESULT", result)
cv2.waitKey(0)

Threshold image:

enter image description here

Mask image:

enter image description here

Result:

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80
0

You have to get pixel coordinate of the box containing test, if they are always the same my code will work.

    from PIL import Image
    im = Image.open('SqbIx.png')
    img =im.load()
    for i in range (73,116):
            for j in range (36,57):
                img[i,j]= (0, 0, 0)
    im.save('mod.png')
gkab
  • 21
  • 4