2014-10-07 2 views
0

그리기 앱을 만들고 있는데, 사용자가 잊었거나 정리하지 않은 모든 픽셀의 목록이있는 NSArray가 있습니다. 원하는 것은 근접성을 사용하여 해당 목록 픽셀 그룹을 만드는 것입니다. 그래서 모든 검은 영역을 강조 표시 할 수 있습니다. 이것은 내가 잠시 동안 가지고 있지만 잘 그룹화하지 않습니다.Objective-C의 픽셀 그룹화

-(NSMutableArray*)orderMissingPixelsByGroups:(NSMutableArray*)missingPixels 
     { 
     NSMutableArray *finalArray=[[NSMutableArray alloc]init]; 
     NSMutableArray *auxArray = [[NSMutableArray alloc]init]; 

     for (int i=0; i<[missingPixels count]; i++) { 

      NSValue *value = [missingPixels objectAtIndex:i]; 
      if (i<[missingPixels count]-1) { 
      NSValue *nextValue = [missingPixels objectAtIndex:i+1]; 
      CGPoint point = value.CGPointValue; 
      CGPoint point2 = nextValue.CGPointValue; 
      if (abs(point.x-point2.x)<5) { 
      [auxArray addObject:[NSValue valueWithCGPoint:point]]; 
      } 
      else if (abs(point.y-point2.y)<5){ 
      [auxArray addObject:[NSValue valueWithCGPoint:point]]; 
      } 
     else 
     { 
      [auxArray addObject:[NSValue valueWithCGPoint:point]]; 
      [finalArray addObject:auxArray]; 
      auxArray = [[NSMutableArray alloc]init]; 
     } 

    } 
    else 
    { 
     [auxArray addObject:value]; 
     [finalArray addObject:auxArray]; 
    } 



    } 
    return finalArray; 
} 

조언이 있다면 매우 감사 할 것입니다.

답변

0

예이 방법이 최선의 방법이 아닐 수 있습니다. 이런 식으로 해보십시오. 참고 :이 방법을 최적화하기에는 너무 많은 공간이 있음에도 테스트하지는 못했지만 "모양"을 만드는 방법에 대한 이해를 돕기를 바랍니다. 그렇지 않으면 "블롭"키워드 아래에서 몇 가지 빠른 접근 방식을 검색 할 수 있습니다 ...

- (NSMutableArray *)createBlobsFromPixels:(NSMutableArray *)pixels { 
    NSMutableArray *initialBlobs = [[NSMutableArray alloc] init]; 
    for(NSValue *pixel in pixels) 
    { 
     [initialBlobs addObject:@[pixel]]; 
    } 

    while (YES) { 
     NSIndexSet *indexSet = [self joinBlobs:initialBlobs minimumRadius:5.0f]; 
     if(indexSet.count < 2) { 
      break; 
     } 
     else { 
      [self mergeBlobs:initialBlobs atIndexA:indexSet.firstIndex andIndexB:indexSet.lastIndex]; 
     } 
    } 

    return initialBlobs; 
} 

- (NSIndexSet *)joinBlobs:(NSMutableArray *)blobs minimumRadius:(CGFloat)minRadius { 
    // this method will try to join 2 blobs 
    // if successfull it will return an index set with 2 blob indices which should be joined 
    // if no blobs are able to join the method will return nil 

    if(blobs.count < 2) { 
     // can not join a single blub 
     return nil; 
    } 

    NSUInteger count = blobs.count; 
    // iterate through every pair 
    for(NSUInteger i=0; i<count-1; i++) { 
     for(NSUInteger j=i+1; j<count; j++) { 
      NSArray *blobA = blobs[i]; 
      NSArray *blobB = blobs[j]; 

      // check if any of the pixels from blob A are close enough from any pixel in blob B and merge those two blobs 

      for(NSValue *pixelA in blobA) { 
       for(NSValue *pixelB in blobB) { 
        CGPoint pointA = [pixelA CGPointValue]; 
        CGPoint pointB = [pixelB CGPointValue]; 
        if(fabsf(pointA.x-pointB.x)<minRadius && fabsf(pointA.y-pointB.y)<minRadius) { 
         // can join these 2 blobs 
         NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init]; 
         [indexSet addIndex:i]; 
         [indexSet addIndex:j]; 
         return indexSet; // returns YES as the blob can been joined 
        } 
       } 
      } 
     } 
    } 
    return nil; 
} 

- (void)mergeBlobs:(NSMutableArray *)blobs atIndexA:(NSUInteger)indexA andIndexB:(NSUInteger)indexB { 
    NSArray *blobA = blobs[indexA]; 
    NSArray *blobB = blobs[indexB]; 

    [blobs removeObject:blobA]; 
    [blobs removeObject:blobB]; 

    [blobs addObject:[blobA arrayByAddingObjectsFromArray:blobB]]; 
}