图像的灰度和黑白处理算法

偶尔找一个图像转化的算法,试了试不错。

效果图:

2010060918215066.jpg (原图)

2010060918220183.png(处理处理)

2010060918221367.png(黑白处理)

代码:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace ConsoleApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


Color GrayT(Color c)
{
int rgb = Convert.ToInt32(( double )( 0.3 * c.R + 0.59 * c.G + 0.11 * c.B));
return Color.FromArgb(rgb, rgb, rgb);

}

void Binarizate(Bitmap map)
{
int tv = ComputeThresholdValue(map);
int x = map.Width;
int y = map.Height;
for ( int i = 0 ; i < x; i ++ )
{
for ( int j = 0 ; j < y;j ++ )
{
if (map.GetPixel(i, j).R >= tv)
{

map.SetPixel(i, j, Color.FromArgb(
0xff , 0xff , 0xff ));
}
else
{
map.SetPixel(i, j, Color.FromArgb(
0 , 0 , 0 ));
}
}
}
}

int ComputeThresholdValue(Bitmap map)
{
int i;
int k;
double csum;
int thresholdValue = 1 ;
int [] iHist = new int [ 0x100 ];
for (i = 0 ; i < 0x100 ; i ++ )
{
iHist[i]
= 0 ;
}

int gmin = 0xff ;
int gmax = 0 ;

for (i = 1 ; i < map.Width - 1 ; i ++ )
{
for ( int j = 1 ; j < map.Height - 1 ; j ++ )
{
int cn = map.GetPixel(i, j).R;
iHist[cn]
++ ;
if (cn > gmax)
gmax
= cn;
if (cn < gmin)
gmin
= cn;
}
}

double sum = csum = 0.0 ;
int n = 0 ;
for (k = 0 ; k < 0xff ; k ++ )
{
sum
+= k * iHist[k];
n
+= iHist[k];
}

if (n == 0 )
{
return 60 ;
}

double fmax = - 1.0 ;
int n1 = 0 ;
for (k = 0 ; k < 0xff ; k ++ )
{
n1
+= iHist[k];
if (n1 != 0 )
{
int n2 = n - n1;
if (n2 == 0 )
{
return thresholdValue;
}

csum
+= k * iHist[k];
double m1 = csum / (( double )n1);
double m2 = (sum - csum) / (( double )n2);
double sb = ((n1 * n2) * (m1 - m2)) * (m1 - m2);
if (sb > fmax)
{
fmax
= sb;
thresholdValue
= k;
}

}

}
return thresholdValue;
}

private void button1_Click( object sender, EventArgs e)
{
string path = @" C:\Documents and Settings\v-jimsom\My Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\ " ;
string mapPath = path + @" Image\untitled.bmp " ;
string grayPath = path + @" Image\untitled1.bmp " ;
string bitPath = path + @" Image\untitled2.bmp " ;
Image map
= Bitmap.FromFile(mapPath);
Bitmap bitmap
= new Bitmap(map);

// Gray the bitmap

for ( int i = 0 ; i < bitmap.Width; i ++ )
{
for ( int j = 0 ; j < bitmap.Height; j ++ )
{
Color cc
= bitmap.GetPixel(i, j);
bitmap.SetPixel(i, j, GrayT(cc));
}
}

if (File.Exists(grayPath))
{
File.Delete(grayPath);
}
bitmap.Save(grayPath);

this .pictureBox1.Image = Bitmap.FromFile(grayPath);

// binarizata the bitmap

Binarizate(bitmap);
if (File.Exists(bitPath))
{
File.Delete(bitPath);
}
bitmap.Save(bitPath);

this .pictureBox1.Image = Bitmap.FromFile(bitPath);

}
}
}

 

转载于:https://www.cnblogs.com/jimson/archive/2010/06/09/Graph.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值