2013-10-28 3 views
0

부분 회색조 이미지를 만들려고합니다. 이미지의 각 픽셀을 모두 읽고 회색 픽셀 색을 회색 색으로 바꾸고, 픽셀 색이 원하는 색상 나는 그것이 특정 픽셀의 색상이 변경되지 않도록 적용되도록 제한합니다. 내가 어디로 가야할지 모르겠지만 그것은 회색 스케일로 전체 이미지를 변경하고 이미지를 90도 회전시킵니다. 사전에이 문제와 관련하여 도움을받을 수있는 사람이 있습니까?부분 회색조 이미지를 만들려고합니다.

-(UIImage *) toPartialGrayscale{ 
const int RED = 1; 
const int GREEN = 2; 
const int BLUE = 3; 

initialR=255.0; 
initialG=0.0; 
initialB=0.0;//218-112-214//0-191-255 
float r; 
float g; 
float b; 
tollerance=50; 

// Create image rectangle with current image width/height 
CGRect imageRect = CGRectMake(0, 0, originalImageView.image.size.width * scale, originalImageView.image.size.height * scale); 

int width = imageRect.size.width; 
int height = imageRect.size.height; 

// the pixels will be painted to 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); 

// paint the bitmap to our context which will fill in the pixels array 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [originalImageView.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 
     uint8_t gray = (uint8_t) ((30 * rgbaPixel[RED] + 59 * rgbaPixel[GREEN] + 11 * rgbaPixel[BLUE])/100); 

     // set the pixels to grayi 

     r= initialR-rgbaPixel[RED]; 
     g= initialG-rgbaPixel[GREEN]; 
     b= initialB-rgbaPixel[BLUE]; 

     if ((r<tollerance&&r>-tollerance)&&(g<tollerance&&g>-tollerance)&&(b<tollerance&&b>-tollerance)) 
     { 
      rgbaPixel[RED] = (uint8_t)r; 
      rgbaPixel[GREEN] = (uint8_t)g; 
      rgbaPixel[BLUE] = (uint8_t)b; 
     } 
     else 
     { 
      rgbaPixel[RED] = gray; 
      rgbaPixel[GREEN] = gray; 
      rgbaPixel[BLUE] = gray; 
     } 


    } 
} 

// create a new CGImageRef from our context with the modified pixels 
CGImageRef image = CGBitmapContextCreateImage(context); 

// we're done with the context, color space, and pixels 
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 
free(pixels); 

// make a new UIImage to return 
UIImage *resultUIImage = [UIImage imageWithCGImage:image 
              scale:scale 
             orientation:UIImageOrientationUp]; 

// we're done with image now too 
CGImageRelease(image); 

return resultUIImage; 
} 

이것은 모든 종류의 도움을 사용하고있는 코드입니다. 다시 한 번 감사드립니다.

+0

일부 코드는 오류를 식별하는 데 도움이됩니다. – Vignesh

+0

방금 ​​내 코드를 게시했습니다. vignesh 즉각적인 응답을 주셔서 감사합니다. – user2922409

답변

0

오리엔테이션 조각은 최종 이미지를 만들 때 전달하는 UIImageOrientationUp 상수로 재생하면 쉽게 해결할 수 있습니다. 필요한 것을 얻을 때까지 왼쪽이나 오른쪽으로 시도하십시오.

작동하지 않는 임계 값에 대해서는 "어깨 끈"이 실제로 예상대로 작동하는지 확인할 수 있습니까? 255로 변경하고 전체 이미지가 색상을 유지하는지 확인해야합니다. 회색이면 여전히 조건문이 문제가있는 부분임을 알 수 있습니다.

+0

안녕하세요 ryan이 응답 해 주신 데 대해 감사 드리며, 255로 "tollerance"값을 대체하려고 시도했습니다. 전체 이미지가 그 색상을 유지하게되었습니다. – user2922409

+0

굉장! 모든 것이 제대로 작동하고 있지만 "Red"에 대한 정의가 지나치게 제한되어 테스트 한 이미지에서 작업 할 수 없습니다. 이것에 대한 많은 접근법이 있지만, 시도해 볼 수있는 것은 픽셀의 빨강 값을 가져 와서 파랑과 녹색을 빼는 것입니다. 결과가 임계 값 이상이면 "빨간색"입니다. 첫 번째 접근법에도 아무 문제가 없었지만 관계없이 "Red"의 정의를 미세 조정해야합니다. –

+0

앱에 적합할지 여부는 다를 수 있지만 인터페이스에 UISlider를 추가하고 조정시 임계 값을 설정할 수도 있습니다. –

관련 문제