Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-12-2009, 01:52 PM   #1 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 6
Question Share your Image Proccessing (filter) code

Would anyone out there be willing to share there implemention of some image proccessing code.

If you could include all 3 important steps that would be amazing

1. access raw RGBA image data
2. apply any image proccessing (edge detect, greyscale, contrast) What filter isn't as important since I intend to use my own once I have a framework to work from.
3. create a new UIImage etc from the modified data

Thanks a million for any help
afastrunner is offline   Reply With Quote
Old 02-12-2009, 01:54 PM   #2 (permalink)
Senior Member
 
Join Date: Jan 2009
Location: Florida, USA
Posts: 471
Default

There are plenty of books on the topic
Slayer5150 is offline   Reply With Quote
Old 02-12-2009, 02:00 PM   #3 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 6
Default

Quote:
Originally Posted by Slayer5150 View Post
There are plenty of books on the topic
As I said it's not about the filter implmentation. I have books on Image Proccessing. The issue is I can't seem to get it all to come togther on the iphone.

That's why I'd like the most basic example (like grayscale) so I have something to work from. I found articles on getting image data, and articles on creating images and all the pieces (though haven't seen any image proccessing code on here or elsewhere) but can't get the steps to play nicely togther.

I'd show you what I have so far but i'm at work and code is at home.
afastrunner is offline   Reply With Quote
Old 02-12-2009, 02:10 PM   #4 (permalink)
Registered Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,122
Default

There are several posts on this forum that answer questions 1 and 3. Spend some time searching.
RickMaddy is offline   Reply With Quote
Old 02-12-2009, 02:10 PM   #5 (permalink)
Senior Member
 
Join Date: Jan 2009
Location: Florida, USA
Posts: 471
Default

Dude, this is trivial stuff. Get a pointer to the raw image data, and then iterate trough the pixels. If you need to work on a matrix of pixels, use multiple pointers. There is nothing iPhone specific, this is easy stuff, but nobody is going to write the code for you. Go look in a book or use google, there is a massive amount of examples for image processing.
Slayer5150 is offline   Reply With Quote
Old 02-12-2009, 02:25 PM   #6 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 6
Cool

Again i'm very aware of the steps seperatly. I've done the searching i've seen the posts here such as http://www.iphonedevsdk.com/forum/ip...ge-pixels.html

I guess I should have just posted my code and let you guys find the bug. since this code should be trivial as you say and I agree I thought someone would have have a simple example that they could copy and paste so I could find out there I went wrong.

I'll post my code later.
afastrunner is offline   Reply With Quote
Old 02-16-2009, 11:45 PM   #7 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 6
Default

Here is the code i've worked out for anyone else who would like to user it. if anyone sees any problems with the code please let me know.

I've included a few different basic image processing examples just comment/uncomment the one that you want to use. I haven't implemented any Matrix filters but will be working on that soon.

PHP Code:
    CGImageRef inImage img.CGImage;        
    
CGContextRef ctx;
    
        
        
CFDataRef m_DataRef;
        
m_DataRef CGDataProviderCopyData(CGImageGetDataProvider(inImage));
        
UInt8 m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    
//    Byte tmpByte;
        
int length CFDataGetLength(m_DataRef);
        
NSLog(@"len %d"length);
        
        
NSLog(@"width=%d, height=%d"CGImageGetWidthinImage ), CGImageGetHeightinImage ));        
        
NSLog(@"1=%d, 2=%d, 3=%d"CGImageGetBitsPerComponent(inImage), CGImageGetBitsPerPixel(inImage),CGImageGetBytesPerRow(inImage));
        

    
//    int Contrast_transform[255];
    //    float contrast = .15;//quantifies the angle of the slope (in radians) of the contrast transform        
    //    for(int i=0;i<256;i++){
    //        if(i<(int)(128.0f+128.0f*tan(contrast))&&i>(int)(128.0f-128.0f*tan(contrast)))
    //            Contrast_transform[i]=(i-128)/tan(contrast)+128;
    //        else if(i>=(int)(128.0f+128.0f*tan(contrast)))
    //            Contrast_transform[i]=255;
    //        else
    //            Contrast_transform[i]=0;
    //    }    
        
        
for (int index 0index lengthindex += 4)
        {
        
//    tmpByte = (m_PixelBuf[index + 1] + m_PixelBuf[index + 2] + m_PixelBuf[index + 3]) / 3;
        //    if (tmpByte >= 128)
        //        m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 255;
        //    else
        //        m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 0;
            
            
m_PixelBuf[index 1] = 255 m_PixelBuf[index 1];
            
m_PixelBuf[index 2] = 255 m_PixelBuf[index 2];
            
m_PixelBuf[index 3] =    255 m_PixelBuf[index 3];    
        
        
//    m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = tmpByte;        
    
        //    m_PixelBuf[index + 1] = Contrast_transform[m_PixelBuf[index + 1]];
        //    m_PixelBuf[index + 2] = Contrast_transform[m_PixelBuf[index + 2]];
        //    m_PixelBuf[index + 3] = Contrast_transform[m_PixelBuf[index + 3]];
            
        
}
        
    
        
        
ctx CGBitmapContextCreate(m_PixelBuf,
                                    
CGImageGetWidthinImage ),
                                    
CGImageGetHeightinImage ),
                                    
8,
                                    
CGImageGetBytesPerRowinImage ),
                                    
CGImageGetColorSpaceinImage ),
                                    
kCGImageAlphaPremultipliedFirst );
        
        
        
CGImageRef imageRef CGBitmapContextCreateImage (ctx);
        
UIImagerawImage = [UIImage imageWithCGImage:imageRef];
        
        
CGContextRelease(ctx);
        
    
image.image rawImage

Last edited by afastrunner; 02-17-2009 at 12:18 AM.
afastrunner is offline   Reply With Quote
Old 03-03-2009, 06:31 PM   #8 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 166
Default

Works great, thanks for the code.

Can anyone think of any way of optimizing this code? Something I will have to look into myself.
cakesy is offline   Reply With Quote
Old 04-10-2009, 08:00 AM   #9 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 1
Default Need help on changing contrast of an Image

Hi afastrunner,

I used your code to increase/deccrese the contrast of my png image. It just makes the image black and white. i tried varying the angle of the slope of the contrast also but it makes every thing black or white. How to maintain the color components of the pixels. can you please guide me a little bit on this.
rajivtec is offline   Reply With Quote
Old 06-01-2009, 12:42 PM   #10 (permalink)
Registered Member
 
Join Date: Jul 2008
Location: Hyderabad,India
Age: 25
Posts: 233
Default

Hi afastrunner,

Thanks for your code.

Can you help me in adding tanning effects to a image.
I need to get the image having some time periods of tanning(some 1hrs, 30mnts, 15mnts etc).

approximate to time span of tanning I need to get the image with that effect.

Is it possible to do like that??

Thanks,
narendar
narendar is offline   Reply With Quote
Old 07-17-2009, 05:03 PM   #11 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 23
Default Memory Leak

Remeber, when using CGDataProviderCopyData, you are copying bits and are responsible for releasing them with CFRelease().

In the end of afastrunner's code, there should be (or at least somewhere in the program):
Code:
CFRelease(m_DataRef);
Karplusan is offline   Reply With Quote
Old 07-26-2009, 08:33 PM   #12 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default

Quote:
Originally Posted by afastrunner View Post
Here is the code i've worked out for anyone else who would like to user it. if anyone sees any problems with the code please let me know.

I've included a few different basic image processing examples just comment/uncomment the one that you want to use. I haven't implemented any Matrix filters but will be working on that soon.

PHP Code:
    CGImageRef inImage img.CGImage;        
    
CGContextRef ctx;
    
        
        
CFDataRef m_DataRef;
        
m_DataRef CGDataProviderCopyData(CGImageGetDataProvider(inImage));
        
UInt8 m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    
//    Byte tmpByte;
        
int length CFDataGetLength(m_DataRef);
        
NSLog(@"len %d"length);
        
        
NSLog(@"width=%d, height=%d"CGImageGetWidthinImage ), CGImageGetHeightinImage ));        
        
NSLog(@"1=%d, 2=%d, 3=%d"CGImageGetBitsPerComponent(inImage), CGImageGetBitsPerPixel(inImage),CGImageGetBytesPerRow(inImage));
        

    
//    int Contrast_transform[255];
    //    float contrast = .15;//quantifies the angle of the slope (in radians) of the contrast transform        
    //    for(int i=0;i<256;i++){
    //        if(i<(int)(128.0f+128.0f*tan(contrast))&&i>(int)(128.0f-128.0f*tan(contrast)))
    //            Contrast_transform[i]=(i-128)/tan(contrast)+128;
    //        else if(i>=(int)(128.0f+128.0f*tan(contrast)))
    //            Contrast_transform[i]=255;
    //        else
    //            Contrast_transform[i]=0;
    //    }    
        
        
for (int index 0index lengthindex += 4)
        {
        
//    tmpByte = (m_PixelBuf[index + 1] + m_PixelBuf[index + 2] + m_PixelBuf[index + 3]) / 3;
        //    if (tmpByte >= 128)
        //        m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 255;
        //    else
        //        m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = 0;
            
            
m_PixelBuf[index 1] = 255 m_PixelBuf[index 1];
            
m_PixelBuf[index 2] = 255 m_PixelBuf[index 2];
            
m_PixelBuf[index 3] =    255 m_PixelBuf[index 3];    
        
        
//    m_PixelBuf[index + 1] = m_PixelBuf[index + 2] = m_PixelBuf[index + 3] = tmpByte;        
    
        //    m_PixelBuf[index + 1] = Contrast_transform[m_PixelBuf[index + 1]];
        //    m_PixelBuf[index + 2] = Contrast_transform[m_PixelBuf[index + 2]];
        //    m_PixelBuf[index + 3] = Contrast_transform[m_PixelBuf[index + 3]];
            
        
}
        
    
        
        
ctx CGBitmapContextCreate(m_PixelBuf,
                                    
CGImageGetWidthinImage ),
                                    
CGImageGetHeightinImage ),
                                    
8,
                                    
CGImageGetBytesPerRowinImage ),
                                    
CGImageGetColorSpaceinImage ),
                                    
kCGImageAlphaPremultipliedFirst );
        
        
        
CGImageRef imageRef CGBitmapContextCreateImage (ctx);
        
UIImagerawImage = [UIImage imageWithCGImage:imageRef];
        
        
CGContextRelease(ctx);
        
    
image.image rawImage
The code seems to be malfunctioning for me (Simulator iPhone OS 2.0), everything gets either drawn to one side or not at all.



Can anyone help, I only altered the code to load an UIImage from the simulator's photo library which was called "image", so the only edited line is :
Code:
CGImageRef inImage = img.CGImage;
Anyway, this was the most helpful piece of code so far. Most of the time, the only reply you get is "buy a book"...
ykrsdn is offline   Reply With Quote
Old 11-08-2009, 03:32 PM   #13 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 3
Default Anyone using this this code for pixel manipulation?

The code that afastrunner posted many months ago actually works... The contrast adjustment (which was commented out) actually works well, but suffers from too much CPU action to pixel map values like this for very large images (10Meg+).

Has anyone done anything simular, but found a faster way to filter the pixels for contrast, brightness, etc.?
cavalleydude is offline   Reply With Quote
Old 11-09-2009, 02:10 PM   #14 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 3
Default Looking for simular image filters

Did you create a window/level solution? I've used your code for contrast adjustment, but looking for faster window/level techniques. Any suggestions?
cavalleydude is offline   Reply With Quote
Old 01-13-2010, 06:59 AM   #15 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 96
Default

Hi Guys,

I've just made a post with a code sample found on the web that allow you to modify brightness of a uiimage
http://www.iphonedevsdk.com/forum/ip...ng-opengl.html

I've posted here it for reference.

If someone could help me to modify that code in order to make it work correctly it will be awesome :-D

David
Dvdkite is offline   Reply With Quote
Old 01-14-2010, 10:55 AM   #16 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default Sharing Greyscale Code

I just read this: Grayscale - Wikipedia, the free encyclopedia

and succesfully implemented the greyscale in my app.

There you go: this is the filter code part:

Code:
			for(int index=0;index<length;index+=4){
				Byte grayScale = 
				(Byte)(data[index+3]*.11  + 
					   data[index + 2] * .59  + 
					   data[index + 1] * .3); 
				
				//set the new image's pixel to the grayscale version
				data[index+1] = grayScale;
				data[index+2] = grayScale;
				data[index+3] = grayScale;
			}


Oh yeah, those .11, .59 and .3 are magic numbers. Hehe
rocotilos is offline   Reply With Quote
Old 01-14-2010, 11:02 AM   #17 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 96
Default

Hi Rocotilos,

I've also managed the grayscale with the code found on the web:

http://www.iphonedevsdk.com/forum/ip...recth-why.html


posted for reference
Dvdkite is offline   Reply With Quote
Old 01-14-2010, 11:07 AM   #18 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Your method is different. Interesting.
I did thru manipulating of the pixel by pixels.
rocotilos is offline   Reply With Quote
Old 01-14-2010, 11:08 AM   #19 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 96
Default

Quote:
Originally Posted by rocotilos View Post
Your method is different. Interesting.
I did thru manipulating of the pixel by pixels.
ahahaha... yes but it is not mine... I just find it on the web with google!!!!!!!!! :-D
Dvdkite is offline   Reply With Quote
Old 01-14-2010, 11:13 AM   #20 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
Default

Quote:
Originally Posted by rocotilos View Post
Your method is different. Interesting.
I did thru manipulating of the pixel by pixels.
i wrote this code for grayscale.

Code:
for (int index = 0; index < length; index += 4)
{
	Byte tempR = m_PixelBuf[index + 1];
	Byte tempG = m_PixelBuf[index + 2];
	Byte tempB = m_PixelBuf[index + 3];
		
	int eyeGrayScale = tempR * .3 + tempG * .59 + tempB * .11;
		
		
	m_PixelBuf[index + 1] = eyeGrayScale; 
	m_PixelBuf[index + 2] = eyeGrayScale; 
	m_PixelBuf[index + 3] = eyeGrayScale;
		
}
edit: added code tag

Last edited by dany_dev; 01-22-2011 at 12:49 PM.
dany_dev is offline   Reply With Quote
Old 01-14-2010, 11:49 AM   #21 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default Sharing Brightness Code

Ok done.

I think this is the correct brightness filter for your image. Did this from scratch.

It will go from Black to White. Oh yeah, the slider value must be -1 to +1.
Set the default to 0.

Code:
	for(int index=0;index<length;index+=4){
		// BRIGHTNESS
		
		Byte amountRed;
		Byte amountGreen;
		Byte amountBlue;
		
		if (thevalue<=0) {
			amountRed= (Byte)(data[index+1]*thevalue);
			amountGreen = (Byte)(data[index + 2]*thevalue);
			amountBlue = (Byte)(data[index+3]*thevalue);	
		} else {
			amountRed= (Byte)((255-data[index+1])*thevalue);
			amountGreen = (Byte)((255-data[index + 2])*thevalue);
			amountBlue = (Byte)((255-data[index+3])*thevalue);
			
		}

		data[index+1] = data[index+1]+amountRed;
		data[index+2] = data[index+2]+amountGreen;
		data[index+3] = data[index+3]+amountBlue;
			
    }
Oh yeah, dont forget to change the method "thevalue" typecast to float coz now we're using -1 to +1.

Last edited by rocotilos; 01-14-2010 at 11:53 AM.
rocotilos is offline   Reply With Quote
Old 01-15-2010, 06:43 AM   #22 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
Default

now i understand the problem, lol, initially i thinked that the problem was that code not working, after i uderstood that you want to make an image in grayscale .
However, good work!.
dany_dev is offline   Reply With Quote
Old 01-15-2010, 06:59 AM   #23 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 96
Default

Hi Dany,

Yes ... but I've tryed your code WITHOUT Apply the GrayScale !
So I've tryed it on UIImage taken from camera and I don't know why but it doesn't work so good for me... colours tone change (for example a photo of a pizza swith to original to blue tone)...

Hi Rocotilos,

Thank you very much for your code.. but it give me a strange colors tone modification like explained above...
I don't know If I'm doing something wrong...
I'll do some other test and let's see how it will work!
Dvdkite is offline   Reply With Quote
Old 01-15-2010, 01:11 PM   #24 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by Dvdkite View Post
Hi Dany,

Yes ... but I've tryed your code WITHOUT Apply the GrayScale !
So I've tryed it on UIImage taken from camera and I don't know why but it doesn't work so good for me... colours tone change (for example a photo of a pizza swith to original to blue tone)...

Hi Rocotilos,

Thank you very much for your code.. but it give me a strange colors tone modification like explained above...
I don't know If I'm doing something wrong...
I'll do some other test and let's see how it will work!
Dvdkite, I had this happen to me too when playing around with it.
You gotta be careful of which data you change.

data[0] = ALPHA CHANNEL
data[1] = RED CHANNEL
data[2] = GREEN CHANNEL
data[3] = BLUE CHANNEL.

or

data[index] = alpha
data[index+1] = red..
data[index+2] = green..
data[index+3] = blue..


I presume you are only modifying the 0 to 2, hence the blue channel remain (no change in blue, so when u reduce others, the image become blue). make sure you selecting the correct channel to modify their values.

EDIT: I took a look at your brightness code (in the other thread), and spot the problem there:

Code:
for(int i=0;i<3;i++){
			if(data[index+i]+value<0){
				data[index+i]=0;
			}else{
				if(data[index+i]+value>255){
					data[index+i]=255;
				}else{
					data[index+i]+=value;
				}
			}
		}
You might wanna try changing that to i=1;i<4

Last edited by rocotilos; 01-15-2010 at 01:16 PM.
rocotilos is offline   Reply With Quote
Old 01-20-2010, 12:34 PM   #25 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Colorize code.. anyone have that?

I'm working on it, but if anyone else want to share first, pls do.
rocotilos is offline   Reply With Quote
Reply

Bookmarks

Tags
bitmap, effects, filter, image proccessing, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 247
20 members and 227 guests
ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:40 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0