PC Review


Reply
Thread Tools Rate Thread

How to convert color image to bi-level image?

 
 
ShihChengYu@gmail.com
Guest
Posts: n/a
 
      3rd Apr 2006
Dear all:

How to convert color image to bi-level image? I have confronted one
problem when I build my OCR project. I used an software API function to



enhance my project, but the API only support bi-level image. So I need
this convert code to do this process, but it is not my study field. I
try to view several website to write this code, but it can not work.
Would anyone give me the conversion code or give me some tip or website



to learn. I only need this function to complete my project. I will
appreciate your kindness, thanks a lot.

 
Reply With Quote
 
 
 
 
Chris Dunaway
Guest
Posts: n/a
 
      3rd Apr 2006
Is this what you need? if not, there are many other useful articles on
that site.

http://www.bobpowell.net/onebit.htm

 
Reply With Quote
 
ShihChengYu@gmail.com
Guest
Posts: n/a
 
      4th Apr 2006
Thanks four your response.

I found a VB.NET code to transform from color image to bi-level image.

I try to transform to C# which I used, but there is always several
error I can't solve. Is anyone can see the error reason and tell me .
Thanks a lot.
-----------------------------------------------------------------------------------------------
Public Function GetBWBitmap(ByVal src As Bitmap) As Bitmap
Dim width, height, index, maskIndex As Integer
Dim dest As Bitmap
Dim row, col As Integer
Dim bmdDest As BitmapData

width = src.Width
height = src.Height

'Create a new bitmap of the proper format
dest = New Bitmap(width, height, PixelFormat.Format1bppIndexed)
bmdDest = dest.LockBits(New Rectangle(0, 0, width, height),
dest.Flags, dest.PixelFormat)
Dim myBytes(bmdDest.Stride * height - 1), temp(0) As Byte

'Copy the src bitmap to the array of bytes
For row = 0 To height - 1
For col = 0 To width - 1
index = row * bmdDest.Stride + ((col - 3.5) / 8)
maskIndex = Math.Abs((col Mod 8) - 7)
temp(0) = myBytes(index)
Dim myBits As New BitArray(temp)
Dim myMask As New BitArray(8, 0)
If (src.GetPixel(col, row).GetBrightness() > 0.5F) Then
myMask(maskIndex) = True
End If
myBits.Or(myMask)
myBits.CopyTo(myBytes, index)
Next
Next

'Copy array of bytes into destination bitmap
Marshal.Copy(myBytes, 0, bmdDest.Scan0, bmdDest.Stride * height
- 1)
dest.UnlockBits(bmdDest)
GetBWBitmap = dest

End Function
----------------------------------------------------------------------------------------------------
public Bitmap ImageConvert(Bitmap SourceImg)
{
//Declare Variable
int ImgWidth;
int ImgHeight;
Bitmap DestinationImg;
BitmapData DestinationData;
//Set SourceImg width and height to variables imgwidth and
imgheight
ImgWidth = SourceImg.Width;
ImgHeight = SourceImg.Height;
//Create a new bitmap of the proper format
//Set DestinationImg to the Black and White formate,
Format1bppindexed.
//Specifies that the pixel format is 1 bit per pixel and
that it uses indexed color.
//The color table therefore has two colors in it.
DestinationImg = new Bitmap(ImgWidth, ImgHeight,
PixelFormat.Format1bppIndexed);
//Locks a Bitmap into system memory.
DestinationData = DestinationImg.LockBits(new Rectangle(0,
0, ImgWidth, ImgWidth), DestinationImg.Flags,
DestinationImg.PixelFormat);

//Copy the SourceImg bitmap to the array of bytes
byte[] TempBytes;
byte[] ImgBytes = new
byte[DestinationData.Stride*ImgHeight-1];
int index;
int maskIndex;
for (int row = 0; row <= ImgHeight - 1; row++)
{
for (int col = 0; col <= ImgWidth - 1; col++)
{
index = row * DestinationData.Stride + (int)((col -
3.5) / 8);
maskIndex = Math.Abs((col % 8) - 7);
TempBytes = ImgBytes;
BitArray myBits = new BitArray(TempBytes);
BitArray myMask = new BitArray(8,true);
if ((SourceImg.GetPixel(col, row).GetBrightness() >
0.5F))
{
myMask[maskIndex]= true;
}
myBits.Or(myMask);
myBits.CopyTo(ImgBytes, index);
}
}
Marshal.Copy(ImgBytes, 0, DestinationData.Scan0,
DestinationData.Stride * ImgHeight - 1);
DestinationImg.UnlockBits(DestinationData);
return DestinationImg;
}

 
Reply With Quote
 
ShihChengYu@gmail.com
Guest
Posts: n/a
 
      4th Apr 2006
The error message is "parameter is not valid", it is on
DestinationData = DestinationImg.LockBits(new Rectangle(0,
0, ImgWidth, ImgWidth), DestinationImg.Flags,
DestinationImg.PixelFormat);

Is there anyone knows how to solve this error?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Color image to Gray Shade Image Sugan Microsoft VB .NET 1 5th Jul 2006 01:54 PM
How to convert color image to bi-level image? ShihChengYu@gmail.com Microsoft C# .NET 0 3rd Apr 2006 03:55 PM
Is it possible to convert a 32-bit color image into a 1-bit-per-pixel image in VB.net? If yes, how can it be done? Ira Microsoft VB .NET 2 22nd Feb 2006 02:32 PM
Convert color image to B&W Scott Schluer Microsoft ASP .NET 7 14th Oct 2004 10:02 PM
Image - convert color image to black and white programmatically DanS Microsoft Dot NET 2 29th Aug 2003 03:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:13 PM.