【发布时间】:2026-01-08 05:50:01
【问题描述】:
到目前为止,我正在逐个搜索每个像素,检查颜色并查看它是否为黑色......如果不是,我将继续下一个像素。这需要很长时间,因为我只能检查大约。每秒 100 像素(加速我的 NSTimer 会冻结应用程序,因为它检查速度不够快。)所以无论如何我可以让 Xcode 返回所有黑色像素并忽略其他所有内容,所以我只需要检查这些像素而不是每个像素。我正在尝试检测图像上最左侧的黑色像素。
这是我当前的代码。
- (void)viewDidLoad {
timer = [NSTimer scheduledTimerWithTimeInterval: 0.01
target: self
selector:@selector(onTick:)
userInfo: nil repeats:YES];
y1 = 0;
x1 = 0;
initialImage = 0;
height1 = 0;
width1 = 0;
}
-(void)onTick:(NSTimer *)timer {
if (initialImage != 1) {
/*
IMAGE INITIALLY GETS SET HERE... "image2.image = [blah blah blah];" took this out for non disclosure reasons
*/
initialImage = 1;
}
//image2 is the image I'm checking the pixels of.
width1 = (int)image2.size.width;
height1 = (int)image2.size.height;
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image2.CGImage));
const UInt32 *pixels = (const UInt32*)CFDataGetBytePtr(imageData);
if ( (pixels[(x1+(y1*width1))]) == 0x000000) { //0x000000 is black right?
NSLog(@"black!");
NSLog(@"x = %i", x1);
NSLog(@"y = %i", y1);
}else {
NSLog(@"val: %lu", (pixels[(x1+(y1*width1))]));
NSLog(@"x = %i", x1);
NSLog(@"y = %i", y1);
x1 ++;
if (x1 >= width1) {
y1 ++;
x1 = 0;
}
}
if (y1 > height1) {
/*
MY UPDATE IMAGE CODE GOES HERE (IMAGE CHANGES EVERY TIME ALL PIXELS HAVE BEEN CHECKED
*/
y1 = 0;
x1 = 0;
}
另外,如果一个像素非常接近黑色但不是完全黑色怎么办...我可以在某处添加一个误差范围,以便它仍然可以检测到 95% 黑色的像素吗?谢谢!
【问题讨论】:
标签: objective-c ios image colors pixel