1
A
回答
0
你可以将它转换为8位灰度图像(见Convert UIImage to 8 bits,或见the link that Spynet shared包括其他排列),但我无法适应,要创建4位灰度位图(CGBitmapContextCreate
抱怨说,它不是一个有效的参数组合)。它看起来应该是理论上可行的(有012位的,pixel format types下列出的4位格式),但我无法让它工作这种技术。
+0
感谢您的回答,我会检查它。 –
0
我找到了将图像转换为4位灰度的解决方案,代码如下。
-(UIImage *)grayscaleImageAt4Bits:(UIImage *)_image
{
CGImageRef imageRef = [_image CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);
NSInteger _bitsPerComponent = 8;
NSInteger _bytesPerPixel = 1;
NSInteger _bytesPerRow = _bytesPerPixel * width;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
uint8_t *sourceData = malloc(height * width * _bytesPerPixel);
CGContextRef context = CGBitmapContextCreate(sourceData,
width,
height,
_bitsPerComponent,
_bytesPerRow,
colorSpace,
kCGImageAlphaNone);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
const int _redIndex = 0;
const int _greenIndex = 1;
const int _blueIndex = 2;
int _byteIndex = 0;
int _bitIndex = 0;
uint8_t *_newImageBits = malloc(height * width * _bytesPerPixel/2);
for(int j=0;j<height;j++)
{
for(int k=0;k<width;k++)
{
UInt8 _perPixel = _newImageBits[_byteIndex];
UInt8 *rgbPixel = (UInt8 *) &sourceData[j * width + k];
int _red = rgbPixel[_redIndex];
int _green = rgbPixel[_greenIndex];
int _blue = rgbPixel[_blueIndex];
int _pixelGrayValue = (_green + _red + _blue)/16;
UInt8 _pixelByte = (UInt8)_pixelGrayValue;
_perPixel |= (_pixelByte<<4);
_newImageBits[_byteIndex] = _perPixel;
_bitIndex += 4;
if(_bitIndex > 7)
{
_byteIndex++;
_bitIndex = 0;
}
}
}
free(sourceData);
//Direct fetch Bytes of 4-Bits, then you can use this Bytes (sourceData) to do something.
sourceData = _newImageBits;
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *_grayImage = [UIImage imageWithCGImage:cgImage];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
//Convert to NSData type, then you can use this NSData (_sourceImageData) to do something.
NSData *_sourceImageData = [NSData dataWithBytes:sourceData length:(width * height * _bytesPerPixel)];
free(sourceData);
return _grayImage;
}
而iOS只支持至少8位颜色显示,所以4位数据只传输到openGL的纹理使用。
+0
iOS 8,现在给出一个警告:'将枚举类型'enum CGImageAlphaInfo'隐式转换为不同的枚举类型'CGBitmapInfo'(又名'enum CGBitmapInfo')' –
相关问题
- 1. 如何将捕获的位图转换为8位图像作为灰度?
- 2. 将32bpp位图转换为16bpp(灰度)
- 3. 在.Net 2.0中将8位灰度tiff图像转换为8位灰度jpg
- 4. 如何将16位,TIFF,灰度图像转换为32位,TIFF,在Java中的灰度图像?
- 5. Android:将imageview转换为位图,灰度,将位图转换为imageview
- 6. 将图像转换为灰度图像
- 7. 转换8位灰度级图像以7位灰度级图像
- 8. 如何将16位灰度图像转换为java中的RGB图像?
- 9. 如何将位图图像转换为Windows Phone 7中的png?
- 10. 如何将图像转换为位图
- 11. 如何将位图转换为图像
- 12. 将PNG图像转换为像素灰度值特征向量
- 13. 将RGB图像转换为灰度和灰度转换为RGB图像?
- 14. 如何将32位RGBA图像转换为保存灰度图的alpha
- 15. 将位图(RGB_565)转换为灰度(8位)Android
- 16. 将GIF图像转换为PNG格式,32位深度
- 17. 如何将灰度位图转换为alpha蒙版?
- 18. 如何将short []转换为int []来创建灰度位图?
- 19. MATLAB/Opencv:将24位RGB图像转换为16位灰色
- 20. 如何将24位图像转换为16位rgb565位图?
- 21. J2ME:将透明PNG图像转换为灰度
- 22. 灰度PNG图像
- 23. 如何将RGB图像转换为灰度图像?
- 24. 如何将灰度图像转换为RGB图像?
- 25. 位图8位灰度图像在C
- 26. 在java中转换8位灰度RAW图像中的图像
- 27. 16位灰度png图像的Python图像处理
- 28. 将简单图像转换为灰度
- 29. 将图像转换为灰度
- 30. 将彩色图像转换为灰度
http://stackoverflow.com/questions/1298867/convert-image-to-grayscale – Spynet
谢谢,我会检查它,代码让我有些事情要做。 –