我正在使用以下open cv方法将UIImage转换为Matrix。
cv::Mat gtpl;// Stores BGRA matrix
UIImage *tplImg = [UIImage imageNamed:@"lion"];
cv::Mat tpl;
UIImageToMat(tplImg, tpl); //Converts Image to Matrix
cv::cvtColor(tpl, gtpl, CV_RGBA2BGRA); // Converts matrix to 4 channels and save into gtpl variable.
//While Adding it to camera's live feed
- (void)processImage:(cv::Mat &)img {
cv::Rect roi( cv::Point(100, 100), cv::Size(gtpl.size()));//Creating rect on which image will be mapped
cv::Mat destinationROI = img( roi );
cv::Mat channels[4];
split(gtpl, channels);
gtpl.copyTo( destinationROI ); //Copying gtpl matrix onto cameras matrix.
}
在相机上生成白色背景的图像。
正如您所看到的,图像的背景显示为白色,但它是透明的。
感谢您的帮助。
答案 0 :(得分:1)
我自己解决了。
cv::Mat overlayImage(const cv::Mat &background, const cv::Mat &foreground,
cv::Mat &output, cv::Point2i location)
{
background.copyTo(output);
// start at the row indicated by location, or at row 0 if location.y is negative.
for(int y = std::max(location.y , 0); y < background.rows; ++y)
{
int fY = y - location.y; // because of the translation
// we are done of we have processed all rows of the foreground image.
if(fY >= foreground.rows)
break;
// start at the column indicated by location,
// or at column 0 if location.x is negative.
for(int x = std::max(location.x, 0); x < background.cols; ++x)
{
int fX = x - location.x; // because of the translation.
// we are done with this row if the column is outside of the foreground image.
if(fX >= foreground.cols)
break;
// determine the opacity of the foregrond pixel, using its fourth (alpha) channel.
double opacity;
try{
opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3]) / 255.;
}
catch(Exception e){
}
// and now combine the background and foreground pixel, using the opacity,
// but only if opacity > 0.
for(int c = 0; opacity > 0 && c < output.channels(); ++c)
{
try{
if (foreground.data != nil)
{
unsigned char foregroundPx =
foreground.data[fY * foreground.step + fX * foreground.channels() + c];
unsigned char backgroundPx =
background.data[y * background.step + x * background.channels() + c];
output.data[y*output.step + output.channels()*x + c] =
backgroundPx * (1.-opacity) + foregroundPx * opacity;
}
}catch(Exception e){
}
}
}
}
return output;
}