位图中的颜色百分比
- 作者: 当时场面很混乱__
- 来源: 51数据库
- 2022-10-20
问题描述
在不了解位图的情况下开始**
To get total pixels in bitmap height*Width To get total white pixels Where R==255 & B==255 & G==255 To get total black pixels Where R==0 & B==0 & G==0 To get total grey pixels where R=G=B
其余的将是应该给我的混合颜色.显然程序会运行数千次,所以我需要使用Lockbits.
Rest of them will be a mixed colours which should give me. Obviously the program will run for thousands of times,so I need to use Lockbits.
当前问题是结果不准确.请建议.尝试使用 aforge.net 或 imagemagick.net 库来检查它是否可以给出准确的结果
current problem is inaccurate result. pls suggest. Trying to use aforge.net or imagemagick.net libraries to check whether it can give accurate results
如何找到位图中的颜色像素百分比,最初位图对象来自PDF页面.我尝试使用 bitmap.getpixel() 它需要很长时间,LockBits 的性能更好,想知道使用 Lockbits 找到不包括黑色、白色和灰色的彩色像素的百分比.这是为了识别PDF文件中的彩色页面和打印特定页面的颜色使用.
How do I find the color pixels percentage in bitmap, originally the bitmap object is from PDF page. I tried with bitmap.getpixel() it is taking ages, LockBits is better in performance, Would like to know using Lockbits finding the percentage of color pixels excluding black, white and grey. This is to identify the colour pages in PDF file and colour usage for printing specific page.
我刚刚有一个代码来检测黑白像素的数量,我只是想利用这个代码来检测百分比,只需找到总像素,然后差异应该给我彩色像素,不确定方法是否正确!!
I have just got a code to detect the count of black and white pixels, I am just trying to make use of this code to detect percentage just by finding the total pixels and then the difference should give me colour pixels,not sure it is correct approach or not!!
public void ColourPercentage(Bitmap page, ref int nBlackCount, ref int nWhiteCount)
{
System.Drawing.Image image = null;
Bitmap bmpCrop = null;
BitmapData bmpData = null;
byte[] imgData = null;
int n = 0;
try
{
image = page;
bmpCrop = new Bitmap(image);
for (int h = 0; h < bmpCrop.Height; h++)
{
bmpData = bmpCrop.LockBits(new System.Drawing.Rectangle(0, h, bmpCrop.Width, 1),
System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
imgData = new byte[bmpData.Stride];
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, imgData, 0
, imgData.Length);
bmpCrop.UnlockBits(bmpData);
for (n = 0; n <= imgData.Length - 3; n += 3)
{
if ((int)imgData[n] == 000 && (int)imgData[n + 1] == 0 && (int)imgData[n + 2] == 000)// R=0 G=0 B=0 represents Black
{
nBlackCount++;
}
else if ((int)imgData[n] == 255 && (int)imgData[n + 1] == 255 && (int)imgData[n + 2] == 255) //R=255 G=255 B=255 represents White
{
nWhiteCount++;
}
else if ((int)imgData[n] == (int)imgData[n + 1] && (int)imgData[n + 1] == (int)imgData[n + 2])
nBlackCount++;
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
<小时>
public void blackwhiteCount(Bitmap page, ref int nBlackCount, ref int nWhiteCount)
{
System.Drawing.Color pixel;
try
{
for (int i = 0; i < page.Height; i++)
{
for (int j = 0; j < page.Width; j++)
{
pixel = page.GetPixel(i, j);
if (pixel.R == 0 && pixel.G == 0 && pixel.B == 0)
nBlackCount++;
else if (pixel.R == 255 && pixel.G == 255 && pixel.B == 255)
nWhiteCount++;
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show("Unable to parse image " + ex);
}
}
<小时>
ColourPercentage(page, ref nblack, ref nwhite);
double nTotal = page.Width * page.Height;
string blackper, whiteper, colourper;
double black =(double) nblack*100 / nTotal;
double white =(double) nwhite *100 / nTotal;
double colour = 100 - (black + white);
推荐答案
我找到了自己,为了方便浏览者重新使用,添加更正(问题已经更新了答案).
I have found myself, for viewers convenience for re usage, adding the correction(Question is updated with the answer already).
要获得总像素,请使用图像尺寸,而不是计算 bitlockdata 中的循环像素.
To get total pixels use image dimensions, instead of counting the looping pixels in bitlockdata.
Image page= new Image(filePath); double nTotal = page.Width * page.Height;
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
