# Find tracing/plotting stacked bands/lines in an image

I have images with lines/bands, the bands/lines are parallel but can be of different gray levels, I want to extract or trace the lines/bands as single line, all lines/bands are parallel.

Source:

Output Plot:

The source image contain sine wave lines (10 pixels in amplitude) that are stacked on top on of each other forming what looks like bands. I used the location of the max of the cross-correlation of the first column with other columns of the image.

I am looking for other approaches as cross correlation is too expensive and very sensitive to noise.

• Well, due to the gray scale variations, Have you tried to apply a simple gradient to you image and extract the positions of all values greater than 0 (considering any noise is affecting your image) ? – Alexander Leon VI Oct 22 '15 at 15:57
• I tried using a sobel filter (vertically and/or horizontally), but the results is too noisy and I am unable to get rid of the top and bottom edge effects. Also I am not able to isolate a single line. – Mohan Baro Oct 22 '15 at 16:22
• Ok, maybe you can smooth the image before apply the sobel filter – Alexander Leon VI Oct 22 '15 at 19:04
• I have a question, due to all lines are 'parallel', do you really need all the bands or just one is enough? – Alexander Leon VI Oct 23 '15 at 14:28
• Correlation method seems to be giving the best answer. The simple summation method fails in the presence of noise. – Mohan Baro Nov 25 '15 at 18:01

Why not try just summing the columns of the image?

Well, you could try smoothing the image first, but that will have the same effect as just smoothing the output.

R code for this is below the first lot.

Here's the extra image, processed.

R Code Below

# 26593
# install.packages("png")

file <- "Q26593_original.png"

library(png)

d <- dim(img)

par(mfrow=c(2,1))
plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y")
lim <- par()
rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])

plot(colSums(img[,,1]), type="l")


R Code #2 Below

# 26593
# install.packages("png")
# install.packages("EBImage") --- Didn't work.
# ## try http if https is not available
# source("http://bioconductor.org/biocLite.R")
# biocLite("EBImage")

file <- "Q26593_original.png"

library(png)
library(EBImage)

d <- dim(img)

#xx <- (0:(d[2]-1) - d[2]/2)/d[2]
#yy <- (0:(d[1]-1) - d[1]/2)/d[1]
#sigma <- 0.2

#w <-  exp(-yy*yy/sigma) %*%  t(exp(-xx*xx/sigma))
#w1 <- exp(-yy*yy/sigma) %*% t(rep(1,d[2]))

f <- makeBrush(11, shape='disc', step=FALSE)
imgf <- filter2(img, f)
imgf <- imgf/max(imgf)

par(mfrow=c(3,1))
plot(1:2, type='n', main="Filtered Image", xlab="x", ylab="y")
lim <- par()
rasterImage(imgf, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])

#plot(1:2, type='n', main="Window Function", xlab="x", ylab="y")
#lim <- par()
#rasterImage(w1, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])

plot(colSums(imgf[,,1]), type="l", main="Column sum of filtered image")

plot(filter(colSums(img[,,1]), rep(1,11)), type="l", main="Column sum of unfiltered image, filtered")

• Thank you @Peter, I had tried the summation and averaging methods, as you have shown it does work at the conceptual level, but the issue is the edge artifacts (kinks in the curve that you have plotted). I believe is coming from the edges (top and bottom boundary). It looks ok with this image, but when I have more complicated source images the summation method fails. I am glad though you also came up with the summation idea. Any suggestions on how I could get rid of the edge artifacts? – Mohan Baro Oct 23 '15 at 13:34
• @MohanBaro: Just filter it! :-) – Peter K. Oct 23 '15 at 15:24
• Thank you very much @Peter, can you please try this source image imgur.com/D4D0NLm with your R code. I am not familiar with R and I have not gotten around to converting your code. – Mohan Baro Oct 23 '15 at 22:07
• @MohanBaro : Done. – Peter K. Oct 24 '15 at 0:31
• Thank you @Peter K, it does seem work good. As soon I complete testing I will post the expect resulted for the source image 2, I was expecting it to have more correspondence with the observed semi-circular curved sections.. – Mohan Baro Oct 25 '15 at 5:07