Image Processing/Filtering VB (Visual Basic)

Posted on 2002-03-27
Last Modified: 2013-11-25
Same question I had posted long time back, again with Increased points:

Please visit for more information and for sample images.

I want to filter the above images  so that only number will appear on the image with black background.
i.e. numbers numbers in white having black background.
What I have tried up till now is Sobel operator and other edge detecting techniques but I am not satisfied
with the result. (coz that operator requires the tolerance or threshold and I want to automate it without
any user input. i.e. i am looking for something (algorithm) , which will calculate the tolerance/threshold
 depending upon image quality (eg. Image 4 having tolerance 157, but for image 3 it may be less.)
Please help me.
I need the algorithm or program in VB for the Image processing. If you have any questions please feel

Please visit for more information and for sample images.

Thanks and kind regards.


I can convert it to B/W, that’s no problem. But while converting to B/W I loose image data. (As
I have image in Gray scale or color, I want the data from the white part, which are numbers, but not
always picture quality is good. So on the web page I have put some different images for that. [try converting
those images , you will loose the number (white) part L.].
Is there is any way we can find out programmatically the quality of the image?

If so please please please please let me know.

Thanks and kind regards,
--- (MCSD)
Question by:GuruVB

Expert Comment

ID: 6899670
Hello GuruVB !

--> Is there is any way we can find out programmatically the quality of the image?

I think this is the crucial point. Making a B/W image from a color image when a threshold is given isn't difficult.

You want to get the best threshold value in dependency of the image quality.

If the below conditions are true i would have a possible solution:

1. What you have to filter out are character or digis
2. You can find free OCR-Software or Code or you can purchase it

Algorithm (only principle):

Private Function GetThreshold() As Byte
    Dim myOCR As New myOCRClass
    Dim LowVal As Byte
    Dim HighVal As Byte
    For LowVal = 0 To 255
        Set Picture1.Picture = LoadPicture("C:\TEMP\Test.BMP")
        Call SetPictureLuminosity(Picture1, LowVal)
        If myOCR.GetText(Picture1.Picture) <> "" Then Exit For

    For HighVal = 255 To 0 Step -1
        Set Picture1.Picture = LoadPicture("C:\TEMP\Test.BMP")
        Call SetPictureLuminosity(Picture1, HighVal)
        If myOCR.GetText(Picture1.Picture) <> "" Then Exit For

    GetThreshold = (HighVal + LowVal) / 2
    Set myOCR = Nothing
End Function

LVL 22

Expert Comment

ID: 6899761
Although I have not explored this too deeply, what comes to mind is a two-part scan.  The first part builds an array or all color values, ranging from (0,0,0) to (255,255,255).  (Obviously this will take much less memory if dealing with a 256-color image since most of the combinations will not occur.)

Once you have the array, rank the high end such that you can determine what would be a good threshhold.  For example, take 10% of the high-end numbers (based on some criterion that you determine, like the sum of the RGB components > 255+127 or if any one component is above 240 then it's good) and use that as your threshhold.

In the second scan, anything in the 10% range gets turned white, and anything else gets turned black.

Try adjusting the 10% so it meets your expected level...maybe 25% or even 50%.

For very bright pictures, the 10% will be a lot of near-white combinations; for very dark pictures, the 10% will be a lot of low-valued combinations.

I think the hardest part is determining how to combine the R,G,B components to determine a ranking, but if you find a color-to-grayscale algorithm, it probably becomes easy.
LVL 18

Expert Comment

ID: 6899839
I tried a few things on the samples from your website, and while most things worked pretty well on the big sample, non of the other samples worked well at all.  This is because there is so much glare or refracted light on the numbers that it's very difficult to distinguish between the number and the glare in the glass.

The technique that I tried was to go pixel by pixel to see if the long pixel color value was closer to vbWhite or vbBlack.  Depending on which it was, I'd set it to white or black. If you move the cuttoff point around then you can distinguish between different shades of white, turning one shade to black and others to white.  The problem in your images is that many of the pixels in the glare areas of your sample pictures are brighter than some of the pixels in the number area, so, it will be difficult to ever get a clean image from these samples.

As far as suggestions about picture quality, you could possilby save the darkest dark value, the lighest white value and the average color value of all pixels to get an overall idea of contrast.

The other thought that I had, but never got around to trying, would be to first put the image through some filters to rachet up the contrast, so that there would be a greater distinction for the light areas and dark areas, but you'll still have problems if your photos are going to include glare, I just don't see a way around it.
LVL 49

Expert Comment

ID: 7637900
Hi GuruVB,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Save as PAQ -- No Refund.

GuruVB, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer

Accepted Solution

SpideyMod earned 0 total points
ID: 7754798
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question