VB.net中的颜色识别 [英] Color Recognition in VB.net , Rgb
本文介绍了VB.net中的颜色识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
现在,我想对存储在硬盘中的图片进行颜色识别.我想分开颜色(哪个像素是蓝色,哪个像素是红色,依此类推).我使用了Visual Studio 2008.有可能发展吗?任何人都可以向我提出建议.
Hi All,
Now I want to make color recognition for picture that stored in hard disk. I want to separate color (which pixel is blue, which pixel is red and so on). I used Visual Studio 2008 . Is it possible to develop it? Anyone make me suggestion , please.
推荐答案
仔细阅读以下代码,这可能会对您有所帮助
go through the below code this may helps you
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GetPixelExample
{
public class Form1 : Form
{
private class ImageControl : Panel
{
private Bitmap bitmap = new Bitmap("C:\\alien.jpg");
public ImageControl()
{
MouseMove += new MouseEventHandler(HandleMouseMove);
}
private void HandleMouseMove(object sender, MouseEventArgs e)
{
// Scale the window coordinates to the bitmap coordinates
double windowX = e.X;
double windowY = e.Y;
double controlWidth = Width;
double controlHeight = Height;
double imageWidth = bitmap.Width;
double imageHeight = bitmap.Height;
Color pixel = bitmap.GetPixel(
(int)(windowX * bitmap.Width / controlWidth),
(int)(windowY * bitmap.Height / controlHeight));
System.Diagnostics.Debug.WriteLine(pixel);
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
Invalidate();
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
pevent.Graphics.DrawImage(bitmap, Bounds);
}
}
public Form1()
{
ImageControl control = new ImageControl();
control.Dock = DockStyle.Fill;
Controls.Add(control);
this.PerformLayout();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
是的,有可能.但这可能并不像您想的那么简单-它可能取决于文件的存储方式.位图很简单:一个像素就是一个像素. jpeg并不是那么容易:使用有损"算法对数据进行压缩,因此以蓝色像素旁边的红色像素开始的颜色可能变成洋红色像素.
无论哪种方式,首先要开始的是加载图像:
Yes, it it possible. But it may not be as simple as you think - it may depend on what the file is stored as. A bitmap is easy: a pixel is a pixel. A jpeg is not so easy: the data is compressed using a "lossy" algorithm, so what starts out as a red pixel next to a blue pixel may become a magenta pixel.
Either way, the first place to start is to load your image:
Image i = Image.FromFile(path);
然后分别查看每个像素:
Then look at each pixel individually:
int pixel = i.GetPixel(x, y);
然后,像素包含ARGB值.
The pixel then contains the ARGB value.
int Red = (pixel >> 16) & 0xFF;
int Green = (pixel >> 8) & 0xFF;
int Blue = pixel & 0xFF;
这篇关于VB.net中的颜色识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文