我正在开发一个可以进行实时图像处理的iPhone应用程序.其管道中最早的步骤之一是将BGRA图像转换为灰度图像.我尝试了几种不同的方法,时间结果的差异远大于我想象的可能.首先我尝试使用C.我通过添加B + 2*G + R/4来近似转换为光度
void BGRA_To_Byte(Image<BGRA> &imBGRA, Image<byte> &imByte)
{
uchar *pIn = (uchar*) imBGRA.data;
uchar *pLimit = pIn + imBGRA.MemSize();
uchar *pOut = imByte.data;
for(; pIn < pLimit; pIn+=16) // Does four pixels at a time
{
unsigned int sumA = pIn[0] + 2 * pIn[1] + pIn[2];
pOut[0] = sumA / 4;
unsigned int sumB = pIn[4] + 2 * pIn[5] + pIn[6];
pOut[1] = sumB / 4;
unsigned int sumC = pIn[8] + 2 * pIn[9] …
Run Code Online (Sandbox Code Playgroud) NSSet拥有对其对象的强引用,因此Objective C解决方案是使用NSHashTable.weakObjectsHashTable()a la
NSHashTable* mySet = [NSHashTable weakObjectsHashTable];
[mySet addObject:anyOldObject];
[mySet count]; //returns 1
[mySet containsObject:anyOldObject]; //returns true
Run Code Online (Sandbox Code Playgroud)
但是,这似乎没有用
var mySet = NSHashTable.weakObjectsHashTable()
mySet.addObject(anyOldObject)
mySet.count //returns 1
mySet.containsObject(anyOldObject) //returns false
Run Code Online (Sandbox Code Playgroud)
我错过了什么?或者这是一个错误?
Swift似乎没有任何()或所有()的函数所以我试图创建自己的函数.我得到的最接近的是
func any<S: SequenceType
where S.Generator.Element: BooleanType>
(sequence: S) -> Bool{
var __g = sequence.generate()
var first_element = __g.next()
if first_element == nil{
return false
}
return reduce(sequence, first_element!, {$0 || $1})
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据评论,这个功能应该看起来像这样
func any<S: SequenceType
where S.Generator.Element: BooleanType>
(sequence: S) -> Bool{
return reduce(sequence, false, {$0 || $1})
}
Run Code Online (Sandbox Code Playgroud)
编译告诉我
'S.Generator.Element' is not convertible to 'Bool'
Run Code Online (Sandbox Code Playgroud)
在我看来,我明确地告诉编译器它在第二行.我错过了什么?
我正在创建一个简单的管道,从AVCaptureSession获取图像,在OpenCV中处理它们,然后在OpenGL中呈现它们.它基于RosyWriter但没有音频和录制功能.OpenCV处理看起来像
- (void)processPixelBuffer: (CVImageBufferRef)pixelBuffer
{
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
int bufferWidth = CVPixelBufferGetWidth(pixelBuffer);
int bufferHeight = CVPixelBufferGetHeight(pixelBuffer);
unsigned char *pixel = (unsigned char *)CVPixelBufferGetBaseAddress(pixelBuffer);
cv::Mat image = cv::Mat(bufferWidth,bufferHeight,CV_8UC4,pixel);
//do any processing
[self setDisplay_matrix:image];
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,在这个功能中我没有复制任何内存,我想保持这种方式.问题是pixelBuffer可能仍然拥有display_image中包含的内存.处理代码可以分配或不分配新的存储器并将其存储在图像中.如果处理没有分配新内存,我必须使用display_matrix传递pixelBuffer以防止数据被擦除.有没有办法让我拥有记忆的所有权?我想破坏pixelBuffer而不破坏它指向的内存.
在相关的说明中,LockBaseAddress究竟做了什么?如果我传递一个cv :: Mat,CVImageBufferRef对我每次想用cv :: Mat修改/使用数据时都要锁定基地址吗?
我正在研究和ios项目使用Apple LLVM 4.0进行优化编译.我实现了两个不同版本的函数,一个在C中,一个在NEON中.我想测试他们彼此的表现.我的想法是将它们称为相同的次数,然后在Time Profiler中查找它们以查看在每个中花费的相对时间.最初我的代码看起来像
used_value = score_squareNEON(patch, image, current_pos);
used_value = score_squareC(patch, image, current_pos);
Run Code Online (Sandbox Code Playgroud)
当我描述NEON代码根本没有出现的时候.接下来我试过了
for(int i = 0; i < successively_bigger_numbers; i++)
{
used_value = score_squareNEON(patch, image, current_pos);
{
used_value = score_squareC(patch, image, current_pos);
Run Code Online (Sandbox Code Playgroud)
仍然没有NEON代码的贡献.接下来是
used_value = score_squareNEON(patch, image, current_pos);
test = score_squareC(patch, image, current_pos);
Run Code Online (Sandbox Code Playgroud)
测试从未被读过的地方.没有.然后
test = score_squareNEON(patch, image, current_pos);
test = 0;
other_used_variable += test;
used_value = score_squareC(patch, image, current_pos);
Run Code Online (Sandbox Code Playgroud)
依然没有.最终使它执行这两个功能的是
value = score_squareNEON(patch, image, current_pos);
test = score_squareC(patch, image, current_pos);
...
min = (value+test)/2; //before it …
Run Code Online (Sandbox Code Playgroud) 我有一个接收图像的功能和一个切片对象,指定要操作的图像的子区域.我想在指定区域周围绘制一个框以进行调试.绘制框的最简单方法是获取其两个角的坐标.然而,我无法找到从切片对象中获取这些坐标的好方法.
在我定义一个大矩阵并在其上使用我的切片来确定哪些元素受到影响时,当然有一种非常低效的方法.
#given some slice like this
my_slice = np.s_[ymin:ymax+1, xmin:xmax+1]
#recover its dimensions
large_matrix = np.ones((max_height, max_width))
large_matrix[my_slice] = 1
minx = np.min(np.where(large_matrix == 1)[0])
maxx = np.max(np.where(large_matrix == 1)[0])
...
Run Code Online (Sandbox Code Playgroud)
如果这是最好的方法,我可能不得不从传递切片对象切换到某种矩形对象.
ios ×2
swift ×2
assembly ×1
c ×1
c++ ×1
compilation ×1
foundation ×1
iphone ×1
llvm ×1
neon ×1
numpy ×1
objective-c ×1
optimization ×1
python ×1
xcode6 ×1