0
这是我的代码:该算法如何计算rgb颜色?
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// this is the image buffer
CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the image buffer
CVPixelBufferLockBaseAddress(cvimgRef,0);
// access the data
size_t width=CVPixelBufferGetWidth(cvimgRef);
size_t height=CVPixelBufferGetHeight(cvimgRef);
// get the raw image bytes
uint8_t *buf=(uint8_t *) CVPixelBufferGetBaseAddress(cvimgRef);
size_t bprow=CVPixelBufferGetBytesPerRow(cvimgRef);
// and pull out the average rgb value of the frame
float r=0,g=0,b=0;
int test = 0;
for(int y=0; y<height; y++) {
for(int x=0; x<width*4; x+=4) {
b+=buf[x];
g+=buf[x+1];
r+=buf[x+2];
}
buf+=bprow;
test += bprow;
}
r/=255*(float) (width*height);
g/=255*(float) (width*height);
b/=255*(float) (width*height);
//Convert color to HSV
RGBtoHSV(r, g, b, &h, &s, &v);
// Do my stuff...
}
最后行,运行后:R,G,B之间具有值[0,1]。但据我所知,RGB的值从0到255,不是吗?
我觉得最后的操作是得到r,g,b的平均值是吗?以及为什么倍增255?
谢谢。但我不知道为什么要将它转换为[0,1]范围?为什么不按高度*宽度进行分割? (如果r/=(float)(width * height),r将大于255)。 我更新了我的代码。 TY! – hachzz
它们被转换为0-1范围,因为'RGBtoHSV'方法预计RGB值的范围是0-1,而不是0-255,就像'UIColor'方法一样。 – rmaddy
哦,好的,谢谢你rmaddy和科多。 – hachzz