CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2005
    Posts
    6

    display a bitmap image file in black and white

    Hi,

    I want to open a bitmap image file and display it in black and white. can anyone help me in this?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: display a bitmap image file in black and white

    Do you mean black and white (2 colors) or greyscale?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    May 2005
    Posts
    6

    Re: display a bitmap image file in black and white

    Black and white, only 2 colors

  4. #4
    Join Date
    May 2005
    Posts
    15

    Re: display a bitmap image file in black and white

    To display your bmp image file into black and white you will have to write a function to threshold the image. you can set the threshold value of the colours according your own choice.

    To do this you will have to first load the image and then with the help of GetColour or getpixel functions compare the values with your threshold value and then if the value is less than your threshold then set it to black, using the setpixel (i think there is also setColour) functions. for black the value would be 0 and for white the value would be 255. and also keep in mind if you are using pixel values than you will have to set it for all the three basic colours RGB.

    Hope this is what you want.

    Farooq.

  5. #5
    Join Date
    Oct 2002
    Posts
    1,134

    Re: display a bitmap image file in black and white

    Converting color to B&W is a little more complex than farooqmaniar indicates. You'll first have to convert the color to greyscale by using some formula: one example is Greyscale = 0.299Red + 0.587Blue + 0.114Green, although there are others.

    Once you have a greyscale value, you can use thresholding to determine which pixels you force to white (because their greyscale value is greater than the threshold) and which you force to black.

    Then, once you've done all that, you'll discover - as I have - that the threshold value is quite critical in how your final image appears. You may find - as I did - that you have to vary the threshold value for each image. I never did find an algorithm for that, so if you come up with one, please publish it here.

    Good luck!
    Regards
    Robert Thompson

  6. #6
    Join Date
    May 2005
    Posts
    15

    Re: display a bitmap image file in black and white

    Well TSYS,

    Its not difficult, and its not even necessary to convert the image to a greyscale image, you can directly convert it into a black and white image, the only thing at this time you need to take care is that you have to provide threshold for all the three colours. (RGB) or may be you need to provide value for HUE, SATURATION and Intensity. (HSI)

    Well and for your question , the solution is, to do dynamic thresholding. To use dynamic thresholding there are various methods and one of them that i remember right now is iterative method. In this method you first find out the peak of the pixels (mean the maximum number of pixels for a particular grey scale value) once you have found this out, your next target is to find the second peak. Once you have found out these two peaks, you have to find the minium point(minimum number of pixels for a particular grey scale value) between these two peaks. and this point will act as a threshold value.

    Dynamic thresholding is a bit complex but it works perfect. There are other methods other than iterative method. if you search on net properly you will be able to find the algorithms.

    Farooq.

  7. #7
    Join Date
    Oct 2002
    Posts
    1,134

    Re: display a bitmap image file in black and white

    Yes, there are several interesting papers on dynamic thresholding on the net which I didn't know about. Thanks for pointing out the search term to use.

    I'm not convinced you can go directly from RGB (or HSI) to B&W. Can you point to any examples?
    Regards
    Robert Thompson

  8. #8
    Join Date
    May 2005
    Posts
    15

    Re: display a bitmap image file in black and white

    Search using the term "colour thresholding" and you will find a lot of articles.

    Farooq

  9. #9
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: display a bitmap image file in black and white

    Just a thought.

    Can't we just create a monchrome Device Context. Blit the original bitmap into this monochrome DC and blit the monochrome DC into the Windows DC.

    What do you think?
    If there is no love sun won't shine

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: display a bitmap image file in black and white

    Quote Originally Posted by miteshpandey
    Just a thought.

    Can't we just create a monchrome Device Context. Blit the original bitmap into this monochrome DC and blit the monochrome DC into the Windows DC.

    What do you think?
    That;s the exact question that popped up in my head too.. Does MS' GDI does all that thresholding internally ??

  11. #11
    Join Date
    May 2005
    Posts
    6

    Arrow Re: display a bitmap image file in black and white

    "Can't we just create a monchrome Device Context. Blit the original bitmap
    into this monochrome DC and blit the monochrome DC into the Windows DC."

    Can someone give the sample code, i have tried but not able to do it? Should be just three lines....

    Besides i am using a DIB, but i guess i can convert it to a bitmap....Any ideas???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured