3

I followed (and modified) the method from the best-rated answer of this post.

My image is a little bit different. I used HoughLinesP and managed to detect the majority of red lines. enter image description here

I was wondering is there a way to remove detected lines from the image, without damage to the other black intersecting lines? I am interested in black lines only. Is there a smarter way to isolate black lines without too many missing pixels and segments?

CypherX
  • 7,019
  • 3
  • 25
  • 37
Rosa
  • 155
  • 10
  • The way to go, I think, would be to (1) perform FFT, (2) selectively remove the frequencies that correspond to the lines, and (3) do inverse FFT. – PlinyTheElder Sep 19 '19 at 13:51

2 Answers2

2

If you want to isolate just black lines, a simple Otsu's threshold and bitwise-and should do it

enter image description here

import cv2

image = cv2.imread('3.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
result = cv2.bitwise_and(image,image,mask=thresh)
result[thresh==0] = (255,255,255)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey()
nathancy
  • 42,661
  • 14
  • 115
  • 137
0

This looks like a problem of signal-separation/processing.

I don't know if this would work or not. But this is just a hunch. Give it a shot and see if it works. Assume your image as a convolved image of the measuring strip and the ECG. So, if you process this in the Fourier domain, perhaps you could dis-entangle these two types of signals.

  1. Take fourier transform (FFT) of the image. (scipy has fft functionality). Call the original image: f and fft-image: F.
  2. Take an image of just the measuring strip (but no measured pattern on it for ECG) and evaluate the FFT for this one as well. Call this image: g, fft-image: G.
  3. Calculate inverse FFT of (F/G) and see if that clears up the background effect.

In case this does not work, please leave a note in the comment section.

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • When I try `solution = fftpack.ifft(im_fft/grid_fft)` it gives me **ValueError: operands could not be broadcast together with shapes (753,1244,4) (753,1244,3)** I am a beginner (surprise, surprise) – Rosa Sep 19 '19 at 06:21
  • You would need to use `fftshift` and `ifftshift` in conjunction with `fft`/`ifft`. See the examples here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.fft.fftshift.html. – CypherX Sep 19 '19 at 06:26
  • Normally for an image there are 3 RGB and an alpha channel. The alpha channel would consist of transparency/opacity information. Looks like your ECG image has an alpha channel in it. Use numpy indexing to drop the alpha channel first and then apply `fft`. On the other hand, see what happens, if you teak with the alpha channel numbers. I don't know: because that's some additional information, perhaps you could exploit it. – CypherX Sep 19 '19 at 06:31
  • No no, FFT of (F/G) would be deconvolution, not removal of G from F. That would not work at all in this case. You would need the subtraction of (F-G), but you can't do that either because there's an unknown phase shift. The correct solution is to do FFT and then selectively remove certain frequencies. – PlinyTheElder Sep 19 '19 at 13:46
  • @PlinyTheElder: Yes, it would be deconvolution. If you see, I mentioned that the assumption is to treat the ECH image as a convolved image of two times of signal: ECG and background. I know this could be a completely wrong to assume. But still, if it works to some extent, thought of suggesting that. And I think you meant iFFT(F/G). – CypherX Sep 19 '19 at 16:10
  • @Rosa Another thing that I could think of is using Independent Component Analysis (ICA from sklearn library). ICA could separate two types of signals from a mixture of them. See the examples here: https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.FastICA.html. – CypherX Sep 19 '19 at 16:10
  • @Rosa Please try and see if you get anything meaningful by calculating iFFT(F-G). – CypherX Sep 19 '19 at 16:14
  • @PlinyTheElder Is there any method that could standardize the selective removal of frequencies you suggested? – CypherX Sep 19 '19 at 21:53
  • @CypherX: I don't think there's anything built into OpenCV that you could use out of the box, but it certainly can do FFT and iFFT. Have a look here for the explanation of the principle: https://craftofcoding.wordpress.com/tag/periodic-noise/ – PlinyTheElder Sep 20 '19 at 13:42