Ask Your Question
0

fixed point arithmetic

asked 2013-07-16 08:01:36 -0600

Tim Redfern gravatar image

Hi, I wonder if anyone can help me with a basic question about the way openCV calculates pixel values.

I want to implement alpha masking, using CV_8U4 matrices.

So, if there are 2 input images in1 and in2,

out.R=(in1.R*(255-in2.A))+(in2.R*in2.A)

out.G=(in1.G*(255-in2.A))+(in2.R*in2.A)

out.B=(in1.B*(255-in2.A))+(in2.R*in2.A)

in other words- I want to use a fixed point arithmetic in the same way openGL does, where 0 represents 0.0f and 255 represents 1.0f..

or in other words:

0*0=0
128*128=64
255*255=255

however, in2.R.mul(in2.A) doesn't work like this - and in2.R.mul(in2.A,1.0/255.0) doesn't either.

Is there any way to easily do openGL style pixel operations in openCV?

Thanks

Tim Redfern

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-07-18 09:04:07 -0600

Tim Redfern gravatar image

I managed to achieve this using cv::Mat.mul(Cv::Mat,double), but it turns out to be slower than just using integer arithmetic 'manually'

for reference: the openCV algorithm I used was:

vector<cv::Mat> ichans,ochans;
vector<cv::Mat> compchans;
cv::split(rgb,ichans);
cv::split(other.rgb,ochans);
uint8_t b=0xFF;
cv::Mat iA=b-other.alpha;
for (int i=0;i<3;i++) {
    compchans.push_back(ichans[i].mul(iA,1.0/255.0)+ochans[i].mul(other.alpha,1.0/255.0));
}
merge(compchans,rgb);

but, it proved quicker to just do:

for (int i=0;i<w*h*3;i++) {
    rgb.data[i]=(uint8_t)(((((int)rgb.data[i])*(0xFF-other.alpha.data[i/3]))>>8)+((((int)other.rgb.data[i])*((int)other.alpha.data[i/3]))>>8));
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-07-16 08:01:36 -0600

Seen: 1,143 times

Last updated: Jul 18 '13