Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binarize images 1

Status
Not open for further replies.

jamesoc

Technical User
Feb 1, 2005
38
IE
any body got any information on how to binarize an image or any links to other pages .thanks
 
How do you mean? Your question isn't as clear as it might be (at least, not to me)
 
i need to binarize an image ie change the image from colour into a black and white image (bitmap)
 
Aha!

One method (requiring the a small number of API calls, and with very limited 'intelligence' concerning the conversion) would be to BitBlit an image from a color device context to a monochrome device context ...
 
Strongm,
I was also working on this idea. But I realized that this method has limitations. The color on the bitmap that matches the background color of the source DC is painted as white. All other colors are painted as black.
I think thats why you say 'very limited intelligence'...

This technique is good in color masking. But not for color to monochorme conversion.

I remember there was a thread in which we posted code to convert a color image to grayscale. I used SetColorAdjustment function, and you posted GDI+ code. But I cannot find that thread.
 
Yep. I didn't want to get onto the more complicated versions straight away.

And I can't find outr old threads either, s0 here's my version using ColorAdjust...

Option Explicit

Private Declare Function GetColorAdjustment Lib "gdi32" (ByVal hdc As Long, lpca As COLORADJUSTMENT) As Long
Private Declare Function SetColorAdjustment Lib "gdi32" (ByVal hdc As Long, lpca As COLORADJUSTMENT) As Long
Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long

Private Type COLORADJUSTMENT
caSize As Integer
caFlags As Integer
caIlluminantIndex As Integer
caRedGamma As Integer
caGreenGamma As Integer
caBlueGamma As Integer
caReferenceBlack As Integer
caReferenceWhite As Integer
caContrast As Integer
caBrightness As Integer
caColorfulness As Integer
caRedGreenTint As Integer
End Type

Private Const HALFTONE = 4


Private Sub Picture1_Click()
Dim ca As COLORADJUSTMENT
With Picture1
.AutoRedraw = True
.ScaleMode = vbPixels
SetStretchBltMode .hdc, HALFTONE
GetColorAdjustment .hdc, ca
ca.caColorfulness = -100 'No colors!
SetColorAdjustment .hdc, ca
StretchBlt .hdc, 0, 0, .ScaleWidth, .ScaleHeight, .hdc, 0, 0, .ScaleWidth, .ScaleHeight, vbSrcCopy
.Refresh
End With
End Sub
 
thanks for your help ill give the code a try .will the code work with any image i put in to picture1
 
after loading the code i keep getting error warnings saying
"only coments may appear after end sub end function or end property" and the seconbd line of code declaring 'Private Declare Function getcoloradjustment Lib "gdi32" (ByVal hdc As Long, ipac As coloradjustment) As Long' is highlighted am i missing something
 
All of the "Private Declare" statments must appear at the top of your form or module (along with the option explicit statement).
 
i have move the option explicit and private declare function into the general part of the form but now im only getting user defined type not defined any idea on the cause would be great help
 
Tell you what ...


Start a brand new VB project with a single Form. Add a Picturebox to the form

Copy the code I've provided exactly as is and paste it into the Form's code window, replacing everything that might already be there.

Load a picture of your choice into the picturebox via it's designtime Picture property

Run the program. Click the picture. Tell us what happens

 
it converted the image to gray scale .thanks alot for your help i guess i should have been able to see were the warning were coming from myself in hindsight
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top