bool YuvToAnyAutoTest(int width, int height, bool is420, View::Format dstType, const Func & f1, const Func & f2) { bool result = true; std::cout << "Test " << f1.description << " & " << f2.description << " [" << width << ", " << height << "]." << std::endl; const int uvWidth = is420 ? width/2 : width; const int uvHeight = is420 ? height/2 : height; View y(width, height, View::Gray8, NULL, TEST_ALIGN(width)); FillRandom(y); View u(uvWidth, uvHeight, View::Gray8, NULL, TEST_ALIGN(uvWidth)); FillRandom(u); View v(uvWidth, uvHeight, View::Gray8, NULL, TEST_ALIGN(uvWidth)); FillRandom(v); View dst1(width, height, dstType, NULL, TEST_ALIGN(width)); View dst2(width, height, dstType, NULL, TEST_ALIGN(width)); TEST_EXECUTE_AT_LEAST_MIN_TIME(f1.Call(y, u, v, dst1)); TEST_EXECUTE_AT_LEAST_MIN_TIME(f2.Call(y, u, v, dst2)); result = result && Compare(dst1, dst2, 0, true, 64); return result; }
int main(int argc, char *argv[]) { //if (argc != 3) return 1; IoChannel src1(argv[1], "r"); IoChannel dst1(argv[2], "w"); IoChannel src2(argv[1], "r"); IoChannel dst2(argv[3], "w"); CopyChannel cc1(src1, dst1); CopyChannel cc2(src2, dst2, 2000); GMainLoop *loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(loop); return 0; }
TEST(algorithmTest, transform_image) { const int width = 10; const int height = 10; cv::Mat_<int> src1(height, width); for(int y=0; y<src1.rows; ++y) { for(int x=0; x<src1.cols; ++x) { src1(y, x) = y*src1.cols + x; } } cv::Mat_<int> src2(height, width); for(int y=0; y<src2.rows; ++y) { for(int x=0; x<src2.cols; ++x) { src2(y, x) = x*src2.cols + y; } } cv::Mat_<int> src3(height, width); for(int y=0; y<src3.rows; ++y) { for(int x=0; x<src3.cols; ++x) { src3(y, x) = x*y; } } cv::Mat_<int> dst1(height, width); cv::Mat_<int> dst2(height, width); cvutil::transform_image(src1, dst1, [](int x){ return x*2; }); for(int y=0; y<dst2.rows; ++y) { for(int x=0; x<dst2.cols; ++x) { dst2(y, x) = src1(y, x)*2; } } ASSERT_TRUE(std::equal(dst2.begin(), dst2.end(), dst2.begin())); cvutil::transform_image(src1, src2, dst1, [](int x, int y){ return x*2 + y*2; }); for(int y=0; y<dst2.rows; ++y) { for(int x=0; x<dst2.cols; ++x) { dst2(y, x) = src1(y, x)*2 + src2(y, x)*2; } } ASSERT_TRUE(std::equal(dst2.begin(), dst2.end(), dst2.begin())); cvutil::transform_image(src1, src2, src3, dst1, [](int x, int y, int z){ return x + y + z; }); for(int y=0; y<dst2.rows; ++y) { for(int x=0; x<dst2.cols; ++x) { dst2(y, x) = src1(y, x) + src2(y, x) + src3(y, x); } } ASSERT_TRUE(std::equal(dst2.begin(), dst2.end(), dst2.begin())); }
bool YuvToAnyDataTest(bool create, int width, int height, bool is420, View::Format dstType, const Func & f) { bool result = true; Data data(f.description); std::cout << (create ? "Create" : "Verify") << " test " << f.description << " [" << width << ", " << height << "]." << std::endl; const int uvWidth = is420 ? width/2 : width; const int uvHeight = is420 ? height/2 : height; View y(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View u(uvWidth, uvHeight, View::Gray8, NULL, TEST_ALIGN(uvWidth)); View v(uvWidth, uvHeight, View::Gray8, NULL, TEST_ALIGN(uvWidth)); View dst1(width, height, dstType, NULL, TEST_ALIGN(width)); View dst2(width, height, dstType, NULL, TEST_ALIGN(width)); if(create) { FillRandom(y); FillRandom(u); FillRandom(v); TEST_SAVE(y); TEST_SAVE(u); TEST_SAVE(v); f.Call(y, u, v, dst1); TEST_SAVE(dst1); } else { TEST_LOAD(y); TEST_LOAD(u); TEST_LOAD(v); TEST_LOAD(dst1); f.Call(y, u, v, dst2); TEST_SAVE(dst2); result = result && Compare(dst1, dst2, 0, true, 64); } return result; }
void copy_channels_node_t::do_process( const render::context_t& context) { Imath::Box2i src1_area( ImathExt::intersect( input_as<image_node_t>(0)->defined(), defined())); image::const_image_view_t src1( input_as<image_node_t>(0)->const_subimage_view( src1_area)); Imath::Box2i src2_area( ImathExt::intersect( input_as<image_node_t>(1)->defined(), defined())); image::const_image_view_t src2( input_as<image_node_t>(1)->const_subimage_view( src2_area)); image::image_view_t dst1( subimage_view( src1_area)); image::image_view_t dst2( subimage_view( src2_area)); int ch = get_value<int>( param( "red")); if( ch == copy_source) copy_channel( src1, 0, dst1, 0); else copy_channel( src2, ch-1, dst2, 0); ch = get_value<int>( param( "green")); if( ch == copy_source) copy_channel( src1, 1, dst1, 1); else copy_channel( src2, ch-1, dst2, 1); ch = get_value<int>( param( "blue")); if( ch == copy_source) copy_channel( src1, 2, dst1, 2); else copy_channel( src2, ch-1, dst2, 2); ch = get_value<int>( param( "alpha")); switch( ch) { case copy_source: copy_channel( src1, 3, dst1, 3); break; case set_one: case set_zero: { Imath::Box2i area( ImathExt::intersect( defined(), format())); copy_channel( src1, ch-1, subimage_view( area), 3); } break; default: copy_channel( src2, ch-1, dst2, 3); } }
int test_layer_reverse() { // Blog: http://blog.csdn.net/fengbingchun/article/details/77160872 #ifdef __linux__ std::string image_name{ "test_data/images/lena.png" }; #else std::string image_name{ "E:/GitCode/CUDA_Test/test_data/images/lena.png" }; #endif cv::Mat matSrc = cv::imread(image_name); CHECK(matSrc.data); cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY); const int width{ 1511 }, height{ 1473 }; const auto length = width * height; cv::resize(matSrc, matSrc, cv::Size(width, height)); cv::Mat matTmp1; matSrc.convertTo(matTmp1, CV_32FC1); float elapsed_time1{ 0.f }, elapsed_time2{ 0.f }; // milliseconds const std::vector<int> vec{ 5, 7}; std::unique_ptr<float[]> dst1(new float[length]), dst2(new float[length]); std::for_each(dst1.get(), dst1.get() + length, [](float& n) {n = 0.f; }); std::for_each(dst2.get(), dst2.get() + length, [](float& n) {n = 0.f; }); int ret = layer_reverse_cpu((float*)matTmp1.data, dst1.get(), length, vec, &elapsed_time1); if (ret != 0) PRINT_ERROR_INFO(image_reverse_cpu); ret = layer_reverse_gpu((float*)matTmp1.data, dst2.get(), length, vec, &elapsed_time2); if (ret != 0) PRINT_ERROR_INFO(image_reverse_gpu); compare_result(dst1.get(), dst2.get(), length); cv::Mat matTmp2(height, width, CV_32FC1, dst2.get()), matDst; matTmp2.convertTo(matDst, CV_8UC1); #ifdef __linux__ save_image(matSrc, matDst, 400, 200, "test_data/images/image_reverse.png"); #else save_image(matSrc, matDst, 400, 200, "E:/GitCode/CUDA_Test/test_data/images/image_reverse.png"); #endif fprintf(stderr, "test layer reverse: cpu run time: %f ms, gpu run time: %f ms\n", elapsed_time1, elapsed_time2); return 0; }
bool AveragingBinarizationDataTest(bool create, int width, int height, SimdCompareType type, const Func2 & f) { bool result = true; Data data(f.description); TEST_LOG_SS(Info, (create ? "Create" : "Verify") << " test " << f.description << " [" << width << ", " << height << "]."); View src(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View dst1(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View dst2(width, height, View::Gray8, NULL, TEST_ALIGN(width)); const uint8_t value = 127; const size_t neighborhood = 17; const uint8_t threshold = 128; const uint8_t positive = 7; const uint8_t negative = 3; if(create) { FillRandom(src); TEST_SAVE(src); f.Call(src, value, neighborhood, threshold, positive, negative, dst1, type); TEST_SAVE(dst1); } else { TEST_LOAD(src); TEST_LOAD(dst1); f.Call(src, value, neighborhood, threshold, positive, negative, dst2, type); TEST_SAVE(dst2); result = result && Compare(dst1, dst2, 0, true, 64); } return result; }
bool ContourMetricsMaskedDataTest(bool create, int width, int height, const FuncM & f) { bool result = true; Data data(f.description); TEST_LOG_SS(Info, (create ? "Create" : "Verify") << " test " << f.description << " [" << width << ", " << height << "]."); View src(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View mask(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View dst1(width, height, View::Int16, NULL, TEST_ALIGN(width)); View dst2(width, height, View::Int16, NULL, TEST_ALIGN(width)); if(create) { FillRandom(src); FillRandom(mask); TEST_SAVE(src); TEST_SAVE(mask); f.Call(src, mask, 128, dst1); TEST_SAVE(dst1); } else { TEST_LOAD(src); TEST_LOAD(mask); TEST_LOAD(dst1); f.Call(src, mask, 128, dst2); TEST_SAVE(dst2); result = result && Compare(dst1, dst2, 0, true, 32, 0); } return result; }
bool AnyToAnyAutoTest(int width, int height, View::Format srcType, View::Format dstType, const Func & f1, const Func & f2) { bool result = true; TEST_LOG_SS(Info, "Test " << f1.description << " & " << f2.description << " for size [" << width << "," << height << "]."); View src(width, height, srcType, NULL, TEST_ALIGN(width)); FillRandom(src); View dst1(width, height, dstType, NULL, TEST_ALIGN(width)); View dst2(width, height, dstType, NULL, TEST_ALIGN(width)); TEST_EXECUTE_AT_LEAST_MIN_TIME(f1.Call(src, dst1)); TEST_EXECUTE_AT_LEAST_MIN_TIME(f2.Call(src, dst2)); result = result && Compare(dst1, dst2, 0, true, 64); return result; }
bool TexturePerformCompensationAutoTest(int width, int height, int shift, const Func4 & f1, const Func4 & f2) { bool result = true; TEST_LOG_SS(Info, "Test " << f1.description << " & " << f2.description << " [" << width << ", " << height << "] <" << shift << ">."); View src(width, height, View::Gray8, NULL, TEST_ALIGN(width)); FillRandom(src); View dst1(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View dst2(width, height, View::Gray8, NULL, TEST_ALIGN(width)); TEST_EXECUTE_AT_LEAST_MIN_TIME(f1.Call(src, shift, dst1)); TEST_EXECUTE_AT_LEAST_MIN_TIME(f2.Call(src, shift, dst2)); result = result && Compare(dst1, dst2, 0, true, 32, 0); return result; }
bool TexturePerformCompensationDataTest(bool create, int width, int height, const Func4 & f) { bool result = true; Data data(f.description); TEST_LOG_SS(Info, (create ? "Create" : "Verify") << " test " << f.description << " [" << width << ", " << height << "]."); View src(width, height, View::Gray8, NULL, TEST_ALIGN(width)); FillRandom(src); View dst1(width, height, View::Gray8, NULL, TEST_ALIGN(width)); View dst2(width, height, View::Gray8, NULL, TEST_ALIGN(width)); const int shift = -7; if(create) { FillRandom(src); TEST_SAVE(src); f.Call(src, shift, dst1); TEST_SAVE(dst1); } else { TEST_LOAD(src); TEST_LOAD(dst1); f.Call(src, shift, dst2); TEST_SAVE(dst2); result = result && Compare(dst1, dst2, 0, true, 32, 0); } return result; }
bool AnyToAnyDataTest(bool create, int width, int height, View::Format srcType, View::Format dstType, const Func & f) { bool result = true; Data data(f.description); TEST_LOG_SS(Info, (create ? "Create" : "Verify") << " test " << f.description << " [" << width << ", " << height << "]."); View src(width, height, srcType, NULL, TEST_ALIGN(width)); View dst1(width, height, dstType, NULL, TEST_ALIGN(width)); View dst2(width, height, dstType, NULL, TEST_ALIGN(width)); if(create) { FillRandom(src); TEST_SAVE(src); f.Call(src, dst1); TEST_SAVE(dst1); } else { TEST_LOAD(src); TEST_LOAD(dst1); f.Call(src, dst2); TEST_SAVE(dst2); result = result && Compare(dst1, dst2, 0, true, 64, 0); } return result; }
TEST(algorithmTest, transform_image_xy) { const int width = 10; const int height = 10; cv::Mat_<int> src1(height, width); for(int y=0; y<src1.rows; ++y) { for(int x=0; x<src1.cols; ++x) { src1(y, x) = y * x; } } cv::Mat_<int> src2(height, width); for(int y=0; y<src2.rows; ++y) { for(int x=0; x<src2.cols; ++x) { src2(y, x) = 2*x + 3*y; } } cv::Mat_<int> dst1(height, width); cvutil::transform_image_xy(src1, dst1, [](int p, int x, int y){ return p + x + 3*y; }); cv::Mat_<int> dst2(height, width); for(int y=0; y<dst2.rows; ++y) { for(int x=0; x<dst2.cols; ++x) { dst2(y, x) = x*y + x + 3*y; } } ASSERT_TRUE(std::equal(dst1.begin(), dst1.end(), dst2.begin())); cvutil::transform_image_xy(src1, src2, dst1, [](int p1, int p2, int x, int y){ return p1*p2 + x + 5*y; }); for(int y=0; y<dst2.rows; ++y) { for(int x=0; x<dst2.cols; ++x) { dst2(y, x) = x*y*(2*x + 3*y) + x + 5*y; } } ASSERT_TRUE(std::equal(dst1.begin(), dst1.end(), dst2.begin())); }
void dng_image::GetRepeat (dng_pixel_buffer &buffer, const dng_rect &srcArea, const dng_rect &dstArea) const { // If we already have the entire srcArea in the // buffer, we can just repeat that. if ((srcArea & buffer.fArea) == srcArea) { buffer.RepeatArea (srcArea, dstArea); } // Else we first need to get the srcArea into the buffer area. else { // Find repeating pattern size. dng_point repeat = srcArea.Size (); // Find pattern phase at top-left corner of destination area. dng_point phase = dng_pixel_buffer::RepeatPhase (srcArea, dstArea); // Find new source area at top-left of dstArea. dng_rect newArea = srcArea + (dstArea.TL () - srcArea.TL ()); // Find quadrant split coordinates. int32 splitV = newArea.t + repeat.v - phase.v; int32 splitH = newArea.l + repeat.h - phase.h; // Top-left quadrant. dng_rect dst1 (dng_rect (newArea.t, newArea.l, splitV, splitH) & dstArea); if (dst1.NotEmpty ()) { dng_pixel_buffer temp (buffer); temp.fArea = dst1 + (srcArea.TL () - dstArea.TL () + dng_point (phase.v, phase.h)); temp.fData = buffer.DirtyPixel (dst1.t, dst1.l, buffer.fPlane); DoGet (temp); } // Top-right quadrant. dng_rect dst2 (dng_rect (newArea.t, splitH, splitV, newArea.r) & dstArea); if (dst2.NotEmpty ()) { dng_pixel_buffer temp (buffer); temp.fArea = dst2 + (srcArea.TL () - dstArea.TL () + dng_point (phase.v, -phase.h)); temp.fData = buffer.DirtyPixel (dst2.t, dst2.l, buffer.fPlane); DoGet (temp); } // Bottom-left quadrant. dng_rect dst3 (dng_rect (splitV, newArea.l, newArea.b, splitH) & dstArea); if (dst3.NotEmpty ()) { dng_pixel_buffer temp (buffer); temp.fArea = dst3 + (srcArea.TL () - dstArea.TL () + dng_point (-phase.v, phase.h)); temp.fData = buffer.DirtyPixel (dst3.t, dst3.l, buffer.fPlane); DoGet (temp); } // Bottom-right quadrant. dng_rect dst4 (dng_rect (splitV, splitH, newArea.b, newArea.r) & dstArea); if (dst4.NotEmpty ()) { dng_pixel_buffer temp (buffer); temp.fArea = dst4 + (srcArea.TL () - dstArea.TL () + dng_point (-phase.v, -phase.h)); temp.fData = buffer.DirtyPixel (dst4.t, dst4.l, buffer.fPlane); DoGet (temp); } // Replicate this new source area. buffer.RepeatArea (newArea, dstArea); } }
ERcExitCode CResourceCompilerHelper::InvokeResourceCompiler(const char* szSrcFilePath, const char* szDstFilePath, const bool bUserDialog) { const char* szAdjustedFilename = szSrcFilePath; #if defined(USE_GAMESTREAM) char szFullSrcPathBuf[ICryPak::g_nMaxPath]; if (gEnv && gEnv->pGameStream) { szAdjustedFilename = gEnv->pCryPak->AdjustFileName(szSrcFilePath, szFullSrcPathBuf, ICryPak::FLAGS_RESOLVE_TO_CACHE); } #endif ERcExitCode eRet = eRcExitCode_Pending; // make command for execution wchar_t szProjectDir[512]; GetCurrentDirectoryW(sizeof(szProjectDir) / sizeof(szProjectDir[0]), szProjectDir); SettingsManagerHelpers::CFixedString<wchar_t, 512> wRemoteCmdLine; SettingsManagerHelpers::CFixedString<wchar_t, 512> wDir; CSettingsManagerTools smTools = CSettingsManagerTools(); const char* const szRcParentDir = (smTools.Is64bitWindows() && (DirectoryExists(L"Bin64/rc") || !DirectoryExists(L"Bin32/rc"))) ? "Bin64" : "Bin32"; wchar_t szRegSettingsBuffer[1024]; smTools.GetEngineSettingsManager()->GetValueByRef("RC_Parameters", SettingsManagerHelpers::CWCharBuffer(szRegSettingsBuffer, sizeof(szRegSettingsBuffer))); wRemoteCmdLine.appendAscii(szRcParentDir); wRemoteCmdLine.appendAscii("/rc/"); wRemoteCmdLine.appendAscii(RC_EXECUTABLE); wRemoteCmdLine.appendAscii(" \""); wRemoteCmdLine.append(szProjectDir); wRemoteCmdLine.appendAscii("\\"); wRemoteCmdLine.appendAscii(szAdjustedFilename); wRemoteCmdLine.appendAscii("\" /userdialog=0 "); wRemoteCmdLine.append(szRegSettingsBuffer); // make it write to a filename of our choice char szDstFilename[512]; char szDstPath[512]; RemovePath(szDstFilePath, szDstFilename, 512); RemoveFilename(szDstFilePath, szDstPath, 512); wRemoteCmdLine.appendAscii(" /overwritefilename=\""); wRemoteCmdLine.appendAscii(szDstFilename); wRemoteCmdLine.appendAscii("\""); wRemoteCmdLine.appendAscii(" /targetroot=\""); wRemoteCmdLine.append(szProjectDir); wRemoteCmdLine.appendAscii("\\"); wRemoteCmdLine.appendAscii(szDstPath); wRemoteCmdLine.appendAscii("\""); wDir.append(szProjectDir); wDir.appendAscii("\\"); wDir.appendAscii(szRcParentDir); wDir.appendAscii("\\rc"); STARTUPINFOW si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.dwX = 100; si.dwY = 100; si.dwFlags = STARTF_USEPOSITION; PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); #if defined(DEBUG) && !defined(NDEBUG) && defined(_RENDERER) extern ILog *iLog; char tmp1[512]; char tmp2[512]; SettingsManagerHelpers::CCharBuffer dst1(tmp1, 512); SettingsManagerHelpers::ConvertUtf16ToUtf8(wDir.c_str(), dst1); SettingsManagerHelpers::CCharBuffer dst2(tmp2, 512); SettingsManagerHelpers::ConvertUtf16ToUtf8(wRemoteCmdLine.c_str(), dst2); iLog->Log("Debug: RC: dir \"%s\", cmd \"%s\"\n", tmp1, tmp2); #endif if (!CreateProcessW( NULL, // No module name (use command line). const_cast<wchar_t*>(wRemoteCmdLine.c_str()), // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. BELOW_NORMAL_PRIORITY_CLASS + (bUserDialog ? 0 : CREATE_NO_WINDOW), // creation flags. NULL, // Use parent's environment block. wDir.c_str(), // Set starting directory. &si, // Pointer to STARTUPINFO structure. &pi)) // Pointer to PROCESS_INFORMATION structure. { eRet = eRcExitCode_FatalError; } else { // Wait until child process exits. WaitForSingleObject(pi.hProcess, INFINITE); DWORD exitCode; if (GetExitCodeProcess(pi.hProcess, &exitCode) == 0) { eRet = eRcExitCode_Error; } else { eRet = (ERcExitCode)exitCode; } } // Close process and thread handles. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return eRet; }
void crossCorr( const Mat& img, const Mat& _templ, Mat& corr, Size corrsize, int ctype, Point anchor, double delta, int borderType ) { const double blockScale = 4.5; const int minBlockSize = 256; std::vector<uchar> buf; Mat templ = _templ; int depth = img.depth(), cn = img.channels(); int tdepth = templ.depth(), tcn = templ.channels(); int cdepth = CV_MAT_DEPTH(ctype), ccn = CV_MAT_CN(ctype); CV_Assert( img.dims <= 2 && templ.dims <= 2 && corr.dims <= 2 ); if( depth != tdepth && tdepth != std::max(CV_32F, depth) ) { _templ.convertTo(templ, std::max(CV_32F, depth)); tdepth = templ.depth(); } CV_Assert( depth == tdepth || tdepth == CV_32F); CV_Assert( corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1 ); CV_Assert( ccn == 1 || delta == 0 ); corr.create(corrsize, ctype); int maxDepth = depth > CV_8S ? CV_64F : std::max(std::max(CV_32F, tdepth), cdepth); Size blocksize, dftsize; blocksize.width = cvRound(templ.cols*blockScale); blocksize.width = std::max( blocksize.width, minBlockSize - templ.cols + 1 ); blocksize.width = std::min( blocksize.width, corr.cols ); blocksize.height = cvRound(templ.rows*blockScale); blocksize.height = std::max( blocksize.height, minBlockSize - templ.rows + 1 ); blocksize.height = std::min( blocksize.height, corr.rows ); dftsize.width = std::max(getOptimalDFTSize(blocksize.width + templ.cols - 1), 2); dftsize.height = getOptimalDFTSize(blocksize.height + templ.rows - 1); if( dftsize.width <= 0 || dftsize.height <= 0 ) CV_Error( CV_StsOutOfRange, "the input arrays are too big" ); // recompute block size blocksize.width = dftsize.width - templ.cols + 1; blocksize.width = MIN( blocksize.width, corr.cols ); blocksize.height = dftsize.height - templ.rows + 1; blocksize.height = MIN( blocksize.height, corr.rows ); Mat dftTempl( dftsize.height*tcn, dftsize.width, maxDepth ); Mat dftImg( dftsize, maxDepth ); int i, k, bufSize = 0; if( tcn > 1 && tdepth != maxDepth ) bufSize = templ.cols*templ.rows*CV_ELEM_SIZE(tdepth); if( cn > 1 && depth != maxDepth ) bufSize = std::max( bufSize, (blocksize.width + templ.cols - 1)* (blocksize.height + templ.rows - 1)*CV_ELEM_SIZE(depth)); if( (ccn > 1 || cn > 1) && cdepth != maxDepth ) bufSize = std::max( bufSize, blocksize.width*blocksize.height*CV_ELEM_SIZE(cdepth)); buf.resize(bufSize); // compute DFT of each template plane for( k = 0; k < tcn; k++ ) { int yofs = k*dftsize.height; Mat src = templ; Mat dst(dftTempl, Rect(0, yofs, dftsize.width, dftsize.height)); Mat dst1(dftTempl, Rect(0, yofs, templ.cols, templ.rows)); if( tcn > 1 ) { src = tdepth == maxDepth ? dst1 : Mat(templ.size(), tdepth, &buf[0]); int pairs[] = {k, 0}; mixChannels(&templ, 1, &src, 1, pairs, 1); } if( dst1.data != src.data ) src.convertTo(dst1, dst1.depth()); if( dst.cols > templ.cols ) { Mat part(dst, Range(0, templ.rows), Range(templ.cols, dst.cols)); part = Scalar::all(0); } dft(dst, dst, 0, templ.rows); } int tileCountX = (corr.cols + blocksize.width - 1)/blocksize.width; int tileCountY = (corr.rows + blocksize.height - 1)/blocksize.height; int tileCount = tileCountX * tileCountY; Size wholeSize = img.size(); Point roiofs(0,0); Mat img0 = img; if( !(borderType & BORDER_ISOLATED) ) { img.locateROI(wholeSize, roiofs); img0.adjustROI(roiofs.y, wholeSize.height-img.rows-roiofs.y, roiofs.x, wholeSize.width-img.cols-roiofs.x); } borderType |= BORDER_ISOLATED; // calculate correlation by blocks for( i = 0; i < tileCount; i++ ) { int x = (i%tileCountX)*blocksize.width; int y = (i/tileCountX)*blocksize.height; Size bsz(std::min(blocksize.width, corr.cols - x), std::min(blocksize.height, corr.rows - y)); Size dsz(bsz.width + templ.cols - 1, bsz.height + templ.rows - 1); int x0 = x - anchor.x + roiofs.x, y0 = y - anchor.y + roiofs.y; int x1 = std::max(0, x0), y1 = std::max(0, y0); int x2 = std::min(img0.cols, x0 + dsz.width); int y2 = std::min(img0.rows, y0 + dsz.height); Mat src0(img0, Range(y1, y2), Range(x1, x2)); Mat dst(dftImg, Rect(0, 0, dsz.width, dsz.height)); Mat dst1(dftImg, Rect(x1-x0, y1-y0, x2-x1, y2-y1)); Mat cdst(corr, Rect(x, y, bsz.width, bsz.height)); for( k = 0; k < cn; k++ ) { Mat src = src0; dftImg = Scalar::all(0); if( cn > 1 ) { src = depth == maxDepth ? dst1 : Mat(y2-y1, x2-x1, depth, &buf[0]); int pairs[] = {k, 0}; mixChannels(&src0, 1, &src, 1, pairs, 1); } if( dst1.data != src.data ) src.convertTo(dst1, dst1.depth()); if( x2 - x1 < dsz.width || y2 - y1 < dsz.height ) copyMakeBorder(dst1, dst, y1-y0, dst.rows-dst1.rows-(y1-y0), x1-x0, dst.cols-dst1.cols-(x1-x0), borderType); dft( dftImg, dftImg, 0, dsz.height ); Mat dftTempl1(dftTempl, Rect(0, tcn > 1 ? k*dftsize.height : 0, dftsize.width, dftsize.height)); mulSpectrums(dftImg, dftTempl1, dftImg, 0, true); dft( dftImg, dftImg, DFT_INVERSE + DFT_SCALE, bsz.height ); src = dftImg(Rect(0, 0, bsz.width, bsz.height)); if( ccn > 1 ) { if( cdepth != maxDepth ) { Mat plane(bsz, cdepth, &buf[0]); src.convertTo(plane, cdepth, 1, delta); src = plane; } int pairs[] = {0, k}; mixChannels(&src, 1, &cdst, 1, pairs, 1); } else { if( k == 0 ) src.convertTo(cdst, cdepth, 1, delta); else { if( maxDepth != cdepth ) { Mat plane(bsz, cdepth, &buf[0]); src.convertTo(plane, cdepth); src = plane; } add(src, cdst, cdst); } } } } }
int test_layer_channel_normalize() { // Blog: http://blog.csdn.net/fengbingchun/article/details/76976024 #ifdef __linux__ std::string image_name{ "test_data/images/lena.png" }; #else std::string image_name{ "E:/GitCode/CUDA_Test/test_data/images/lena.png" }; #endif cv::Mat matSrc = cv::imread(image_name); if (!matSrc.data) { fprintf(stderr, "read image fail: %s\n", image_name.c_str()); return -1; } const int width{ 511 }, height{ 473 }, channels{ 3 }; cv::resize(matSrc, matSrc, cv::Size(width, height)); matSrc.convertTo(matSrc, CV_32FC3); std::vector<cv::Mat> matSplit; cv::split(matSrc, matSplit); CHECK(matSplit.size() == channels); std::unique_ptr<float[]> data(new float[matSplit[0].cols * matSplit[0].rows * channels]); size_t length{ matSplit[0].cols * matSplit[0].rows * sizeof(float) }; for (int i = 0; i < channels; ++i) { memcpy(data.get() + matSplit[0].cols * matSplit[0].rows * i, matSplit[i].data, length); } float elapsed_time1{ 0.f }, elapsed_time2{ 0.f }; // milliseconds std::unique_ptr<float[]> dst1(new float[matSplit[0].cols * matSplit[0].rows * channels]); std::unique_ptr<float[]> dst2(new float[matSplit[0].cols * matSplit[0].rows * channels]); int ret = layer_channel_normalize_cpu(data.get(), dst1.get(), width, height, channels, &elapsed_time1); if (ret != 0) PRINT_ERROR_INFO(image_normalize_cpu); ret = layer_channel_normalize_gpu(data.get(), dst2.get(), width, height, channels, &elapsed_time2); if (ret != 0) PRINT_ERROR_INFO(image_normalize_gpu); int count{ 0 }, num{ width * height * channels }; for (int i = 0; i < num; ++i) { if (fabs(dst1[i] - dst2[i]) > 0.01/*EPS_*/) { fprintf(stderr, "index: %d, val1: %f, val2: %f\n", i, dst1[i], dst2[i]); ++count; } if (count > 100) return -1; } std::vector<cv::Mat> merge(channels); for (int i = 0; i < channels; ++i) { merge[i] = cv::Mat(height, width, CV_32FC1, dst2.get() + i * width * height); } cv::Mat dst3; cv::merge(merge, dst3); dst3.convertTo(dst3, CV_8UC3, 255.f); #ifdef __linux__ cv::imwrite("test_data/images/image_normalize.png", dst3); #else cv::imwrite("E:/GitCode/CUDA_Test/test_data/images/image_normalize.png", dst3); #endif //cv::resize(matSrc, matSrc, cv::Size(width, height)); #ifdef __linux__ //cv::imwrite("test_data/images/image_src.png", matSrc); #else //cv::imwrite("E:/GitCode/CUDA_Test/test_data/images/image_src.png", matSrc); #endif fprintf(stderr, "test layer channel normalize: cpu run time: %f ms, gpu run time: %f ms\n", elapsed_time1, elapsed_time2); return 0; }