Using the C API for ImageMagick (on iPhone?) to convert to monochrome?
You can achieve monochrome conversion, as described here, with MagickQuantizeImage function.
I'm not quite familiar with dithering images, but an example may look like the following.
#include <wand/MagickWand.h>
int main(int argc, char **argv)
{
const size_t number_colors = 2;
const size_t treedepth = 1;
MagickWandGenesis();
MagickWand *wand = NULL;
wand = NewMagickWand();
MagickReadImage(wand,"source.png");
MagickQuantizeImage(
wand, // MagickWand
number_colors, // Target number colors
GRAYColorspace, // Colorspace
treedepth, // Optimal depth
MagickTrue, // Dither
MagickFalse // Quantization error
);
MagickWriteImage(wand,"out.png");
if(wand)wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
This can give you a rather speckled image at times.
Adjusting depth, color-number, and/or disabling dithering may give you results closer to what you would expect from the examples provided.
MagickQuantizeImage(
wand, // MagickWand
number_colors, // Target number colors
GRAYColorspace, // Colorspace
treedepth, // Optimal depth
MagickFalse, // No-dither
MagickFalse // Quantization error
);
Like such...
For iOS
Not much effort is needed to port the example code to iOS. NextStep/Objective-c methods are compatible with MagickWand library. The following example uses a temporary file to store the monochrome image, but I'm sure there is a better way to pass Magick image-data directly to UImage object.
// MyViewController.h
#import <UIKit/UIKit.h>
#import <wand/MagickWand.h>
@interface MyViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (retain, nonatomic) MagickWand *wand;
@end
// MyViewController.m
#import "MyViewController.h"
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MagickWandGenesis();
self.wand = NewMagickWand();
[self drawMonochromeImage:@"logo:"];
}
-(void)drawMonochromeImage:(NSString *)filePath
{
// Create temporary file
NSString *tempFilePath = [NSTemporaryDirectory()
stringByAppendingPathComponent:@"logo.jpg"];
// Read given image with C-string
MagickReadImage(self.wand,
[filePath cStringUsingEncoding:NSASCIIStringEncoding]
);
// Monochrome image
MagickQuantizeImage(self.wand,2,GRAYColorspace,1,MagickFalse,MagickFalse);
// Write to temporary file
MagickWriteImage(self.wand,
[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]
);
// Load UIImage from temporary file
UIImage *imgObj = [UIImage imageWithContentsOfFile:tempFilePath];
// Display on device
[self.imageView setImage:imgObj];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
-(void)viewDidUnload
{
// Clean-up
if (self.wand)
self.wand = DestroyMagickWand(self.wand);
MagickWandTerminus();
}
@end
imagemagick monochrome convert in iPhone
It turns out what I've been looking for is Adaptive Thresholding in my conversion to monochrome.
Chris Greening has created an image processing library that handles this quite well.
So no imagemagick needed.
Clean Monocrome image with Imagemagik in iOS
Finally I was able to use the "ConvertImageCommand()" function of the ImageMagick API for C and then used this code to generate the image with grate results!.
char *argv[] = {"convert","-respect-parenthesis","\(", input, "-set", "-colorspace", "RGB", "-colorspace", "gray", "-type", "grayscale","-colorspace","RGB", "-normalize","\)","\(","-clone", "0","-set","-colorspace", "RGB","-colorspace","gray","-negate", "-lat", "15x15+5%", "-contrast-stretch", "0","\)","-compose","copy_opacity","-composite","-fill","white","-opaque","none","+matte", output, NULL};
I hope this can be useful to someone.
Regards,
Related Topics
Add Uitapgesturerecognizer to Uitextview Without Blocking Textview Touches
How to Access an Iboutlet from Another Class
How Long Does a Push Notification Sit in Queue Before Being Removed
Finding Distance Between Cllocationcoordinate2D Points
Choosing Units with Measurementformatter
Uicontrolstate.Normal Is Unavailable
How to Implement Tableview Inside Tableview Cell in Swift 3
iOS - Linking Framework Storyboard to Viewcontroller for Use in Main Project
Perform Segue with Identifier Wont Work in Swift 2
Error Setting Text in Collection View Cell
iPhone Camera, How to Avoid Cameraoverlay on Preivew View; How to Know When Entering Preview View
Error Domain=Nsposixerrordomain Code=100 "Protocol Error"
iOS Simulator Is Not Launching
Nsdata to Nsstring with JSON Response
Not Getting the Email Using Google Authentication in Firebase
Xcode Doesn't Write Anything to Output Console
iOS Uitableview with Dynamic Text and Images Rendered Together (Nsattributedstring + Images)