判断一幅图片是否是黑色的,通过灰度直方图,颜色直方图或图像亮度等方法可以实现
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Bitmap bmpobj;
private void Form1_Load(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\33.jpg";
pictureBox1.Image = Image.FromFile(path);
bmpobj = new Bitmap(Application.StartupPath + @"\33.jpg");
//bmpobj = new Bitmap(pic); //转换为Format32bppRgb
}
/// <summary>
/// 根据RGB,计算灰度值
/// </summary>
/// <param name="posClr">Color值</param>
/// <returns>灰度值,整型</returns>
private int GetGrayNumColor(System.Drawing.Color posClr)
{
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
}
/// <summary>
/// 灰度转换,逐点方式
/// </summary>
public void GrayByPixels()
{
int totalNum = 0;
int totalGrayPix = 0;
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
totalNum += 1;
totalGrayPix += tmpValue;
//反转
//bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
}
}
//判断是否是黑色图片:0=全黑,255=全白;这种方法不是很准的,效率也不是很高
//取各个像素的平均值
int avgGray = totalGrayPix / totalNum;
if (avgGray < 50)
{
MessageBox.Show("像素灰度平均值:" + avgGray.ToString() + ",认为是黑色或接近黑色");
}
else
{
MessageBox.Show("彩色图像");
}
}
private void button1_Click(object sender, EventArgs e)
{
GrayByPixels();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Bitmap bmpobj;
private void Form1_Load(object sender, EventArgs e)
{
string path = Application.StartupPath + @"\33.jpg";
pictureBox1.Image = Image.FromFile(path);
bmpobj = new Bitmap(Application.StartupPath + @"\33.jpg");
//bmpobj = new Bitmap(pic); //转换为Format32bppRgb
}
/// <summary>
/// 根据RGB,计算灰度值
/// </summary>
/// <param name="posClr">Color值</param>
/// <returns>灰度值,整型</returns>
private int GetGrayNumColor(System.Drawing.Color posClr)
{
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
}
/// <summary>
/// 灰度转换,逐点方式
/// </summary>
public void GrayByPixels()
{
int totalNum = 0;
int totalGrayPix = 0;
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
totalNum += 1;
totalGrayPix += tmpValue;
//反转
//bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
}
}
//判断是否是黑色图片:0=全黑,255=全白;这种方法不是很准的,效率也不是很高
//取各个像素的平均值
int avgGray = totalGrayPix / totalNum;
if (avgGray < 50)
{
MessageBox.Show("像素灰度平均值:" + avgGray.ToString() + ",认为是黑色或接近黑色");
}
else
{
MessageBox.Show("彩色图像");
}
}
private void button1_Click(object sender, EventArgs e)
{
GrayByPixels();
}
}
}