void OpenCVPicture::colorDistortion(RNG &rng, int sigma1, int sigma2, int sigma3, int sigma4) { // Call as a final preprocessing step, after any affine transforms and // jiggling. assert(mat.type() % 8 == 5); // float std::vector<float> delta1(mat.channels()); std::vector<float> delta2(mat.channels()); std::vector<float> delta3(mat.channels()); std::vector<float> delta4(mat.channels()); for (int j = 0; j < mat.channels(); j++) { delta1[j] = rng.normal(0, sigma1); delta2[j] = rng.normal(0, sigma2); delta3[j] = rng.normal(0, sigma3); delta4[j] = rng.normal(0, sigma4); } float *matData = ((float *)(mat.data)); for (int y = 0; y < mat.rows; y++) { for (int x = 0; x < mat.cols; x++) { int j = x * mat.channels() + y * mat.channels() * mat.cols; bool interestingPixel = false; for (int i = 0; i < mat.channels(); i++) if (std::abs(matData[i + j] - backgroundColor) > 2) interestingPixel = true; if (interestingPixel) { for (int i = 0; i < mat.channels(); i++) matData[i + j] += delta1[i] + delta2[i] * (matData[i + j] - backgroundColor) + delta3[i] * (x - mat.cols / 2) + delta4[i] * (y - mat.rows / 2); } } } }
void distortImageColor(cv::Mat& mat, RNG& rng, float sigma1, float sigma2, float sigma3, float sigma4) { std::vector<float> delta1(mat.channels()); std::vector<float> delta2(mat.channels()); std::vector<float> delta3(mat.channels()); std::vector<float> delta4(mat.channels()); for (int j=0;j<mat.channels();j++) { delta1[j]=rng.normal(0,sigma1); delta2[j]=rng.normal(0,sigma2); delta3[j]=rng.normal(0,sigma3); delta4[j]=rng.normal(0,sigma4); } int j=0; for (int y=0;y<mat.rows;++y) { for (int x=0;x<mat.cols;++x) { for (int i=0;i<mat.channels();++i) { mat.ptr()[j]=std::max(0,std::min(255, (int)(mat.ptr()[j]+ delta1[i]+ delta2[i]*cos(mat.ptr()[j]*3.1415926535/255)+ delta3[i]*(x-mat.cols/2)+ delta4[i]*(y-mat.rows/2)))); ++j; } } } }