【问题标题】:Getting unexpected Pixels from Raw Image从原始图像中获取意外像素
【发布时间】:2014-01-06 21:22:10
【问题描述】:

我试图从游戏场景的某些像素中捕捉 R、G 和 B。为此,我创建了一个黑白位图图像。

此图像首先在 Init() 上加载,然后检查每个精灵移动是否确实是可用点。

问题是我在 R、G 和 B 处获得了意想不到的数据。我尝试了两个位图图像(8 位和 24 位)。它们都只有黑白像素。但是 r、g 和 b 一直告诉我这些像素是任何其他颜色。我认为“no_of_channels”应该是 3,因为我没有使用 alpha 通道,对吧?有任何想法吗?

应用程序.h

// background mask
UIImage* bgmask;
CGImageRef aCGImageRef;
CFDataRef rawData;
UInt8 * bgmaskbuf;

初始化():

    // BG Mask
    bgmask = [UIImage imageNamed:@"mask.bmp"];
    aCGImageRef = bgmask.CGImage;

    rawData = CGDataProviderCopyData(CGImageGetDataProvider(aCGImageRef));
    bgmaskbuf = (UInt8 *) CFDataGetBytePtr(rawData);

检查Pixel数据的方法:

-(BOOL) checkPixel: (CGFloat)x : (CGFloat)y{

BOOL result = FALSE;


//int length = CFDataGetLength(rawData);

//for(int i=0; i<length; i+=3)
//{
//    int r = bgmaskbuf[i];
//    int g = bgmaskbuf[i+1];
//    int b = bgmaskbuf[i+2];

//    NSLog(@"Ptr: %d, R: %d, G: %d, B: %d", i, r, g, b);
//}


int no_of_channels = 3;
int image_width = SCREEN_WIDTH();

unsigned long row_stride = image_width * no_of_channels; // 960 bytes in this case
unsigned long x_offset = x * no_of_channels;

/* assuming RGB byte order (as opposed to BGR) */
row_stride * (int)y + x_offset
int r = bgmaskbuf[next_pixel];
int g = bgmaskbuf[next_pixel + 1];
int b = bgmaskbuf[next_pixel + 2];
NSLog(@"Ptr: %d, R: %d, G: %d, B: %d",next_pixel r, g, b);

if((r==0)&&(g==0)&&(b==0)){
    result = TRUE;
}

    return result;
}

如何解决这个问题? 谢谢。

以下问题:

这是我试图解决这个问题的方法:

在像素检查时,我尝试运行内部的每个像素:

int length = CFDataGetLength(rawData);

for(int i=0; i<length; i+=3)
{
    int r = bgmaskbuf[i];
    int g = bgmaskbuf[i+1];
    int b = bgmaskbuf[i+2];

    NSLog(@"Ptr: %d, R: %d, G: %d, B: %d", i, r, g, b);
}

长度为 786432,这是有道理的(1024 * 768 像素)。我可以看到/读取所有像素,总共 2359296 字节(R + G + B)。

现在,奇怪的是,在处理用户的触摸和移动时,数据缓冲区索引(例如 793941)在地址 0x13200555 处为我提供 EXC_BAD_ACCESS。

当我尝试像这样阅读它时会发生这种情况:

row_stride * (int)y + x_offset
int r = bgmaskbuf[next_pixel];
int g = bgmaskbuf[next_pixel + 1];
int b = bgmaskbuf[next_pixel + 2];

bgmaskbuf 从 0x13240000 开始。

因此,从 0x13240000 到 0x13480000 的地址范围应该是可读的。

但我刚刚读到了同样的地址!

【问题讨论】:

  • 您看到的实际频道值是多少?
  • 几个样本:2014-01-06 07:25:26.190 color[3022:70b] R: 160, G: 182, B: 0 2014-01-06 07:25:26.244 color [3022:70b] R: 80, G: 111, B: 105 2014-01-06 07:25:26.247 颜色[3022:70b] R: 67, G: 84, B: 114 2014-01-06 07: 25:26.256 颜色[3022:70b] R: 32, G: 109, B: 97 2014-01-06 07:25:26.298 颜色[3022:70b] R: 116, G: 104, B: 0 2014-01 -06 07:25:26.300 颜色[3022:70b] R:116,G:67,B:111
  • 嗨乔希,请检查我的问题。我要求其他几个测试.. tks!

标签: ios objective-c bitmap core-foundation bytebuffer


【解决方案1】:

您需要检查一些值。行步幅实际上可能不仅仅是图像宽度和通道数。他们喜欢填充行以使它们保持在边界上。您应该能够从图像中获取该信息。要检查您可以查看 checkpixel 在顶部/底部行是否正常工作(一些图像也在内存中倒置)以查看值是否正确。

【讨论】:

  • 你好 Fade!我明白你的意思了!事实上,图像是颠倒的。我做了一些其他的测试..你能再检查一下我的问题吗?那里有额外的信息。谢了!
【解决方案2】:

什么对我真正有用:

  • 仅将位图图像保存为 1 位(最好和最简单的方法是 Ms Paint,我找不到 Mac 应用程序)。
  • 生成的蒙版确实从屏幕图像旋转了 180 度。

为此,我只使用了 1 个频道:

-(BOOL) checkPixel: (CGFloat)x : (CGFloat)y{

BOOL result = FALSE;

int no_of_channels = 1;
int image_width = SCREEN_WIDTH();

unsigned long row_stride = image_width * no_of_channels; // 960 bytes in this case
unsigned long x_offset = x * no_of_channels;


row_stride * (int)y + x_offset
int pixie = bgmaskbuf[next_pixel];

if(pixie==0)){
    result = TRUE;
}

我认为图像编辑更容易而不是代码旋转蒙版 =)

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2022-11-19
    • 2018-05-24
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多