oc 图片变黑白

2024-09-06 11:58
文章标签 图片 oc 黑白

本文主要是介绍oc 图片变黑白,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

理论依据:
所谓颜色或灰度级指黑白显示器中显示像素点的亮暗差别,在彩色显示器中表现为颜色的不同,灰度级越多,图像层次越清楚逼真。灰度级取决于每个像素对应的刷新 存储单元的位数和显示器本身的性能。如每个象素的颜色用16位 二进制数表示,我们就叫它16位图,它可以表达2的16次方即65536种颜色。如每一个象素采用24位二进制数表示,我们就叫它24位图,它可以表达2的24次方即16777216种颜色。
灰度就是没有色彩,RGB色彩分量全部相等。如果是一个二值灰度图象,它的象素值只能为0或1,我们说它的灰度级为2。用个例子来说明吧:一个256级灰度的图象,如果RGB三个量相同时,如:RGB(100,100,100)就代表灰度为100,RGB(50,50,50)代表灰度为50。
彩色图象的灰度其实在转化为黑白图像后的像素值(是一种广义的提法),转化的方法看应用的领域而定,一般按加权的方法转换,R, G,B 的比一般为3:6:1。
任何颜色都由红、绿、蓝 三原色组成,假如原来某点的颜色为RGB(R,G,B),那么,我们可以通过下面几种方法,将其转换为灰度:
1. 浮点 算法:Gray=R*0.3+G*0.59+B*0.11
2.整数方法:Gray=(R*30+G*59+B*11)/100
3.移位方法:Gray =(R*77+G*151+B*28)>>8;
4. 平均值法:Gray=(R+G+B)/3;
5.仅取绿色:Gray=G;
通过上述任一种方法求得Gray后,将原来的RGB(R,G,B)中的R,G,B统一用Gray替换,形成新的颜色RGB(Gray,Gray,Gray),用它替换原来的RGB(R,G,B)就是灰度图了。

综上所述,就有了面两种方法,其实也就算一种吧,仅供大家参考。

方法1:(经过测试,该方法会有黑色底问题,有空研究下为什么)
//UIImage: 去色功能的实现(图片灰色显示)
- ( UIImage *)grayImage:( UIImage *)sourceImage {
   
  int  width = sourceImage. size . width ;
   
  int  height = sourceImage. size . height ;
   
  CGColorSpaceRef  colorSpace =  CGColorSpaceCreateDeviceGray ();
   
  CGContextRef  context =  CGBitmapContextCreate  ( nil , width, height, 8 , 0 , colorSpace, kCGImageAlphaNone );
   
  CGColorSpaceRelease (colorSpace);
   
  if  (context == NULL ) {
       
  return  nil ;
    }
   
  CGContextDrawImage (context, CGRectMake ( 0 , 0 , width, height), sourceImage. CGImage );
   
  UIImage  *grayImage = [ UIImage imageWithCGImage : CGBitmapContextCreateImage (context)];
   
  CGContextRelease (context);
   
  return  grayImage;
}

方法2
- ( UIImage *)grayscaleImageForImage:( UIImage *)image {
   
  // Adapted from this thread: http://stackoverflow.com/questions/1298867/convert-image-to-grayscale
   
  const  int  RED = 1 ;
   
  const  int  GREEN = 2 ;
   
  const  int  BLUE = 3 ;
   
   
  // Create image rectangle with current image width/height
   
  CGRect  imageRect =  CGRectMake ( 0 , 0 , image. size . width * image. scale , image. size . height * image. 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), [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
           
  uint32_t  gray =  0.3  * rgbaPixel[RED] + 0.59  * rgbaPixel[GREEN] + 0.11  * rgbaPixel[BLUE];
           
           
  // set the pixels to gray
            rgbaPixel[RED] = gray;
            rgbaPixel[GREEN] = gray;
            rgbaPixel[BLUE] = gray;
        }
    }
   
   
  // create a new CGImageRef from our context with the modified pixels
   
  CGImageRef  imageRef =  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 :imageRef scale :image. scale orientation : UIImageOrientationUp ];
   
   
  // we're done with image now too
   
  CGImageRelease (imageRef);
   
   
  return  resultUIImage;
}




这篇关于oc 图片变黑白的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1141937

相关文章

使用opencv优化图片(画面变清晰)

文章目录 需求影响照片清晰度的因素 实现降噪测试代码 锐化空间锐化Unsharp Masking频率域锐化对比测试 对比度增强常用算法对比测试 需求 对图像进行优化,使其看起来更清晰,同时保持尺寸不变,通常涉及到图像处理技术如锐化、降噪、对比度增强等 影响照片清晰度的因素 影响照片清晰度的因素有很多,主要可以从以下几个方面来分析 1. 拍摄设备 相机传感器:相机传

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

Spring MVC 图片上传

引入需要的包 <dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-

Prompt - 将图片的表格转换成Markdown

Prompt - 将图片的表格转换成Markdown 0. 引言1. 提示词2. 原始版本 0. 引言 最近尝试将图片中的表格转换成Markdown格式,需要不断条件和优化提示词。记录一下调整好的提示词,以后在继续优化迭代。 1. 提示词 英文版本: You are an AI assistant tasked with extracting the content of

研究人员在RSA大会上演示利用恶意JPEG图片入侵企业内网

安全研究人员Marcus Murray在正在旧金山举行的RSA大会上公布了一种利用恶意JPEG图片入侵企业网络内部Windows服务器的新方法。  攻击流程及漏洞分析 最近,安全专家兼渗透测试员Marcus Murray发现了一种利用恶意JPEG图片来攻击Windows服务器的新方法,利用该方法还可以在目标网络中进行特权提升。几天前,在旧金山举行的RSA大会上,该Marcus现场展示了攻击流程,

恶意PNG:隐藏在图片中的“恶魔”

&lt;img src=&quot;https://i-blog.csdnimg.cn/blog_migrate/bffb187dc3546c6c5c6b8aa18b34b962.jpeg&quot; title=&quot;214201hhuuhubsuyuukbfy_meitu_1_meitu_2.jpg&quot;/&gt;&lt;/strong&gt;&lt;/span&gt;&lt;

PHP抓取网站图片脚本

方法一: <?phpheader("Content-type:image/jpeg"); class download_image{function read_url($str) { $file=fopen($str,"r");$result = ''; while(!feof($file)) { $result.=fgets($file,9999); } fclose($file); re

(入门篇)JavaScript 网页设计案例浅析-简单的交互式图片轮播

网页设计已经成为了每个前端开发者的必备技能,而 JavaScript 作为前端三大基础之一,更是为网页赋予了互动性和动态效果。本篇文章将通过一个简单的 JavaScript 案例,带你了解网页设计中的一些常见技巧和技术原理。今天就说一说一个常见的图片轮播效果。相信大家在各类电商网站、个人博客或者展示页面中,都看到过这种轮播图。它的核心功能是展示多张图片,并且用户可以通过点击按钮,左右切换图片。

matplotlib绘图中插入图片

在使用matplotlib下的pyplot绘图时,有时处于各种原因,需要采用类似贴图的方式,插入外部的图片,例如添加自己的logo,或者其他的图形水印等。 一开始,查找到的资料都是使用imshow,但是这会有带来几个问题,一个是图形的原点发生了变化,另外一个问题就是图形比例也产生了变化,当然最大的问题是图形占据了整个绘图区域,完全喧宾夺主了,与我们设想的只在绘图区域中占据很小的一块不相符。 经

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注