如何从图像框中获取白色像素? [英] How to get white pixel from image box?
问题描述
海佬..
我是C#语言的初学者..
我有一个基于大小将水果分类的项目.
这是我的步骤:
1.从网络摄像头捕获图像.
2.将图像转换为灰度.
3.将图像转换为二进制图像.
4.计算白色像素以获取对象大小.
但是现在,我陷入了步骤4..
我无法计算图像框中的白色像素,
我的项目使用C#和emguCV作为库,
有人可以解决我的问题吗?如何计算图像框中的白色像素?
谢谢,
(对不起,我的英语不好:D)
-添加了注释中的格式化代码
这是将图像捕获转换为二进制图像的代码:
Hai guys..
I''m a beginner in C# language..
I have a project to classification fruits based on size..
This is my step :
1. Capture image from webcam.
2. Convert image to gray scale.
3. Convert image to binary image.
4. Counting white pixel to getting the object size.
But now, i get stuck on step 4..
I can''t count white pixel from image box,
My project using C# and emguCV as a library,
Anybody can solve my problem? How to count white pixel from image box?
Thanks,,
(Sorry for my bad English :D)
- Added formatted code from comment
This the code to convert image capture to binary image :
Image<gray,> imgTarget = hueFilter.And(saturationFilter).And(valueFilter);
imgTarget._Erode(1);
imgTarget._Dilate(1);
Image<gray,> bin = imgTarget.ThresholdBinary(new Gray(80), new Gray(255));
imageBox3.Image = bin.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR, true);
imgTarget.Dispose();
channels[0].Dispose();
channels[1].Dispose();
channels[2].Dispose();
但是我尝试使用此代码来计算白色像素值:
But i was try to count the white pixel value using this code :
Image<gray,> binary = new Image<gray,>(320, 240);
imageBox3.Image = binary;
for (int i = 0; i < binary.Rows; i++)
{
for (int j = 0; j < binary.Cols; j++)
{
binary.Data[i, j, 0] = 255;
byte white = binary.Data[i, j, 0];
lblWhite.Text = white + " white pixels";
}
当我运行此项目时,"lblWhite获得值= 255",但它没有计算白色像素..
And when i run this project the "lblWhite getting value = 255" , but it did''t count the white pixel..
Can you tell me what''s the problem?
推荐答案
首先,图像放大和腐蚀方法使用3x3像素网格,这会影响您的结果,但我认为这是故意的.
我会先使用3x3的白色图像开始,然后对侵蚀,膨胀,阈值和调整大小的操作进行注释.这样,您就有了基线来检查其余的代码.
ImageBox对我来说没有多大意义:最好只使用binary
.
您在任何地方都没有比较功能,我希望您将循环中的当前像素与白色像素进行比较,或者将亮度值与255比较.
[edit]
循环的内容对于计算白色像素是错误的,您有两个先行障碍:
Hi, first off the image dilate and erode methods use a 3x3 grid of pixels this will effect your results, but I assume this is deliberate.
I''d start off using a 3x3 white image to start with, and comment the erode, dilate, threshold and re-size operations. That way you have baseline to check the rest of your code.
ImageBox doesn''t make much sense to me: you''d be better off just usingbinary
.
You don''t have comparison anywhere, I''d expect you to compare the current pixel in your loop with a white one, or compare the brightness value with 255.
[edit]
the contents of loop is wrong for counting the white pixels, you have two preoblems:
binary.Data[i, j, 0] = 255; //This sets the pixel to white (I assume)
然后
then
byte white = binary.Data[i, j, 0];
获取刚刚设置为白色(255)的像素的值.
我认为您需要更多类似的东西:
Gets the value of the pixel, which you''ve just set to white (255).
I think you need something more like:
int count = 0;
for (int i = 0; i < binary.Rows; i++)
{
for (int j = 0; j < binary.Cols; j++)
{
if(binary.Data[i, j, 0] == 255)
count++;
lblWhite.Text = string.Format("{0} white pixels", count);
}
}
我发现的一件事是,在任何大小合适的图像中循环像素都是缓慢的,但是您只能优化一种基本算法正在工作的图像.
One thing I''ve found is that looping the pixels in any reasonably sized image is slow, but you should only optimise one your basic algorithm is working.
这篇关于如何从图像框中获取白色像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!