Mirroring CIImage/NSImage

烈酒焚心 提交于 2019-12-13 05:14:59

问题


Currently I have the following

 CIImage *img = [CIImage imageWithCVImageBuffer: imageBuffer];

 NSCIImageRep *imageRep = [NSCIImageRep imageRepWithCIImage:img];
 NSImage *image = [[[NSImage alloc] initWithSize: [imageRep size]] autorelease];
 [image addRepresentation:imageRep];

This works perfectly, I can use the NSImage and when written to a file the image is exactly how I need it to be.

However, I'm pulling this image from the users iSight using QTKit, so I need to be able to flip this image across the y axis.

My first thought was to transform the CIImage using something like this, however my final image always comes out completely blank. When written to a file the dimensions are correct but it's seemingly empty.

- (CIImage *)flipImage:(CIImage *)image
{
    return [image imageByApplyingTransform:CGAffineTransformMakeScale(-1, 1)];
}

Am I approaching this the wrong way? Or have I made a mistake in my code?


回答1:


That transform flips it, but the axis around which it flips is not at the center of the image, but at the left edge. You must also translate the image by its width to account for the movement caused during the scale.




回答2:


Here is some code that may help someone out ==>

    CGAffineTransform rotTrans = CGAffineTransformMakeRotation(M_PI_2);
    CGAffineTransform transTrans1 = CGAffineTransformTranslate(rotTrans, 0.0f, 320.0f);
    CGAffineTransform scaleTrans = CGAffineTransformScale(transTrans1, 1.0, -1.0);
    CGAffineTransform transTrans2 = CGAffineTransformTranslate(scaleTrans, -0.0f, -320.0f);

    self.view.transform = transTrans2;

I use it to flip frames from the front camera horizontally so they always appear up no matter what the rotation of the device. This stuff does get kind of tricky. One thing to do to help figure out what is going on is scaling down along either of the axes and seeing what the result is.



来源:https://stackoverflow.com/questions/6128640/mirroring-ciimage-nsimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!