1

白黒画像で動作するアプリケーションを書いています。私は NSImage オブジェクトをメソッドに渡し、NSImage から NSBitmapImageRep を作成しています。すべて動作しますが、かなり遅いです。これが私のコードです:

- (NSImage *)skeletonization: (NSImage *)image
{
    int x = 0, y = 0;
    NSUInteger pixelVariable = 0;

    NSBitmapImageRep *bitmapImageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];

    [myHelpText setIntValue:[bitmapImageRep pixelsWide]];
    [myHelpText2 setIntValue:[bitmapImageRep pixelsHigh]];

    NSColor *black = [NSColor blackColor];
    NSColor *white = [NSColor whiteColor];
    [myColor set];
    [myColor2 set];

    for (x=0; x<=[bitmapImageRep pixelsWide]; x++) {
        for (y=0; y<=[bitmapImageRep pixelsHigh]; y++) {
            // This is only to see if it's working
            [bitmapImageRep setColor:myColor atX:x y:y];
        }
    }

    [myColor release];
    [myColor2 release];

    NSImage *producedImage = [[NSImage alloc] init];
    [producedImage addRepresentation:bitmapImageRep];
    [bitmapImageRep release];

    return [producedImage autorelease];
}

だから私はCIImageを使用しようとしましたが、(x、y)座標で各ピクセルに入る方法がわかりません。それは本当に重要です。

4

1 に答える 1

0

NSImageの配列プロパティを使用してrepresentations、NSBitmapImageRep を取得します。画像を TIFF にシリアル化してから戻すよりも高速です。

bitmapDataのプロパティを使用してNSBitmapImageRep、イメージ バイトに直接アクセスします。

例えば

unsigned char black = 0;
unsigned char white = 255;

NSBitmapImageRep* bitmapImageRep = [[image representations] firstObject];
// you will need to do checks here to determine the pixelformat of your bitmap data
unsigned char* imageData = [bitmapImageRep bitmapData];

int rowBytes = [bitmapImageRep bytesPerRow];
int bpp = [bitmapImageRep bitsPerPixel] / 8;

for (x=0; x<[bitmapImageRep pixelsWide]; x++) {  // don't use <= 
    for (y=0; y<[bitmapImageRep pixelsHigh]; y++) {

       *(imageData + y * rowBytes + x * bpp ) = black; // Red
       *(imageData + y * rowBytes + x * bpp +1) = black;  // Green
       *(imageData + y * rowBytes + x * bpp +2) = black;  // Blue
       *(imageData + y * rowBytes + x * bpp +3) = 255;  // Alpha
    }
}

データを操作する前に、画像で使用しているピクセル形式を知る必要がありbitsPerPixelます。画像が RGBA 形式であるかどうかを判断するには、NSBitmapImageRep のプロパティを参照してください。

グレー スケール画像、RGB 画像、または CMYK で作業している可能性があります。そして、最初に画像を必要なものに変換します。または、ループ内のデータを別の方法で処理します。

于 2020-07-05T12:35:21.823 に答える