偶尔找一个图像转化的算法,试了试不错。
效果图:
(原图)
(处理处理)
(黑白处理)
代码:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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);
}
}
}
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);
}
}
}