0

次の方法を使用して、入力画像をグレースケールとしきい値に変換しました。

UIImage *image = self.imageView.image;

    // Create image rectangle with current image width/height

    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    NSLog(@"width %f, height %f", image.size.width, image.size.height  );

    // Grayscale color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    // Create bitmap content with current image size and grayscale colorspace
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

    // Draw image into current context, with specified rectangle
    // using previously defined context (with grayscale colorspace)
    CGContextDrawImage(context, imageRect, [image CGImage]);

    // Create bitmap image info from pixel data in current context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    // Create a new UIImage object
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];

    // Release colorspace, context and bitmap information
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);

    // Return the new grayscale image
    self.imageView.image= newImage;




    //Thresholds the grayscale image



    CGImageRef sourceImage = newImage.CGImage ; //creates a CGImage reference for it

    //
    CFDataRef theData; //creates a variable of CFDataRef to store data of the image.
    //
    //
    //
    theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage)); //assigns theData variable to the actual image
    //
    //
    //
    //
    //
    //
    //
    //
    UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);
    //

    int dataLength = CFDataGetLength(theData);
    int counter=0;


    for (int index = 0; index < dataLength; index += 4)
    {

        if (pixelData[index]  < 180)
        {
               NSLog(@"The intensity is %u", pixelData[index]);
            pixelData[index] = 0;
            //pixelData[index+1] = 0;
            //pixelData[index+2] = 0;
            //pixelData[index+3] = 0;

        }

        else
        {
            NSLog(@"The intensity is %u", pixelData[index]);
            pixelData[index] = 255;
           // pixelData[index+1] = 255;
           // pixelData[index+2] = 0;
            //pixelData[index+3] = 0;
        }





    }

for ループが次の強度を書き換えようとすると、アプリがクラッシュします。

pixelData[index] = 0;

誰かが私を助けてくれませんか?

ありがとう!

4

1 に答える 1

1

CFDataGetBytePtrApple docsが言うように、読み取り専用ポインターを返します。ここで解決策を提案します

于 2013-06-17T10:42:20.210 に答える