fixed and generalized ocl::blendLinear

This commit is contained in:
Ilya Lavrenov
2013-10-28 23:49:19 +04:00
parent 529f086b62
commit c49c3e0a91
4 changed files with 202 additions and 181 deletions

View File

@@ -47,73 +47,124 @@
using namespace cv;
using namespace cv::ocl;
using namespace cvtest;
using namespace testing;
using namespace std;
#ifdef HAVE_OPENCL
template <typename T>
void blendLinearGold(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &weights1, const cv::Mat &weights2, cv::Mat &result_gold)
static void blendLinearGold(const Mat &img1, const Mat &img2,
const Mat &weights1, const Mat &weights2,
Mat &result_gold)
{
CV_Assert(img1.size() == img2.size() && img1.type() == img2.type());
CV_Assert(weights1.size() == weights2.size() && weights1.size() == img1.size() &&
weights1.type() == CV_32FC1 && weights2.type() == CV_32FC1);
result_gold.create(img1.size(), img1.type());
int cn = img1.channels();
int step1 = img1.cols * img1.channels();
for (int y = 0; y < img1.rows; ++y)
{
const float *weights1_row = weights1.ptr<float>(y);
const float *weights2_row = weights2.ptr<float>(y);
const T *img1_row = img1.ptr<T>(y);
const T *img2_row = img2.ptr<T>(y);
T *result_gold_row = result_gold.ptr<T>(y);
const float * const weights1_row = weights1.ptr<float>(y);
const float * const weights2_row = weights2.ptr<float>(y);
const T * const img1_row = img1.ptr<T>(y);
const T * const img2_row = img2.ptr<T>(y);
T * const result_gold_row = result_gold.ptr<T>(y);
for (int x = 0; x < img1.cols * cn; ++x)
for (int x = 0; x < step1; ++x)
{
float w1 = weights1_row[x / cn];
float w2 = weights2_row[x / cn];
result_gold_row[x] = static_cast<T>((img1_row[x] * w1 + img2_row[x] * w2) / (w1 + w2 + 1e-5f));
int x1 = x / cn;
float w1 = weights1_row[x1], w2 = weights2_row[x1];
result_gold_row[x] = saturate_cast<T>(((float)img1_row[x] * w1
+ (float)img2_row[x] * w2) / (w1 + w2 + 1e-5f));
}
}
}
PARAM_TEST_CASE(Blend, cv::Size, MatType/*, UseRoi*/)
PARAM_TEST_CASE(Blend, MatDepth, int, bool)
{
cv::Size size;
int type;
int depth, channels;
bool useRoi;
Mat src1, src2, weights1, weights2, dst;
Mat src1_roi, src2_roi, weights1_roi, weights2_roi, dst_roi;
oclMat gsrc1, gsrc2, gweights1, gweights2, gdst, gst;
oclMat gsrc1_roi, gsrc2_roi, gweights1_roi, gweights2_roi, gdst_roi;
virtual void SetUp()
{
size = GET_PARAM(0);
type = GET_PARAM(1);
depth = GET_PARAM(0);
channels = GET_PARAM(1);
useRoi = GET_PARAM(2);
}
void random_roi()
{
const int type = CV_MAKE_TYPE(depth, channels);
const double upValue = 1200;
Size roiSize = randomSize(1, 20);
Border src1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(src1, src1_roi, roiSize, src1Border, type, -upValue, upValue);
Border src2Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(src2, src2_roi, roiSize, src2Border, type, -upValue, upValue);
Border weights1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(weights1, weights1_roi, roiSize, weights1Border, CV_32FC1, -upValue, upValue);
Border weights2Border = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(weights2, weights2_roi, roiSize, weights2Border, CV_32FC1, -upValue, upValue);
Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
randomSubMat(dst, dst_roi, roiSize, dstBorder, type, 5, 16);
generateOclMat(gsrc1, gsrc1_roi, src1, roiSize, src1Border);
generateOclMat(gsrc2, gsrc2_roi, src2, roiSize, src2Border);
generateOclMat(gweights1, gweights1_roi, weights1, roiSize, weights1Border);
generateOclMat(gweights2, gweights2_roi, weights2, roiSize, weights2Border);
generateOclMat(gdst, gdst_roi, dst, roiSize, dstBorder);
}
void Near(double eps = 0.0)
{
Mat whole, roi;
gdst.download(whole);
gdst_roi.download(roi);
EXPECT_MAT_NEAR(dst, whole, eps);
EXPECT_MAT_NEAR(dst_roi, roi, eps);
}
};
typedef void (*blendLinearFunc)(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &weights1, const cv::Mat &weights2, cv::Mat &result_gold);
OCL_TEST_P(Blend, Accuracy)
{
int depth = CV_MAT_DEPTH(type);
for (int i = 0; i < LOOP_TIMES; ++i)
{
random_roi();
cv::Mat img1 = randomMat(size, type, 0.0, depth == CV_8U ? 255.0 : 1.0);
cv::Mat img2 = randomMat(size, type, 0.0, depth == CV_8U ? 255.0 : 1.0);
cv::Mat weights1 = randomMat(size, CV_32F, 0, 1);
cv::Mat weights2 = randomMat(size, CV_32F, 0, 1);
cv::ocl::blendLinear(gsrc1_roi, gsrc2_roi, gweights1_roi, gweights2_roi, gdst_roi);
cv::ocl::oclMat gimg1(img1), gimg2(img2), gweights1(weights1), gweights2(weights2);
cv::ocl::oclMat dst;
static blendLinearFunc funcs[] = {
blendLinearGold<uchar>,
blendLinearGold<schar>,
blendLinearGold<ushort>,
blendLinearGold<short>,
blendLinearGold<int>,
blendLinearGold<float>,
};
cv::ocl::blendLinear(gimg1, gimg2, gweights1, gweights2, dst);
cv::Mat result;
cv::Mat result_gold;
dst.download(result);
if (depth == CV_8U)
blendLinearGold<uchar>(img1, img2, weights1, weights2, result_gold);
else
blendLinearGold<float>(img1, img2, weights1, weights2, result_gold);
blendLinearFunc func = funcs[depth];
func(src1_roi, src2_roi, weights1_roi, weights2_roi, dst_roi);
EXPECT_MAT_NEAR(result_gold, result, CV_MAT_DEPTH(type) == CV_8U ? 1.f : 1e-5f);
Near(depth <= CV_32S ? 1.0 : 0.2);
}
}
INSTANTIATE_TEST_CASE_P(OCL_ImgProc, Blend, Combine(
DIFFERENT_SIZES,
testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC4))
));
#endif
INSTANTIATE_TEST_CASE_P(OCL_ImgProc, Blend,
Combine(testing::Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F),
testing::Range(1, 5), Bool()));