zoukankan      html  css  js  c++  java
  • 图像的灰度和黑白处理算法

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

    效果图:

     (原图)

    (处理处理)

    (黑白处理)

    代码:

    代码
    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);

    }
    }
    }
  • 相关阅读:
    pycharm 安装第三方库,出现错误: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visual studio.com/visual-cpp-build-tools
    c# 开发常用小方法
    [LeetCode]28. 实现 strStr()
    [LeetCode]27. 移除元素
    [LeetCode]21. 合并两个有序链表
    [LeetCode]20. 有效的括号
    [LeetCode]14. 最长公共前缀
    [LeetCode]13. 罗马数字转整数
    [LeetCode]9. 回文数
    [LeetCode]2. 两数相加
  • 原文地址:https://www.cnblogs.com/jimson/p/Graph.html
Copyright © 2011-2022 走看看