removing vertical lines in grayscale images by thickness Suppose we have a grayscale image that contains vertical lines. Now suppose that not all vertical lines are the same, some of them have different thickness. 

Question is, is there a way, in MATLAB or other programming language, to filter the vertical lines by thickness? In other words, based on the user selected thickness, only thin vertical lines are removed, while all other thick vertical lines are kept intact? or all thick vertical lines removed, all other thin vertical lines intact? 

I tried experimenting with MATLAB with the morphological operators, but nothing seems to really work, perhaps I am using the wrong method. 

edit: 
I am thinking about one possible way to solve this, but not in code, just as an idea. Every vertical line has 2 vertical edges. For every pair of 2 edges (1 line), calculate distance between two edges. Then, based on user threshold, remove or keep corresponding lines based on these calculated distances.

edit2: here is sample image:
 
How to remove only 3 thin lines and keep 2 thick lines OR how to remove 2 thick lines and keep 3 thin lines? That is what I have to accomplish. 

edit3: what I mean by remove is in this context interpolate them with the background (average value of left and right close pixels).
Here is some Python code that does mostly what you want.  You need to add the thick/thin line selection logic.  You'll probably want to tweak it some more too.  There seem to be some artifacts when it is turned back into a .jpg.


from PIL    import Image

import numpy as np

#============================================================
def main():

#---- Load the Image

        theImage = Image.open( "test.jpg")

        thePixels = theImage.load()

        theRangeMin = 0
        theRangeMax = 7

#---- Get the Size

        theSize = theImage.size

        theWidth  = theSize[0]
        theHeight = theSize[1]

        print theWidth, theHeight

#---- Scan some Rows

        theSum = np.zeros( theWidth )
        theScanCount = 0

        for n in range( 100, theHeight, 100 ):
            print n
            theScanCount += 1

            for m in range( theWidth ):
                theShade = thePixels[m,n]
                theSum[m] += theShade[0] + theShade[1] + theShade[2]

#---- Calculate Derivatives

        theFirst  = np.zeros( theWidth )
        theSecond = np.zeros( theWidth )
        theSum /= ( 3.0 * theScanCount )

        for m in range( 1, theWidth-1 ):
            theFirst[m]  = ( theSum[m+1] - theSum[m-1] ) * 0.5
            theSecond[m] = theSum[m+1] - 2.0 * theSum[m] + theSum[m-1]
            print m, theSum[m], theSecond[m], theFirst[m]


#---- Find Vertical Lines

        theThreshold = 10

        theLastFirst = 0

        theLines = []

        m = 1
        while m &lt; theWidth-1:
            if theSum[m] &lt; theThreshold:
                while m &lt; theWidth-1:
                    if theSum[m] &gt; theThreshold and theFirst[m] &lt; theThreshold:
                        break
                    m += 1

                print theLastFirst, m
                theRange = m - theLastFirst
                theRate = 1.0 / float( theRange )
                if theRangeMin &lt;= theRange and theRange &lt;= theRangeMax :
                    theLines.append( [ theLastFirst, m, theRange, theRate ] )

            elif abs( theFirst[m] ) &lt; theThreshold : theLastFirst = m

            m += 1

#---- Hide the Lines

        theLineCount = len( theLines )

        for n in range( theHeight ):
            for p in range( theLineCount ):
                theLine = theLines[p]

                theSpot = theLine[0]
                theFirstShade = thePixels[theSpot,n]
                theLastShade  = thePixels[theLine[1],n]
                theRange = theLine[2]
                theRate  = theLine[3]
                theFirstVal = ( theFirstShade[0] + theFirstShade[1] + theFirstShade[2] ) / 3.0 
                theLastVal  = ( theLastShade[0]  + theLastShade[1]  + theLastShade[2] ) / 3.0
                theSlope = ( theLastVal - theFirstVal ) * theRate

                if n == 100: print theSpot, theFirstVal, theLastVal

                for r in range( 1, theRange ):
                    theVal = theFirstVal + r * theSlope
                    theGrey = int( theVal ) & 0xFF
                    theShade = ( theGrey &lt;&lt; 16 ) + ( theGrey &lt;&lt; 8 ) + theGrey
                    thePixels[theSpot+r,n] = theShade
                    if n == 100: print "     ", theVal

#---- Write the Image to a File

        theImage.save( "out.jpg" )

#============================================================
main()





Followup:

Here is the debug output at the first thin line:


194 119.857142857 8.85714285714 -1.14285714286
195 123.142857143 -3.71428571429 1.42857142857
196 122.714285714 -64.7142857143 -32.7857142857
197 57.5714285714 13.8095238095 -58.2380952381
198 6.2380952381 88.0 -7.33333333333
199 42.9047619048 42.7142857143 58.0238095238
200 122.285714286 -79.2380952381 39.7619047619
201 122.428571429 0.428571428571 0.357142857143
202 123.0 -0.142857142857 0.5
203 123.428571429 0.0 0.428571428571
204 123.857142857 0.142857142857 0.5
205 124.428571429 -0.285714285714 0.428571428571
206 124.714285714 1.42108547152e-14 0.285714285714