我正在尝试使用此https://github.com/shimat/opencvsharp
将C ++代码移植到C#中这是C ++代码:
void Ball::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 =
((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3])/ 255.;
// 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)
{
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;
}
}
}
}
这是我移植的代码
public void overlayImage(ref Mat background, ref Mat foreground, ref Mat output, Point location)
{
background.CopyTo(output);
// start at the row indicated by location, or at row 0 if location.y is negative.
for (int y = Math.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 = Math.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 =
((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3])/ 255.;
// 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)
{
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;
}
}
}
}
函数overlay基本上是尝试在指定Point(x,y)的背景图像上覆盖png图像(包含alpha图像)。 C ++函数完美地工作,它最初是用opencv 2.4 C ++编码的。端口不完整,这些行越来越错误。 double opacity =((double)foreground.data [fY * front.step + fX * front.channels()+ 3])/ 255.0;
无符号字符前景Px = 前景数据[fY *前景步骤+ fX *前景通道()+ c];