2013-10-28 40 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常量來解決。嘗試左或右,直到你得到你所需要的。

至於門檻不工作,你可以驗證你的「tollerance」真的是行爲像你期望的。將其更改爲255並查看整個圖像是否保留其顏色(應該)。如果它仍然是灰色的,那麼你知道你的條件陳述是問題所在。

+0

嗨瑞恩謝謝你的迴應,我試着將「tollerance」值替換爲255我得到整個圖像保留其顏色。 – user2922409

+0

太棒了!那麼我們知道一切正常,但是您對「紅色」的定義限制太強,無法處理您測試過的圖像。有很多有效的方法可以解決這個問題,但你可以嘗試的方法是獲取像素的紅色值並從中減去藍色和綠色。如果結果高於某個閾值,則爲「紅色」。你的第一種方法也沒有什麼問題,但是你需要微調「紅色」的定義,不管怎麼說都要寬容一點。 –

+0

它可能適用於您的應用程序,也可能不適合您,但您也可以將UISlider添加到界面,並在調整時設置閾值。 –