ios 将彩色照片转化成黑白等几种类型

-(UIImage *)changeColoursImageTograyScaleImage:(UIImage *)anImage type:(int)type

{

CGImageRef imageRef = anImage.CGImage;

size_t width                   = CGImageGetWidth(imageRef);

size_t height                  = CGImageGetHeight(imageRef);

size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);

size_t bitsPerPixel           = CGImageGetBitsPerPixel(imageRef);

size_t bytesPerRow         = CGImageGetBytesPerRow(imageRef);

CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);

CGBitmapInfo bitMapInfo      = CGImageGetBitmapInfo(imageRef);

bool shouldInterPolate               = CGImageGetShouldInterpolate(imageRef);

CGColorRenderingIntent intent   = CGImageGetRenderingIntent(imageRef);

CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef);

CFDataRef data                         = CGDataProviderCopyData(dataProvider);

UInt8 *buffer                            = (UInt8  *)CFDataGetBytePtr(data);

NSInteger x,y;

for (y = 0; y < height; y++) {

for (x = 0; x < width; x++) {

UInt8 *tmp;

tmp = buffer + y * bytesPerRow + x * 4;

UInt8 red,green,blue;

red     = *(tmp + 0);

green = *(tmp + 1);

blue   = *(tmp + 2);

UInt8 brightness;

switch (type) {

case 0:

brightness = (77 * red + 28 * green + 151 * blue) / 256;

*(tmp + 0) = brightness;

*(tmp + 1) = brightness;

*(tmp + 1) = brightness;

break;

case 1:

brightness = (77 * red + 28 * green + 151 * blue) / 256;

*(tmp + 0) = red;

*(tmp + 1) = green * 0.7;

*(tmp + 1) = blue *0.4;

break;

case 2:

brightness = (77 * red + 28 * green + 151 * blue) / 256;

*(tmp + 0) = 255 - red;

*(tmp + 1) = 255 - green;

*(tmp + 1) = 255 - blue;

break;

default:

*(tmp + 0) = red;

*(tmp + 1) = green;

*(tmp + 2) = blue;

break;

}

}

}

CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data));

CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData);

CGImageRef effectedCGImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, bitMapInfo, effectedDataProvider, NULL, shouldInterPolate, intent);

UIImage *effectedImage = [[UIImage alloc]initWithCGImage:effectedCGImage];

CGImageRelease(effectedCGImage);

CFRelease(effectedDataProvider);

CFRelease(effectedData);

CFRelease(data);

return effectedImage;

}

效果图:

时间: 2024-11-03 03:30:48

ios 将彩色照片转化成黑白等几种类型的相关文章

怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图?

  分类: OpenCV [Q1]怎么样用opencv将彩色图片转化成像素值只有0和255的灰度图? 进行灰度化,IplImage* pImg = cvLoadImage( "C:\\1.bmp", 0 ); 这样图像已经灰度化,然后调用cvThreshold(image, image, 125, 255, CV_THRESH_BINARY); 就可以了,125那里是你所用的阈值,这就是最简单的二值化,你要用ostu,或者别的高级一点的,就要自己写函数了   // Truncate v

彩色图转化成灰度图

对于图像由彩色图转化为灰度图有三种方法分别为 加权法 均值法 最大值法! 加权法就是  GRAY==0.3*R+0.59*G+0.11*B 均值法就是 GRAY==(R+G+B)/3 最大值发就是  max(R,G,B) A=imread('D:\lena.BMP');r=double(A(:,:,1));g=double(A(:,:,2));b=double(A(:,:,3));[m,n]=size(r);bw=zeros(m,n);bw=0.3.*r+0.59.*g+0.11.*b;subp

android 将图片转换成黑白图片

先调用android的系统工具,获取图片生成bitmap文件,再使用android中的二值化技术,将图片转化成黑白图片,主界面代码: package com.example.blackwhite; import java.io.IOException; import android.media.ThumbnailUtils; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore;

彩色照片转换为黑白照片(Color image converted to black and white picture)

This blog will be talking about the color image converted to black and white picture. The project structure as follow: The run result: ========================================================== source code: ===========================================

iOS开发之---将时间戳,转化成时间字符串。

1.将一个NSDate,转化成时间字符串. NSDate *date = [NSDate date]; NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"yyyy-MM-dd"; NSString *dateStr = [fmt stringFromDate:date];

iOS 文字转化成图片

//文字转化成图片 -(UIImage *)imageFromText:(NSArray*)arrContent withFont:(CGFloat)fontSize withTextColor:(UIColor *)textColor withBgImage:(UIImage *)bgImage withBgColor:(UIColor *)bgColor { // set the font type and size UIFont *font = [UIFont fontWithName:@

彩色图片渐变成灰色图片

在遇到英雄死亡需要让它变黑白的过程中参考ps中图片渐变成灰色的方式 解决思路是使用两张图片,一张彩色一张黑白,让彩色透明度随时间增长变0 网上有彩图变灰的代码如下 do { const GLchar* pszFragSource = "#ifdef GL_ES \n \ precision mediump float; \n #endif \n \ uniform sampler2D u_texture; \n varying vec2 v_texCoord; \n varying vec4 v

TMS320DM642中一种彩色图像转化成灰度图像算法

摘要:数字图像中,一般将其他格式图像转化成灰度图像,以使后续的图像计算量变少一些. 平台:Windows10+CCS5.5+TMS320DM642 实现: /* ======================================================================== */ /* Copyright 2006 by Wintech Digital System Technology Corp. */ /* All rights reserved. Prope

小tip: 使用CSS将图片转换成黑白(灰色、置灰) ? 张鑫旭-鑫空间-鑫生活

小tip: 使用CSS将图片转换成黑白(灰色.置灰) 这篇文章发布于 2012年08月19日,星期日,20:41,归类于 css相关, SVG相关. 阅读 159943 次, 今日 146 次 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=2547 //zxx: 最近很积极地折腾手机页面的些东西,加上其他一些人生重要的事,所以木有更新. 可能早就知道,像汶川这种糟糕