`
jsntghf
  • 浏览: 2532994 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

简单的图像处理

    博客分类:
  • iOS
阅读更多

ImageUtil.h

 

#import <Foundation/Foundation.h>
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>

@interface ImageUtil : NSObject 

+ (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize;
+ (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize;

+ (UIImage *)blackWhite:(UIImage *)inImage;
+ (UIImage *)cartoon:(UIImage *)inImage;
+ (UIImage *)memory:(UIImage *)inImage;
+ (UIImage *)bopo:(UIImage *)inImage;
+ (UIImage *)scanLine:(UIImage *)inImage;

@end

 

ImageUtil.m

 

#import "ImageUtil.h"

#include <sys/time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

CGContextRef CreateRGBABitmapContext (CGImageRef inImage) {
	CGContextRef context = NULL; 
	CGColorSpaceRef colorSpace; 
	void *bitmapData; 
	int bitmapByteCount; 
	int bitmapBytesPerRow;
	size_t pixelsWide = CGImageGetWidth(inImage); 
	size_t pixelsHigh = CGImageGetHeight(inImage); 
	bitmapBytesPerRow = (pixelsWide * 4); 
	bitmapByteCount	= (bitmapBytesPerRow * pixelsHigh); 
	colorSpace = CGColorSpaceCreateDeviceRGB();
	if (colorSpace == NULL) {
		fprintf(stderr, "Error allocating color space\n"); return NULL;
	}
    
	bitmapData = malloc( bitmapByteCount ); 
	if (bitmapData == NULL) {
		fprintf (stderr, "Memory not allocated!"); 
		CGColorSpaceRelease( colorSpace ); 
		return NULL;
	}
	context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
	if (context == NULL) {
		free (bitmapData); 
		fprintf (stderr, "Context not created!");
	} 
	CGColorSpaceRelease(colorSpace); 
	return context;
}

unsigned char *RequestImagePixelData(UIImage *inImage) {
	CGImageRef img = [inImage CGImage]; 
	CGSize size = [inImage size];
	CGContextRef cgctx = CreateRGBABitmapContext(img); 
	
	if (cgctx == NULL) 
		return NULL;
	
	CGRect rect = {{0, 0}, {size.width, size.height}}; 
	CGContextDrawImage(cgctx, rect, img); 
	unsigned char *data = CGBitmapContextGetData (cgctx); 
	CGContextRelease(cgctx);
	return data;
}

#pragma mark -

@implementation ImageUtil

+ (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize {
	CGFloat scale;
	CGSize newsize;
	
	if(thisSize.width < aSize.width && thisSize.height < aSize.height) {
		newsize = thisSize;
	} else {
		if(thisSize.width >= thisSize.height) {
			scale = aSize.width / thisSize.width;
			newsize.width = aSize.width;
			newsize.height = thisSize.height * scale;
		} else {
			scale = aSize.height / thisSize.height;
			newsize.height = aSize.height;
			newsize.width = thisSize.width * scale;
		}
	}
	return newsize;
}

+ (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize {
	CGSize size = [ImageUtil fitSize:image.size inSize:viewsize];
	
	UIGraphicsBeginImageContext(size);
    
	CGRect rect = CGRectMake(0, 0, size.width, size.height);
	[image drawInRect:rect];
	
	UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();  
	
	return newimg;  
}

#pragma mark -

+ (UIImage *)blackWhite:(UIImage *)inImage {
	unsigned char *imgPixel = RequestImagePixelData(inImage);
	CGImageRef inImageRef = [inImage CGImage];
	GLuint w = CGImageGetWidth(inImageRef);
	GLuint h = CGImageGetHeight(inImageRef);
	
	int wOff = 0;
	int pixOff = 0;
	
	for(GLuint y = 0;y< h;y++) {
		pixOff = wOff;
		
		for (GLuint x = 0; x < w; x++) {
			int red = (unsigned char)imgPixel[pixOff];
			int green = (unsigned char)imgPixel[pixOff + 1];
			int blue = (unsigned char)imgPixel[pixOff + 2];
			
			int bw = (int)((red+green + blue) / 3.0);
			
			imgPixel[pixOff] = bw;
			imgPixel[pixOff + 1] = bw;
			imgPixel[pixOff + 2] = bw;
			
			pixOff += 4;
		}
		wOff += w * 4;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage *)cartoon:(UIImage *)inImage {
	unsigned char *imgPixel = RequestImagePixelData(inImage);
	CGImageRef inImageRef = [inImage CGImage];
	GLuint w = CGImageGetWidth(inImageRef);
	GLuint h = CGImageGetHeight(inImageRef);
	
	int wOff = 0;
	int pixOff = 0;
	
	for(GLuint y = 0;y < h;y++) {
		pixOff = wOff;
		
		for (GLuint x = 0; x < w; x++) {
			int red = (unsigned char)imgPixel[pixOff];
			int green = (unsigned char)imgPixel[pixOff + 1];
			int blue = (unsigned char)imgPixel[pixOff + 2];
			
			int ava = (int)((red + green + blue) / 3.0);
			
			int newAva = ava > 128 ? 255 : 0;
			
			imgPixel[pixOff] = newAva;
			imgPixel[pixOff + 1] = newAva;
			imgPixel[pixOff + 2] = newAva;
			
			pixOff += 4;
		}
		wOff += w * 4;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage *)memory:(UIImage *)inImage {
	unsigned char *imgPixel = RequestImagePixelData(inImage);
	CGImageRef inImageRef = [inImage CGImage];
	GLuint w = CGImageGetWidth(inImageRef);
	GLuint h = CGImageGetHeight(inImageRef);
	
	int wOff = 0;
	int pixOff = 0;
	
	for(GLuint y = 0;y < h;y++) {
		pixOff = wOff;
		
		for (GLuint x = 0; x < w; x++) {
			int red = (unsigned char)imgPixel[pixOff];
			int green = (unsigned char)imgPixel[pixOff + 1];
			int blue = (unsigned char)imgPixel[pixOff + 2];
			
			red = green = blue = ( red + green + blue ) / 3;
			
			red += red * 2;
			green = green * 2;
			
			if(red > 255)
				red = 255;
			if(green > 255)
				green = 255;
			
			imgPixel[pixOff] = red;
			imgPixel[pixOff + 1] = green;
			imgPixel[pixOff + 2] = blue;
			
			pixOff += 4;
		}
		wOff += w * 4;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage*)bopo:(UIImage*)inImage {
	unsigned char *imgPixel = RequestImagePixelData(inImage);
	CGImageRef inImageRef = [inImage CGImage];
	GLuint w = CGImageGetWidth(inImageRef);
	GLuint h = CGImageGetHeight(inImageRef);
	
	int i, j, m, n;
	int bRowOff;
	int width = 8;
	int height = 8;
	int centerW = width / 2;
	int centerH = height / 2;
    
	int modw = w % width;
	int modh = h % height;
	if(modw) w = w - modw;
	if(modh) h = h - modh;
	
	int br, bg, bb;
	int tr, tg, tb;
	
	double offset;
    
	NSMutableArray *wei = [[NSMutableArray alloc] init];
	for(m = 0; m < height; m++) {
		NSMutableArray *t1 = [[NSMutableArray alloc] init];
		for(n = 0; n < width; n++) {
			[t1 addObject:[NSNull null]];
		}
		[wei addObject:t1];
		[t1 release];
	}
	
	int total = 0;
	int max = (int)(pow(centerH, 2) + pow(centerW, 2));
	
	for(m = 0; m < height; m++) {
		for(n = 0; n < width; n++) {
			offset = max - (int)(pow((m - centerH), 2) + pow((n - centerW), 2));
			total += offset;
            
			[[wei objectAtIndex:m] insertObject:[NSNumber numberWithDouble:offset] atIndex:n];
		}
	}
	for(m = 0; m < height; m++) {
		for(n = 0; n < width; n++) {
			double newVal = [[[wei objectAtIndex:m] objectAtIndex:n] doubleValue] / total;
			[[wei objectAtIndex:m] replaceObjectAtIndex:n withObject:[NSNumber numberWithDouble:newVal]];
		}
	}
	bRowOff = 0;
	for(j = 0; j < h; j += height) {
		int bPixOff = bRowOff;
		
		for(i = 0; i < w; i += width) {
			int bRowOff2 = bPixOff;
			
			tr = tg = tb = 0;
			
			for(m = 0; m < height; m++) {
				int bPixOff2 = bRowOff2;
				
				for(n = 0; n < width; n++) {
					tr += 255 - imgPixel[bPixOff2];
					tg += 255 - imgPixel[bPixOff2 + 1];
					tb += 255 - imgPixel[bPixOff2 + 2];
					
					bPixOff2 += 4;
				}
				
				bRowOff2 += w * 4;
			}
			bRowOff2 = bPixOff;
			
			for(m = 0; m < height; m++) {
				int bPixOff2 = bRowOff2;
				for(n = 0; n < width; n++) {
					offset =  [[[wei objectAtIndex:m] objectAtIndex:n] doubleValue];
					br = 255 - (int)(tr * offset);
					bg = 255 - (int)(tg * offset);
					bb = 255 - (int)(tb * offset);
					
					if(br < 0)
						br = 0;
					if(bg < 0)
						bg = 0;
					if(bb < 0)
						bb = 0;
					imgPixel[bPixOff2] = br;
					imgPixel[bPixOff2 + 1] = bg;
					imgPixel[bPixOff2 + 2] = bb;
					
					bPixOff2 += 4; 
				}
				bRowOff2 += w * 4;
			}
			bPixOff += width * 4; 
		}
		bRowOff += w * height * 4;
	}
	[wei release];
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
	
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

+ (UIImage *)scanLine:(UIImage *)inImage {
	unsigned char *imgPixel = RequestImagePixelData(inImage);
	CGImageRef inImageRef = [inImage CGImage];
	GLuint w = CGImageGetWidth(inImageRef);
	GLuint h = CGImageGetHeight(inImageRef);
	
	int wOff = 0;
	int pixOff = 0;
	
	for(GLuint y = 0;y < h;y += 2) {
		pixOff = wOff;
		
		for (GLuint x = 0; x < w; x++) {
			int red = (unsigned char)imgPixel[pixOff];
			int green = (unsigned char)imgPixel[pixOff + 1];
			int blue = (unsigned char)imgPixel[pixOff + 2];
			
			int newR, newG, newB;
			int rr = red * 2;
			newR = rr > 255 ? 255 : rr;
			int gg = green * 2;
			newG = gg > 255 ? 255 : gg;
			int bb = blue * 2;
			newB = bb > 255 ? 255 : bb;
			
			imgPixel[pixOff] = newR;
			imgPixel[pixOff + 1] = newG;
			imgPixel[pixOff + 2] = newB;
			
			pixOff += 4;
		}
		wOff += w * 4 * 2;
	}
	
	NSInteger dataLength = w * h * 4;
	CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
    
	int bitsPerComponent = 8;
	int bitsPerPixel = 32;
	int bytesPerRow = 4 * w;
	CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
	CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
	CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
	CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
	
	UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
	
	CFRelease(imageRef);
	CGColorSpaceRelease(colorSpaceRef);
	CGDataProviderRelease(provider);
	return my_Image;
}

@end

 

效果图:


分享到:
评论

相关推荐

    用Python来完成简单图像处理.doc

    使用 Python 完成简单图像处理 本文主要介绍了使用 Python 完成简单图像处理的方法和相关技术。图像处理是计算机视觉和图像处理领域的重要组成部分,而 Python 语言则是完成简单图像处理任务的理想选择。下面将对...

    简单图像处理_汇编源码

    "简单图像处理_汇编源码"这个项目可能就是展示了如何利用汇编语言进行基本的图像操作。 汇编语言是计算机底层编程的一种语言,它直接对应于机器指令,每一个汇编指令都可以转化为一个具体的CPU操作。在图像处理中,...

    vc6.0简单图像处理应用程序

    本项目"vc6.0简单图像处理应用程序"提供了一个基于CView类的图像处理解决方案,这相较于常见的使用ScrollView的实现方式,更便于集成到其他项目中。 Visual C++ 6.0(简称VC6.0)是微软发布的一款经典的老版C++...

    基于MATLAB的简单图像处理系统的实现.pdf

    由于目前的图像处理软件往往将算法封装起来,不利于学习和理解图像处理知识,因此本文采用MATLAB作为开发平台,设计并实现了一个简单的数字图像处理系统。该系统以图形用户界面(GUI)为交互方式,功能模块分为采集、...

    opencv 001 简单图像处理

    本教程“opencv 001 简单图像处理”是针对初学者的入门指南,旨在帮助用户了解如何在Visual Studio 2015(VS2015)环境中使用OpenCV进行基本的图像操作。 首先,安装OpenCV是开始的第一步。这通常涉及下载OpenCV的...

    C++编写的简单图像处理系统

    本项目是基于C++编写的简单图像处理系统,其主要功能包括高斯平滑和拉普拉斯锐化,这些都是图像处理中常见的滤波技术。 高斯平滑,又称为高斯滤波,是一种线性滤波方法,主要用于消除图像中的噪声。它使用一个高斯...

    数字图像处理课设--简单图像处理系统

    3、数字图像的增强处理功能: (1)空域中的点运算、直方图的均衡化、各种空间域平滑算法(如局部平滑滤波法、中值滤波等)、锐化算法(如梯度锐化法、高通滤波等) (2)频域的各种增强方法:频域平滑、频域锐化、...

    实现简单图像处理,均值滤波

    实现简单图像处理,包括256色转灰度图、Hough变换、Walsh变换、中值滤波、二值化变换、亮度增减、傅立叶变换、反色、取对数、取指数、图像平移、图像旋转、图像细化、图像缩放、图像镜像、均值滤波、对比度拉伸、...

    简单图像处理 c++

    本项目"简单图像处理 c++"旨在提供一个基础的图像操作平台,用户可以进行图片的打开、显示和保存操作。下面我们将详细探讨相关的知识点。 1. 图像文件格式:图像文件有多种格式,如BMP(位图)、JPEG(联合图像专家...

    图像处理大作业——图像处理小工具箱(实验报告15000+字)

    设计并实现一个具有简单图像处理功能的小软件,并提供图形用户界面对其操作和交互。通过对该软件的设计,将实践与理论结合,从而使同学能更好地理解课程中的内容。 一、设计并实现图形化界面(选做,完成有额外加分...

    c# 源码--简单图像处理

    用c#开发的一个简单图像处理软件,功能如下: 1.图像文件的打开、保存、格式转换 2.图像的复制、粘贴 3.图像的彩色灰度转换、反色效果、图像的锐化、柔化、雾化处理 4.显示效果:百叶窗、翻转、上下拉伸、左右拉伸、...

    实现简单图像处理,方块编码

    实现简单图像处理,包括256色转灰度图、Hough变换、Walsh变换、中值滤波、二值化变换、亮度增减、傅立叶变换、反色、取对数、取指数、图像平移、图像旋转、图像细化、图像缩放、图像镜像、均值滤波、对比度拉伸、...

    基于matlab的简单图像处理.pdf

    【基于MATLAB的简单图像处理】是一篇关于利用MATLAB进行图像处理的课程设计报告,主要涉及了图像处理的基本操作和常见变换。MATLAB作为一款强大的科学计算软件,其图像处理工具箱为图像处理提供了便利。 一、设计...

    基于matlab实现图片进行颜色识别并进行简单图像处理

    基于matlab可以对图片进行颜色识别并进行简单图像处理 一、基本功能:读取本地图片、实时采集图片、图像处理、结果保存 二、颜色识别:利用设置的RGB阈值对图片颜色进行分类并提取 三、灰度化:利用RGB通用比例...

    WP7开发系列——Windows Phone 7平台简单图像处理(PDF)

    ### Windows Phone 7 平台简单图像处理技术详解 #### 一、概述 随着移动设备功能的日益增强,图像处理成为智能手机应用中的一个重要方面。Windows Phone 7 (WP7) 作为微软推出的一款操作系统,虽然在市场上已经...

    基于matlab的字符识别和简单图像处理

    本项目"基于MATLAB的字符识别和简单图像处理"是利用MATLAB进行图像预处理和字符识别的一个实例,主要涉及到以下几个核心知识点: 1. **图像二值化**:图像二值化是将图像上的像素点的灰度值设置为0或255,即将整个...

    matlab简单图像处理实验报告.doc

    总的来说,这个MATLAB简单图像处理实验涵盖了数字图像的基本类型和转换方法,有助于深入理解图像的存储原理和处理技术。通过实际操作,我们可以更好地掌握如何在MATLAB环境中进行图像处理,包括灰度图像、RGB图像和...

    简单图像处理

    本主题“简单图像处理”将引导初学者通过C#的Drawing类库来理解和实现基本的图像操作。C#作为.NET框架的一部分,提供了丰富的API来支持图形和图像处理,使得开发人员可以方便地进行图像的创建、读取、修改和显示。 ...

    简单图像处理软件

    利用matlab实现了一个简单的图像处理软件,提供用户图像界面,并生成.exe可执行文件。提供的功能包括:图像显示空间域变换(RGB、HSV、YCBCR空间)、灰度变换、旋转(翻转)、伽马校正及滤波复原、频率域滤波、创意...

Global site tag (gtag.js) - Google Analytics