ios - ImageMagick如何编写代码以在图像上应用ShepardsDistortion [英] ios - ImageMagick How to code to apply ShepardsDistortion on image

查看:281
本文介绍了ios - ImageMagick如何编写代码以在图像上应用ShepardsDistortion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ImageMagick的新手,我想在源图像上开发ShepardsDistortion的效果。我经历了很多帖子和网站,但我没有找到在iOS中实现ShepardsDistortion的方法。

  MagickWand * mw = NewMagickWand(); 
MagickSetFormat(mw,png);

UIImage * sourceImage = [_ sourceImgView image];
NSData * imgData = UIImagePNGRepresentation(sourceImage);
MagickReadImageBlob(mw,[imgData bytes],[imgData length]);
Image * image = GetImageFromMagickWand(mw);

DistortImage(image,ShepardsDistortion ,,,);

我做到了这一点,但我不知道在DitortImage()中传递什么作为arg。所以,如果有人知道,请帮助我。



编辑:

   - (void)distortImage {

MagickWandGenesis();
MagickWand *魔杖;
MagickBooleanType状态;

wand = NewMagickWand();
MagickSetFormat(魔杖,png);
status = MagickReadImage(wand,chess.png);

// Shepards的参数
双倍积分[8];
points [0] = 250; //第一个X点(开始)
points [1] = 250; //第一个Y点(开始)
points [2] = 50; //第一个X点(结束)
points [3] = 150; //前Y点(结束)
points [4] = 500; //第二个X点(开始)
points [5] = 380; //第二个Y点(开始)
points [6] = 600; //第二个X点(结束)
points [7] = 460; //第二个Y点(结束)


MagickDistortImage(魔杖,ShepardsDistortion,8,points,MagickFalse);
NSString * tempFilePath = [NSTemporaryDirectory()stringByAppendingPathComponent:@out.png];
MagickWriteImage(wand,[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];
_resultImgView.image = imgObj;

//
// unsigned char * cBlob;
// size_t data_size;
// cBlob = MagickGetImageBlob(wand,& data_size);
// NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
// UIImage * uiImage = [UIImage imageWithData:nsBlob];
// _resultImgView.image = uiImage;

MagickWriteImage(wand,out.png);
wand = DestroyMagickWand(魔杖);
MagickWandTerminus();
}


解决方案

参数传递给DistortImage as双打列表的开头,以及有关列表的大小信息。示例:

  size_t SizeOfPoints = 8; 
双倍积分[SizeOfPoints];
DistortImage(图片,
ShepardsDistoration,
SizeOfPoints,
积分,
MagickFalse,
NULL
);

在你的例子中,你似乎在混合),或使用NSData动态创建图像(如这个问题)。



临时路径示例:

  NSString * tempFilePath = [NSTemporaryDirectory()
stringByAppendingPathComponent:@ out.png];
MagickWriteImage(self.wand,
[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];

NSData + blob的例子

  unsigned char * cBlob; 
size_t data_size;
cBlob = MagickGetImageBlob(wand,& data_size);
NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
UIImage * uiImage = [UIImage imageWithData:nsBlob];


I am new to ImageMagick,and i want to develop an effect of ShepardsDistortion on source image. i gone through many posts and sites, but i didn't find way to implement "ShepardsDistortion" in iOS.

    MagickWand *mw = NewMagickWand();
    MagickSetFormat(mw, "png");

    UIImage *sourceImage=[_sourceImgView image];
    NSData *imgData=UIImagePNGRepresentation(sourceImage);
    MagickReadImageBlob(mw, [imgData bytes], [imgData length]);
    Image *image=GetImageFromMagickWand(mw);

    DistortImage(image, ShepardsDistortion, , ,);

I done upto this, but i dont know what to pass as arg in DitortImage(). So if anyone knows then help me.

EDIT:

-(void)distortImage{

    MagickWandGenesis();
    MagickWand * wand;
    MagickBooleanType status;

    wand = NewMagickWand();
    MagickSetFormat(wand, "png");
    status = MagickReadImage(wand,"chess.png");

    // Arguments for Shepards
    double points[8];
    points[0] = 250; // First X point (starting)
    points[1] = 250; // First Y point (starting)
    points[2] =  50; // First X point (ending)
    points[3] = 150; // First Y point (ending)
    points[4] = 500; // Second X point (starting)
    points[5] = 380; // Second Y point (starting)
    points[6] = 600; // Second X point (ending)
    points[7] = 460; // Second Y point (ending)


    MagickDistortImage(wand,ShepardsDistortion,8,points,MagickFalse);
    NSString * tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"out.png"];
    MagickWriteImage(wand,[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
    UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];
    _resultImgView.image=imgObj;

//    
//    unsigned char * cBlob;
//    size_t data_size;
//    cBlob = MagickGetImageBlob(wand, &data_size);
//    NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
//    UIImage *uiImage = [UIImage imageWithData:nsBlob];
//    _resultImgView.image=uiImage;

    MagickWriteImage(wand,"out.png");
    wand=DestroyMagickWand(wand);
    MagickWandTerminus();
}

解决方案

Arguments are passed to DistortImage as the start of a list of doubles, and size information about the list. Example:

size_t SizeOfPoints = 8;
double Points[SizeOfPoints];
DistortImage(image,
             ShepardsDistoration,
             SizeOfPoints,
             Points,
             MagickFalse,
             NULL
            );

In your example, you seem to be mixing MagickWand & MagickCore methods; which, seems unnecessary and confusing. I would keep this distortion simple, and only use MagickWand's MagickDistortImage method. Here's a example in

int main(int argc. const char **argv)
{
  MagickWandGenesis();
  MagickWand * wand;
  MagickBooleanType status;

  wand = NewMagickWand();
  status = MagickReadImage(wand,"logo:");

  // Arguments for Shepards
  double points[8];
  // 250x250 -> 50x150
  points[0] = 250; // First X point (starting)
  points[1] = 250; // First Y point (starting)
  points[2] =  50; // First X point (ending)
  points[3] = 150; // First Y point (ending)
  // 500x380 -> 600x460
  points[4] = 500; // Second X point (starting)
  points[5] = 380; // Second Y point (starting)
  points[6] = 600; // Second X point (ending)
  points[7] = 460; // Second Y point (ending)

  MagickDistortImage(wand,ShepardsDistortion,8,points,MagickFalse);
  MagickWriteImage(wand,"out.png");

  wand=DestroyMagickWand(wand);
  MagickWandTerminus();

  return 0;
}

Resulting in a distorted translated image (details)

Edit

For iOS, you can use NSTemporaryDirectory (like in this answer), or create an image dynamically using NSData (like in this question).

Example with temporary path:

NSString * tempFilePath = [NSTemporaryDirectory() 
                             stringByAppendingPathComponent:@"out.png"];
MagickWriteImage(self.wand,
    [tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];

And an example with NSData + blob

unsigned char * cBlob;
size_t data_size;
cBlob = MagickGetImageBlob(wand, &data_size);
NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
UIImage * uiImage = [UIImage imageWithData:nsBlob];

这篇关于ios - ImageMagick如何编写代码以在图像上应用ShepardsDistortion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆