这是我需要实现的目标:
结果示例:
原始图片
结果图片
这是我尝试过的:
CGFloat colorMasking[6]={222,255,222,255,222,255};
CGImageRef imageRef = CGImageCreateWithMaskingColors([IMG CGImage], colorMasking);
UIImage *resultThumbImage = [UIImage imageWithCGImage:imageRef scale:ThumbImage.scale orientation:IMG.imageOrientation];
它只适用于白色背景。它不是更有效。我需要获得我在上面图片中放置的准确结果。
我还提到了一些引用资料:
iOS how to mask the image background color
How to remove the background of image in iphone app?
Changing the background color of a captured image from camera to white
有人可以帮我实现这个吗?
任何引用或将不胜感激。
提前致谢。
最佳答案
一般来说,根据经验,背景颜色与所有其他颜色的差异越大, 将图像分成前景和背景越容易。在这种情况下,@Chris 已经 建议,可以使用简单的色度键实现。下面是我的快 Wikipedia 中描述的键控实现(它是用 C++ 编写的,但将其翻译成 Objective-C 应该很简单):
/**
* @brief Separate foreground from background using simple chroma keying.
*
* @param imageBGR Image with monochrome background
* @param chromaBGR Color of the background (using channel order BGR and range [0, 255])
* @param tInner Inner threshold, color distances below this value will be counted as foreground
* @param tOuter Outer threshold, color distances above this value will be counted as background
*
* @return Mask (0 - background, 255 - foreground, [1, 255] - partially fore- and background)
*
* Details can be found on [Wikipedia][1].
*
* [1]: https://en.wikipedia.org/wiki/Chroma_key#Programming
*/
cv::Mat1b chromaKey( const cv::Mat3b & imageBGR, cv::Scalar chromaBGR, double tInner, double tOuter )
{
// Basic outline:
//
// 1. Convert the image to YCrCb.
// 2. Measure Euclidean distances of color in YCrBr to chroma value.
// 3. Categorize pixels:
// * color distances below inner threshold count as foreground; mask value = 0
// * color distances above outer threshold count as background; mask value = 255
// * color distances between inner and outer threshold a linearly interpolated; mask value = [0, 255]
assert( tInner <= tOuter );
// Convert to YCrCb.
assert( ! imageBGR.empty() );
cv::Size imageSize = imageBGR.size();
cv::Mat3b imageYCrCb;
cv::cvtColor( imageBGR, imageYCrCb, cv::COLOR_BGR2YCrCb );
cv::Scalar chromaYCrCb = bgr2ycrcb( chromaBGR ); // Convert a single BGR value to YCrCb.
// Build the mask.
cv::Mat1b mask = cv::Mat1b::zeros( imageSize );
const cv::Vec3d key( chromaYCrCb[ 0 ], chromaYCrCb[ 1 ], chromaYCrCb[ 2 ] );
for ( int y = 0; y < imageSize.height; ++y )
{
for ( int x = 0; x < imageSize.width; ++x )
{
const cv::Vec3d color( imageYCrCb( y, x )[ 0 ], imageYCrCb( y, x )[ 1 ], imageYCrCb( y, x )[ 2 ] );
double distance = cv::norm( key - color );
if ( distance < tInner )
{
// Current pixel is fully part of the background.
mask( y, x ) = 0;
}
else if ( distance > tOuter )
{
// Current pixel is fully part of the foreground.
mask( y, x ) = 255;
}
else
{
// Current pixel is partially part both, fore- and background; interpolate linearly.
// Compute the interpolation factor and clip its value to the range [0, 255].
double d1 = distance - tInner;
double d2 = tOuter - tInner;
uint8_t alpha = static_cast< uint8_t >( 255. * ( d1 / d2 ) );
mask( y, x ) = alpha;
}
}
}
return mask;
}
可以在此 Github Gist 中找到完整的代码示例.
不幸的是,您的示例并未遵循该经验法则。由于前景和 背景仅在强度上有所不同很难(甚至不可能)找到一个单一的全局集 良好分离的参数:
物体周围有黑线但物体内部没有孔(tInner=50, tOuter=90)
物体周围没有黑线,但物体内部有孔(tInner=100,tOuter=170)
因此,如果您无法更进一步地更改图像的背景 需要复杂的方法。然而,快速简单的示例实现有点超出范围,但您可能需要查看 image segmentation 的相关领域和 alpha matting .
关于ios - 从图像中删除背景并仅将图像部分保存在 iOS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400299/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
在Ruby中是否有Gem或安全删除文件的方法?我想避免系统上可能不存在的外部程序。“安全删除”指的是覆盖文件内容。 最佳答案 如果您使用的是*nix,一个很好的方法是使用exec/open3/open4调用shred:`shred-fxuz#{filename}`http://www.gnu.org/s/coreutils/manual/html_node/shred-invocation.html检查这个类似的帖子:Writingafileshredderinpythonorruby?
我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu