女神鎮(zhèn)樓
我們都知道圖像美白大致有三種情況
- 自己圖片處理算法(操作具體的像素點(diǎn))
- 第三方開源庫(GPUImage杠愧。 openCV等等)
- 第三方框架(閉源驰弄,收費(fèi))(face++到涂,百度圖像處理框架等等)
今天我們就講解一下圖片處理算法美白府蛇,請(qǐng)看簡(jiǎn)單的效果圖
美白前
美白后
人物雖然丑了點(diǎn)判帮,但是我們不要注意這個(gè)細(xì)節(jié)轨奄,哈哈哈哈。
書接正文
我們創(chuàng)建一個(gè)image處理類ImageUtils
奠衔,里面有一個(gè)返回處理過后image
的方法+ (UIImage*)imageProcess:(UIImage*)image
第一步
首先我們需要先確定圖片的大小谆刨,獲取圖片大小有兩種方式
-
image.size.width
,image.size.height
-
CGImageGetWidth
,CGImageGetHeight
CGImageRef是基于像素的矩陣塘娶,每個(gè)點(diǎn)都對(duì)應(yīng)了圖片中點(diǎn)的像素信息归斤。CGImageRef是定義在QuartzCore框架中的一個(gè)結(jié)構(gòu)體指針,用C語言編寫刁岸。更加詳細(xì)的解析脏里,請(qǐng)看iOS中使用像素位圖(CGImageRef)對(duì)圖片進(jìn)行處理
獲取代碼
CGImageRef imageFef = image.CGImage;
NSUInteger width = CGImageGetWidth(imageFef);
NSUInteger height = CGImageGetHeight(imageFef);
第二步:開辟顏色內(nèi)存空間
開辟內(nèi)存空間的目的就是:讓我們可以獲取內(nèi)存空間的指針,然后根據(jù)指針找到像素點(diǎn)虹曙,操作像素點(diǎn)
創(chuàng)建顏色空間有兩種
- 灰色顏色空間
- 彩色顏色空間
灰色顏色空間沒辦法進(jìn)行美白迫横,只有彩色內(nèi)存空間才能根據(jù)RGB值的改變,來增加美白功能
代碼
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
第三步:創(chuàng)建圖片上下文
這是創(chuàng)建圖片上下文的方法酝碳,下面我們來解釋一下各個(gè)參數(shù)的意義
參數(shù)1
我們看第一個(gè)參數(shù)可知矾踱,這是一個(gè)指針,我們需要放置一個(gè)指針疏哗。
圖片其實(shí)就是一個(gè)像素?cái)?shù)組呛讲,他是一塊內(nèi)存空間,指針指向像素?cái)?shù)組的首地址(c/c++語言語法)
圖片使用像素?cái)?shù)組組成===》由每一個(gè)像素組成===》ARGB組成==》每一個(gè)分量組成(每一個(gè)分量大蟹捣睢:8位)
也就意味著一個(gè)像素點(diǎn)最大的大斜锤椤(4*8=32)
例如:一個(gè)像素點(diǎn)組成(ARGB。RGB芽偏。R雷逆,G,B污尉, BG)
我們不知道圖片每一個(gè)像素點(diǎn)的大小是多大膀哲,所以采用最大的
代碼
UInt32 *inputPixels = (UInt32*)calloc(width * height, sizeof(UInt32));
參數(shù)2:寬
參數(shù)3:高
參數(shù)4:每一個(gè)像素點(diǎn)往产,每一個(gè)分量的大小(固定式:8位)
參數(shù)5:每一行占用的內(nèi)存大小
1某宪、計(jì)算每一個(gè)像素點(diǎn)的大形嫫搿(大小ARGB 4*8 ==》沒8位=1字節(jié) ==》4字節(jié))
2、每一行的大小 = 像素點(diǎn) * 寬 = 4 * width
參數(shù)6:顏色空間
參數(shù)7:是否有透明度
bitmapInfo : 指定bitmap是否包含alpha通道缩抡,像素中alpha通道的相對(duì)位置奠宜,像素組件是整形還是浮點(diǎn)型等信息的字符串
CGImageAlphaInfo結(jié)構(gòu)體
typedef CF_ENUM(uint32_t, CGImageAlphaInfo) {
kCGImageAlphaNone, /* For example, RGB. /
kCGImageAlphaPremultipliedLast, / For example, premultiplied RGBA /
kCGImageAlphaPremultipliedFirst, / For example, premultiplied ARGB /
kCGImageAlphaLast, / For example, non-premultiplied RGBA /
kCGImageAlphaFirst, / For example, non-premultiplied ARGB /
kCGImageAlphaNoneSkipLast, / For example, RBGX. /
kCGImageAlphaNoneSkipFirst, / For example, XRGB. /
kCGImageAlphaOnly / No color data, alpha data only */
};
代碼
UInt32 *inputPixels = (UInt32*)calloc(width * height, sizeof(UInt32));
CGContextRef contextRef = CGBitmapContextCreate(inputPixels, width, height, 8, 4 * width, colorSpaceRef, kCGBitmapByteOrderDefault|kCGImageAlphaPremultipliedLast);
第四步:根據(jù)上下文繪制美白圖片
代碼
CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), imageFef);
第五步:開始美白
美白的原理:操作像素點(diǎn) ==》 操作分量 ==》 修改分量值(增或減少)
我們知道:美白處理其實(shí)就是增加RGB的各個(gè)值
0-255變化的趨勢(shì)是什么?越來越白
我們循環(huán)便利像素點(diǎn)瞻想,把每一個(gè)像素點(diǎn)的值都增加压真,結(jié)果就會(huì)出現(xiàn)美白效果。
在使用之前我們需要了解兩個(gè)概念:&
,>>
不知道的同學(xué)可以參考這篇文章
首先我們定義了一些宏
#define MaskB(x) ((x) & 0xFF)
#define R(x) (MaskB(x))
#define G(x) (MaskB(x >> 8))
#define B(x) (MaskB(x >> 16))
#define A(x) (MaskB(x >> 24))
#define RGBA(r,g,b,a) (MaskB(r) | MaskB(g)<<8 | MaskB(b) << 16 | MaskB(a) << 24)
我們先討論一些怎么獲取RGB
中的R
,其他的都是一樣的蘑险。
我們知道滴肿,像素點(diǎn)是一排一排的向下的,示意圖
我們首先獲取當(dāng)前圖片的像素點(diǎn)-->指針位移 inputPixels數(shù)組的首地址佃迄,不會(huì)變泼差。
UInt32 *currnetPixels = inputPixels + (i * width) + j;
然后獲取我們像素點(diǎn)對(duì)應(yīng)的顏色值(*取值 &取地址)
UInt32 color = *currnetPixels;
最后獲取
R
,G
,B
的值。
color 轉(zhuǎn)化為二進(jìn)制
進(jìn)行邏輯規(guī)則運(yùn)算(&:同為1 不同為0)
color = 111111 0011001 00010101 001000 (A R G B)呵俏,
代碼如下
//循環(huán)便利圖片像素點(diǎn)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//獲取當(dāng)前圖片的像素點(diǎn) -->指針位移 inputPixels數(shù)組的首地址堆缘,不會(huì)變
UInt32 *currnetPixels = inputPixels + (i * width) + j;
//獲取我們像素點(diǎn)對(duì)應(yīng)的顏色值(*取值 &取地址)
UInt32 color = *currnetPixels;
// NSLog(@"====%d====",color);
UInt32 thisR,thisG,thisB,thisA;
//定義亮度
int lumi = 50;
//如何獲取
//獲取紅色分量值(R)
/*
原理:
已知:color = 4290822858
0xFF 轉(zhuǎn)化為二進(jìn)制
color 轉(zhuǎn)化為二進(jìn)制
進(jìn)行邏輯規(guī)則運(yùn)算(&:同為1 不同為0)
color = 111111 0011001 00010101 001000 (A R G B)
*/
thisR = R(color);
thisR = thisR + lumi;
thisR = thisR > 255 ? 255 : thisR;
// NSLog(@"紅色值:%d",thisR);
//獲取綠色分量值
/*
第一步:進(jìn)行`為`運(yùn)算,向右移動(dòng)8位
第二部:獲取G的值普碎,進(jìn)行邏輯運(yùn)算(&)
color = 111111 0011001 00010101 001000 (A R G B)
第三部:
*/
thisG = G(color);
thisG = thisG + lumi;
thisG = thisG > 255 ? 255 : thisG;
thisB = B(color);
thisB = thisB + lumi;
thisB = thisB > 255 ? 255 : thisB;
thisA = A(color);
//修改像素點(diǎn)的值
*currnetPixels = RGBA(thisR, thisG, thisB, thisA);
}
}
第六步:創(chuàng)建UIImage
代碼
CGImageRef newImageRef = CGBitmapContextCreateImage(contextRef);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
第七步:釋放內(nèi)存
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(contextRef);
CGImageRelease(newImageRef);
free(inputPixels);
完整代碼
+ (UIImage*)imageProcess:(UIImage*)image{
//第一步:確定圖片的大小
CGImageRef imageFef = image.CGImage;
NSUInteger width = CGImageGetWidth(imageFef);
NSUInteger height = CGImageGetHeight(imageFef);
第二部:創(chuàng)建顏色空間(開辟內(nèi)存空間吼肥,目的:操作像素點(diǎn))
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
//第三部:創(chuàng)建圖片上下文
UInt32 *inputPixels = (UInt32*)calloc(width * height, sizeof(UInt32));
CGContextRef contextRef = CGBitmapContextCreate(inputPixels, width, height, 8, 4 * width, colorSpaceRef, kCGBitmapByteOrderDefault|kCGImageAlphaPremultipliedLast);
//第四步:根據(jù)上下文繪制美白圖片
CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), imageFef);
//第五部:正式開始美白
//循環(huán)便利圖片像素點(diǎn)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
//獲取當(dāng)前圖片的像素點(diǎn) -->指針位移 inputPixels數(shù)組的首地址,不會(huì)變
UInt32 *currnetPixels = inputPixels + (i * width) + j;
//獲取我們像素點(diǎn)對(duì)應(yīng)的顏色值(*取值 &取地址)
UInt32 color = *currnetPixels;
// NSLog(@"====%d====",color);
UInt32 thisR,thisG,thisB,thisA;
//定義亮度
int lumi = 50;
//如何獲取
//獲取紅色分量值(R)
thisR = R(color);
thisR = thisR + lumi;
thisR = thisR > 255 ? 255 : thisR;
// NSLog(@"紅色值:%d",thisR);
//獲取綠色分量值
thisG = G(color);
thisG = thisG + lumi;
thisG = thisG > 255 ? 255 : thisG;
thisB = B(color);
thisB = thisB + lumi;
thisB = thisB > 255 ? 255 : thisB;
thisA = A(color);
//修改像素點(diǎn)的值
*currnetPixels = RGBA(thisR, thisG, thisB, thisA);
}
}
//創(chuàng)建UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(contextRef);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
//第七步:釋放內(nèi)存
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(contextRef);
CGImageRelease(newImageRef);
free(inputPixels);
return newImage;
}