gpt4 book ai didi

iphone - 如何创建并应用我计算出的图像倾斜变换?

转载 作者:行者123 更新时间:2023-12-03 20:01:40 24 4
gpt4 key购买 nike

我已经问过this question肯尼给了我一个很好的答案(非常感谢他),但我仍然对最终的实现感到困惑。所以,问题是 - 如何访问图像中特定像素的矩阵。

我将 Kenny 的示例翻译为 Objective-C:

- (NSArray *) compute_transform_matrix:(float)X
Y:(float)Y
W:(float)W
H:(float)H
x1a:(float)x1a
y1a:(float)y1a
x2a:(float)x2a
y2a:(float)y2a
x3a:(float)x3a
y3a:(float)y3a
x4a:(float)x4a
y4a:(float)y4a {
float y21 = y2a - y1a, y32 = y3a - y2a, y43 = y4a - y3a, y14 = y1a - y4a, y31 = y3a - y1a, y42 = y4a - y2a;

float a = -H*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42);
float b = W*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);
float c = H*X*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42) - H*W*x1a*(x4a*y32 - x3a*y42 + x2a*y43) - W*Y*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43);

float d = H*(-x4a*y21*y3a + x2a*y1a*y43 - x1a*y2a*y43 - x3a*y1a*y4a + x3a*y2a*y4a);
float e = W*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42);
float f = -(W*(x4a*(Y*y2a*y31 + H*y1a*y32) - x3a*(H + Y)*y1a*y42 + H*x2a*y1a*y43 + x2a*Y*(y1a - y3a)*y4a + x1a*Y*y3a*(-y2a + y4a)) - H*X*(x4a*y21*y3a - x2a*y1a*y43 + x3a*(y1a - y2a)*y4a + x1a*y2a*(-y3a + y4a)));

float g = H*(x3a*y21 - x4a*y21 + (-x1a + x2a)*y43);
float h = W*(-x2a*y31 + x4a*y31 + (x1a - x3a)*y42);
float i = W*Y*(x2a*y31 - x4a*y31 - x1a*y42 + x3a*y42) + H*(X*(-(x3a*y21) + x4a*y21 + x1a*y43 - x2a*y43) + W*(-(x3a*y2a) + x4a*y2a + x2a*y3a - x4a*y3a - x2a*y4a + x3a*y4a));
return [NSArray arrayWithObjects:
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:a],[NSNumber numberWithFloat:b],[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:c],nil],
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:d],[NSNumber numberWithFloat:e],[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:f],nil],
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:1],[NSNumber numberWithFloat:0],nil],
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:g],[NSNumber numberWithFloat:h],[NSNumber numberWithFloat:0],[NSNumber numberWithFloat:i],nil],
nil];
}

并且,在一开始我创建图像位图 - “imageBitmap”:

   - (void) createImageBitmap{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"Demo.png" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
CGImageRef image = CGImageRetain(img.CGImage);

NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef imageBitmap = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(imageBitmap, CGRectMake(0, 0, width, height), image);
}

然后,我想,我需要执行以下操作:

    for(int x=0;x<width;x++){
for(int y=0;x<height;y++){
NSArray* matrixForPoint = [self compute_transform_matrix:x Y:y W:width H:height x1a:0 y1a:0 x2a:width y2a:0 x3a:width y3a:height x4a:0 y4a:height];
// set Matrix for x, y point to "imageBitmap" bitmap, but HOW?????????
}
}

那么,如何将x、y点的Matrix设置为“imageBitmap”位图呢?

提前非常感谢

最佳答案

Kenny 的解决方案为您提供了一种生成 CATransform3D 的方法,然后您可以将其应用于托管图像的 CALayer 或 UIView 层。您需要使用您在此处计算的值来为该变换分配矩阵元素。我的建议是让您的 -compute_transform_matrix: 方法返回 CATransform3D,并将最后几行替换为

CATransform3D skewTransform;
skewTransform.m11 = a;
skewTransform.m12 = b;
skewTransform.m13 = 0;
skewTransform.m14 = c;
skewTransform.m21 = d;
skewTransform.m22 = e;
skewTransform.m23 = 0;
skewTransform.m24 = f;
skewTransform.m31 = 0;
skewTransform.m32 = 0;
skewTransform.m33 = 1;
skewTransform.m34 = 0;
skewTransform.m41 = g;
skewTransform.m42 = h;
skewTransform.m43 = 0;
skewTransform.m44 = i;

return skewTransform;

然后,您可以在图层或 View 的图层上设置此变换以实现此倾斜效果。

关于iphone - 如何创建并应用我计算出的图像倾斜变换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2379039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com