gpt4 book ai didi

ios - 从 IOS 中的 UIImagview 检测最左边的黑色点

转载 作者:行者123 更新时间:2023-11-29 02:56:19 35 4
gpt4 key购买 nike

我无法通过检测 UIImageView 找到最左边的点/帧/位置。我尝试从 UIImageView 中检测黑色像素,如下所示:

-(void)Black_findcolor
{
UIImage *image = imgview.image;//imgview from i want to detect the most black color pixels.

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
int myWidth = CGImageGetWidth(image.CGImage);
int myHeight = CGImageGetHeight(image.CGImage);
const UInt8 *pixels = CFDataGetBytePtr(pixelData);
UInt8 blackThreshold = 10 ;
// UInt8 alphaThreshold = 100;
int bytesPerPixel_ = 4;
CGFloat Xvalue,Yvalue;
int x = 0,y=0;
for( x = 0; x < myWidth; x++)
{
for( y = 0; y < myHeight; y++)
{
int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_;
UInt8 alphaVal = pixels[pixelStartIndex];
UInt8 redVal = pixels[pixelStartIndex + 1];
UInt8 greenVal = pixels[pixelStartIndex + 2];
UInt8 blueVal = pixels[pixelStartIndex + 3];

if(redVal < blackThreshold || blueVal < blackThreshold || greenVal < blackThreshold || alphaVal < blackThreshold)
{
NSLog(@"x =%d, y = %d", x, y);
}
}
}
}

从上面我得到了不同的 x 和 y 像素值,但我想把它作为点。所以我必须在最左边的黑色点的 UIImageView 上设置 pin。

imgPin1.center=CGPointMake(x, y);//i tried but didnt get succeed in it // imgpin1 is Global imageview.

那么谁能给我想法或建议如何设置 imgpin1 最左边黑色像素的框架或位置?

如果需要任何其他信息,请告诉我。

提前致谢。

最佳答案

已编辑请查看我编辑过的答案

    //You need to get resized image from your imageView in case of sizeToFit etc.
float oldWidth = yourImageView.image.size.width;
float scaleFactor = yourImageView.frame.size.width / oldWidth;

float newHeight = yourImageView.image.size.height * scaleFactor;
float newWidth = oldWidth * scaleFactor;

UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
[yourImageView.image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


CGSize size = newImage.size;
int width = size.width;
int height = size.height;
int xCoord = width, yCoord = height;
UInt8 blackThreshold = 10;


// raw data will be assigned into this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

// fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [yourImageView.image CGImage]);

for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];

// convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
uint32_t gray = 0.3 * rgbaPixel[1] + 0.59 * rgbaPixel[2] + 0.11 * rgbaPixel[3];

if (gray < blackThreshold)
{
if (x<xCoord)
{
xCoord = x;
yCoord = y;
NSLog(@"X:%d Y:%d",xCoord,yCoord);
}
}

}
}

// we're done with the context and pixels
CGContextRelease(context);
free(pixels);

imgPin1.center=CGPointMake(xCoord, yCoord);

请注意,如果您使用原始图像遍历所有像素并尝试将图钉显示到您的 UIImageView 上,您将获得 XY 像素在图像原始大小中的坐标,这将不对应于 UIImageView

中显示的 UIImage 的实际坐标

关于ios - 从 IOS 中的 UIImagview 检测最左边的黑色点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866473/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com