- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 GetPixel 获取图像每个像素的颜色。图像包含不同的素色不规则形状,我想找到最大宽度与最大高度匹配的点(或像素)(见下图)。
(来源:fuskbugg.se)
(无视边界)
我正在使用它来遍历捕获的位图:
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color clr = bmp.GetPixel(x, y);
// Hit
if (TestColour(clr)) // See if we're within the shape. I'm just comparing a bunch of colours here.
{
// Stuff I don't know
}
}
}
我通过使用哈希表让它工作,但我知道这是一个糟糕的解决方案。我在考虑只让两个整数(一个用于 X,一个用于 Y)递增并保存每次迭代的最大值,然后将其与前一个进行比较,如果值更高则替换该值。
我不明白我如何能够将这种方法用于像那样嵌套的 for 循环。
有什么意见吗?
最佳答案
使用两个循环找到这个点应该很简单,类似于您的循环。首先,定义您的变量:
//from http://www.artofproblemsolving.com/Wiki/images/a/a3/Convex_polygon.png
Image image = Image.FromFile(@"C:\Users\Jacob\Desktop\Convex_polygon.png");
Bitmap bitmap = new Bitmap(image);
Point maxPoint = new Point(0, 0);
Size maxSize = new Size(0, 0);
接下来,我建议每个像素只调用一次 GetPixel
,并将结果缓存在一个数组中(这可能是我必须使用 API 调用来获取像素时的偏差,但是将证明更容易使用):
Color[,] colors = new Color[bitmap.Width, bitmap.Height];
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
colors[x, y] = bitmap.GetPixel(x, y);
}
}
接下来,这是获取最大高度的简单代码,以及具有该高度的第一个点的 X:
Color shapeColor = Color.FromArgb(245, 234, 229);
for (int x = 0; x < bitmap.Width; x++)
{
int lineHeight = 0;
for (int y = 0; y < bitmap.Height; y++)
{
if (colors[x, y] == shapeColor) // or TestColour(colors[x, y])
lineHeight++;
}
if (lineHeight > maxSize.Height)
{
maxSize.Height = lineHeight;
maxPoint.X = x;
}
}
您可以为每个 y 做一个类似的循环来找到最大宽度。
这里有一点很重要:您的问题没有针对凹形定义 - 在凹形上,每个 x 都有一个高度列表,并且最大高度线可能不会与最大宽度相交。即使在凸形上,您也可能有不止一个答案:矩形就是一个简单的例子。
关于c# - 使用 GetPixel() 查找彩色区域的最大宽度和最大高度相交的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3325723/
我正在编写一个 Android 应用程序,它使用 getPixel 读取位图的像素,并且它正在工作,只是它使用了大量的计算能力。因此,我尝试使用 getPixels 代替,并将所有整数保存到整数数组
我正在为我的 Bitmap 使用方法 getPixel 和 setPixel 并且它太慢了(getPixels 也是).我想处理 Bitmap 的每个像素,然后创建另一个 Bitmap。如何通过 Re
无论它变成什么颜色,值检查都保持不变。它应该每秒给我精确像素的 RGB 值 我尝试以递归的方式进行,简化它,在多个对象上进行测试,结果保持不变。 im = pg.screenshot() import
我正在使用一个 ImageView ,我想知道其中的像素颜色,所以我正在使用 getpixel()...方法但它向我展示了一个错误....这就是我使用获取像素方法的方式.. image = Bitm
我在我的 Android 应用程序中使用此代码将位图转换为纯黑白,它可以工作: public Bitmap ConvertToThreshold(Bitmap anythingBmap) {
好吧,这很容易理解,但是由于一些奇怪的原因我无法让它工作。我已经从实际代码中简化了这个例子。 InputStream is = context.getResources().openRawResour
这是我在这个论坛上的第一篇帖子,如果我发错地方或者做错了,请不要责备我,我很少在论坛发帖。所以,我对 GetPixel 函数有这个问题。基本上,它应该返回 x、y 处的颜色小数。我即将发布的代码在 w
直接看代码。鉴于 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId); Drawable drawable = ge
正如标题所示,调用 Bitmap.GetPixel 不会产生任何结果。没有为变量赋值,也没有抛出异常。 这是我的测试代码: private void frmStatTracker_Load(objec
在我尝试编写一些代码时,收到此错误: Error 1 'System.Drawing.Image' does not contain a definition for 'GetPixel' and n
我需要组合来自两个 WriteableBitmap 对象的两个颜色值并用它们计算一些东西。因此,我在第一个对象上运行 ForEach 循环,并将其颜色值和第二个对象的颜色值解析为一个方法。 write
现在我有一个完全是空白的黄色窗口。由于某种原因,当我调用 GetPixel() 时,我只得到 0xFFFFFFFF (CLR_INVALID)。这是我的最小化代码: HWND _hwndPP = F
我编写此代码是为了消除视频中因相机故障而导致的恼人模式。问题是要对 2 分钟的视频进行编码,此算法需要超过 2 小时。我想显着减少所需的时间。 该算法遍历每个图像,从那里开始查看每 4 个像素,创建平
我在从生成的位图中读回像素值时遇到了一些问题。我首先使用以下代码在我的类中生成一个名为 maskBitmap 的位图: void generateMaskBitmap() {
我是使用 Android 的新手,现在已经为这个问题努力了四天。我非常感谢有人的帮助。 我在 ImageView 中有一张图片,我想获取用户触摸的图片部分的颜色。为此,我正在使用 Bitmap.get
我创建了一个代表汽车的图像,我用不同的颜色为汽车的每个组件着色,例如引擎盖的 RGB 颜色为 251,252,252。 此图像通过 TileView 显示在屏幕上,我需要实现一个函数来获取我触摸的像素
我正在尝试制作一个非常简单的图 block 引擎。但是,使用 Bitmap.getpixel(x,y)并不总是正确匹配颜色。 0xFFFFFFFF 似乎还不错, 0xFF000000 , 0xFF18
我正在使用 Bitmap.getPixels使用下面的代码在位图图像中间获取一行 1 像素高度: int width = source.getWidth(); int height = source.
我正在创建一个涉及获取部分屏幕颜色的应用程序。为此,我使用 Bitmap.getPixel 方法检索屏幕的指定像素,然后将其转换为 RGB 格式,以便以后更轻松地进行编码。问题是无论我使用 getPi
我如何用更快的东西替换 GetPixel()? 目前我正在使用: temp = GetPixel(hMonitor, 1, 1); if (pixelArray[0] != temp) { p
我是一名优秀的程序员,十分优秀!