gpt4 book ai didi

ios - 从 Adob​​eLabsUXMagicSelectionView (Adobe Creative SDK) 获取前景

转载 作者:行者123 更新时间:2023-12-01 18:09:16 25 4
gpt4 key购买 nike

我正在尝试使用 AdobeLabsUXMagicSelectionView我面临两个问题。我想使用以下两种方法“剪切”选定区域(前景):

方法一)getForeground:andMatte:它没有给我正确的前景。当我选择一个区域并调用getForeground:andMatte我给了我前景和背景(混合)。

选择狗的脸
enter image description here

剪狗的脸
enter image description here

文档说:

Alternatively, if you don’t need to process the underlying bitmap directly, and intend to use the results as inputs to CoreGraphics or CoreImage, you can call:



方法2)在此之后,我试图像文档一样“剪切”
extension AdobeLabsUXMagicSelectionView {

func foregroundCGImage() -> CGImage {
let w = size_t(self.image.size.width)
let h = size_t(self.image.size.height)
let data = UnsafeMutablePointer<UInt8>(malloc(4 * w * h * sizeof(UInt8)))
self.readForegroundAndMatteIntoBuffer(data)

for var i = 0; i < 4 * w * h; i += 4 {
let alpha: UInt8 = UInt8(data[i + 3]) / 255
data[i] *= alpha
data[i + 1] *= alpha
data[i + 2] *= alpha
}

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipLast.rawValue)
let ctx = CGBitmapContextCreate(data, w, h, 8, 4 * w, CGColorSpaceCreateDeviceRGB(), bitmapInfo.rawValue)
let imageRef = CGBitmapContextCreateImage(ctx)!

return imageRef
}

}

但它只绘制(黑色)图像的未选择部分(背景)。

任何人都可以帮忙吗?我想要的是获得选定区域的最终图像。

更新:

正如@DonWoodward 所说,我创建了这个类别:
@implementation AdobeLabsUXMagicSelectionView (Foreground)

- (UIImage *)getForeground {
// show the results
// first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h
size_t w = self.image.size.width;
size_t h = self.image.size.height;

uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t));
[self readForegroundAndMatteIntoBuffer:data];

// Paint the non-selected portion of the image black
for (int i = 0; i < 4*w*h; i += 4) {
float alpha = (float)data[i + 3] / 255;
data[i ] *= alpha;
data[i + 1] *= alpha;
data[i + 2] *= alpha;
}
CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return foregroundBits;
}

@end

但是结果在“前景”周围有很多黑色像素。

enter image description here
enter image description here

我需要的?获得一个“干净”的前景(没有黑色像素的选定区域)放在 UIImageView 上

最佳答案

我认为问题在于您的比例因子类型为 uint8_t所以你将比例因子剪裁为零。让它成为一个 float ,它应该工作。这是来自 MagicPuppy(在 objective-c 中)的代码:

    // show the results
// first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h
size_t w = _magicSelectionView.image.size.width;
size_t h = _magicSelectionView.image.size.height;

uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t));
[_magicSelectionView readForegroundAndMatteIntoBuffer:data];

// Paint the non-selected portion of the image black
for (int i = 0; i < 4*w*h; i += 4)
{
float alpha = (float)data[i + 3] / 255;
data[i ] *= alpha;
data[i + 1] *= alpha;
data[i + 2] *= alpha;
}
CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

// show the results
_resultsView = [[UIImageView alloc] initWithFrame: CGRectMake(0, VIEW_Y_OFFSET, self.view.bounds.size.width, self.view.bounds.size.height-VIEW_Y_OFFSET)];
_resultsView.contentMode = UIViewContentModeScaleAspectFit;
[_resultsView setImage: foregroundBits];
[self.view addSubview: _resultsView];

关于ios - 从 Adob​​eLabsUXMagicSelectionView (Adobe Creative SDK) 获取前景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043434/

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