From b71cc8bb1b8f43b4339e5865ef2792a0624de9d5 Mon Sep 17 00:00:00 2001 From: Tianshu Cheng Date: Mon, 31 Aug 2015 22:50:38 -0500 Subject: [PATCH] mPb using 3 channels --- CMakeLists.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ FindSTIM.cmake | 9 +++++++++ cudafunc.cu | 6 ++++++ data/101085.bmp | Bin 0 -> 463738 bytes data/101087.bmp | Bin 0 -> 463738 bytes fun_mPb_theta.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ func_mPb.cpp | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gauss_derivative_odd.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ image_contour_detection.h | 5 +++++ test result/0829_01_vs_gradient_theta=0.PNG | Bin 0 -> 197948 bytes test result/0829_02_vs_gradient_theta=0.PNG | Bin 0 -> 389919 bytes test result/0829_03_vs_gradient_theta=0_r=3,5,10.PNG | Bin 0 -> 597470 bytes test result/0829_03_vs_gradient_theta=90_r=3,5,10.PNG | Bin 0 -> 555231 bytes test result/0829_04_compare_colora_theta=90_r=3,5,10.PNG | Bin 0 -> 572535 bytes test result/0829_04_compare_colorb_theta=90_r=3,5,10.PNG | Bin 0 -> 736291 bytes test result/0829_04_compare_vs_gradient_theta=90_r=3,5,10.PNG | Bin 0 -> 718733 bytes test result/0831_01_compare_vs_mPb.PNG | Bin 0 -> 426314 bytes test result/0831_01_compare_vs_mPb_time.PNG | Bin 0 -> 6719 bytes test_main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 19 files changed, 386 insertions(+), 0 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 FindSTIM.cmake create mode 100644 cudafunc.cu create mode 100644 data/101085.bmp create mode 100644 data/101087.bmp create mode 100644 fun_mPb_theta.cpp create mode 100644 func_mPb.cpp create mode 100644 gauss_derivative_odd.cpp create mode 100644 image_contour_detection.h create mode 100644 test result/0829_01_vs_gradient_theta=0.PNG create mode 100644 test result/0829_02_vs_gradient_theta=0.PNG create mode 100644 test result/0829_03_vs_gradient_theta=0_r=3,5,10.PNG create mode 100644 test result/0829_03_vs_gradient_theta=90_r=3,5,10.PNG create mode 100644 test result/0829_04_compare_colora_theta=90_r=3,5,10.PNG create mode 100644 test result/0829_04_compare_colorb_theta=90_r=3,5,10.PNG create mode 100644 test result/0829_04_compare_vs_gradient_theta=90_r=3,5,10.PNG create mode 100644 test result/0831_01_compare_vs_mPb.PNG create mode 100644 test result/0831_01_compare_vs_mPb_time.PNG create mode 100644 test_main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6b65367 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,50 @@ +#Specify the version being used aswell as the language +cmake_minimum_required(VERSION 2.8.11) + +#Name your project here +project(bsds500) + +#set the module directory +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}") + +#set up CUDA +find_package(CUDA REQUIRED) + +#find the STIM library +find_package(STIM REQUIRED) + +#find the pthreads package +find_package(Threads) + +#find the X11 package +find_package(X11) + +include_directories( + ${STIM_INCLUDE_DIRS} + ) + +#Assign source files to the appropriate variables +file(GLOB SRC_CPP "*.cpp") +file(GLOB SRC_H "*.h") +file(GLOB SRC_CU "*.cu") +file(GLOB SRC_CUH "*.cuh") + +#create an executable file +cuda_add_executable(bsds500 + ${SRC_H} + ${SRC_CPP} + ${SRC_CU} + ${SRC_CUH} + ) + +#set the link libraries +target_link_libraries(bsds500 + #${CUDA_cufft_LIBRARY} + #${CUDA_cublas_LIBRARY} + ${CMAKE_THREAD_LIBS_INIT} + ${X11_LIBRARIES} + ) + +#copy an image test case +configure_file(data/101085.bmp 101085.bmp COPYONLY) +configure_file(data/101087.bmp 101087.bmp COPYONLY) diff --git a/FindSTIM.cmake b/FindSTIM.cmake new file mode 100644 index 0000000..700157d --- /dev/null +++ b/FindSTIM.cmake @@ -0,0 +1,9 @@ +include(FindPackageHandleStandardArgs) + +set(STIM_INCLUDE_DIR $ENV{STIMLIB_PATH}) + +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIR) + +if(STIM_FOUND) + set(STIM_INCLUDE_DIRS ${STIM_INCLUDE_DIR}) +endif() \ No newline at end of file diff --git a/cudafunc.cu b/cudafunc.cu new file mode 100644 index 0000000..d2e5890 --- /dev/null +++ b/cudafunc.cu @@ -0,0 +1,6 @@ +#include + +void blur(float* image, float sigma, unsigned int x, unsigned int y){ + + stim::cuda::cpu_gaussian_blur_2d(image, sigma, x, y); +} \ No newline at end of file diff --git a/data/101085.bmp b/data/101085.bmp new file mode 100644 index 0000000..39178df Binary files /dev/null and b/data/101085.bmp differ diff --git a/data/101087.bmp b/data/101087.bmp new file mode 100644 index 0000000..99ddbad Binary files /dev/null and b/data/101087.bmp differ diff --git a/fun_mPb_theta.cpp b/fun_mPb_theta.cpp new file mode 100644 index 0000000..5e3495f --- /dev/null +++ b/fun_mPb_theta.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +stim::image func_mPb_theta(stim::image lab, float theta, unsigned int w, unsigned int h){ + + stim::image mPb_theta(w, h, 1); + + stim::image pic_light, pic_colora, pic_colorb; + pic_light = lab.channel(0); + pic_colora = lab.channel(1); + pic_colorb = lab.channel(2); + + unsigned int N = w * h; + float sigma = 2; + unsigned int sigma_n = 3; + unsigned r1 = 3; + unsigned r2 = 5; + unsigned r3 = 10; + unsigned r4 = 20; + float alpha[9] = {1,1,1,1,1,1,1,1,1}; + + + stim::image l1,l2,l3,a1,a2,a3,b1,b2,b3; + + l1 = gaussian_derivative_filter_odd(pic_light, sigma, sigma_n, r1 * 2, theta, w, h); + l2 = gaussian_derivative_filter_odd(pic_light, sigma, sigma_n, r2 * 2, theta, w, h); + l3 = gaussian_derivative_filter_odd(pic_light, sigma, sigma_n, r3 * 2, theta, w, h); + a1 = gaussian_derivative_filter_odd(pic_colora, sigma, sigma_n, r2 * 2, theta, w, h); + a2 = gaussian_derivative_filter_odd(pic_colora, sigma, sigma_n, r3 * 2, theta, w, h); + a3 = gaussian_derivative_filter_odd(pic_colora, sigma, sigma_n, r4 * 2, theta, w, h); + b1 = gaussian_derivative_filter_odd(pic_colorb, sigma, sigma_n, r2 * 2, theta, w, h); + b2 = gaussian_derivative_filter_odd(pic_colorb, sigma, sigma_n, r3 * 2, theta, w, h); + b3 = gaussian_derivative_filter_odd(pic_colorb, sigma, sigma_n, r4 * 2, theta, w, h); + + for (unsigned i = 0; i +#include +#include +#include +#include +#include + +stim::image func_mPb(stim::image lab, unsigned int theta_n, unsigned int w, unsigned int h){ + + std::clock_t start; + start = std::clock(); + + //---------------pavel's suggesiton------------------------------------ + std::ostringstream ss; + unsigned int N = w * h; + stim::image mPb_theta(w,h), mPb(w,h); + unsigned size = mPb_theta.size(); + memset ( mPb.data(), 0, size * sizeof(float)); + + float* ptr; + ptr = (float*) malloc(size * sizeof(float) * theta_n); + + for (unsigned int n = 0; n < theta_n; n++){ + + ss << "data_output/mPb_theta"<< n << ".bmp"; + float theta = 180 * ((float)n/theta_n); + + mPb_theta = func_mPb_theta(lab, theta, w, h); + //mPb_theta.load("101087.bmp"); + float* ptr_n = &ptr[ n * w * h * 1 ]; + mPb_theta.channel(0).data_noninterleaved(ptr_n); + + double duration1 = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; + std::cout<<"mPb_theta_"<< theta <<" complished time:"<< duration1 <<"s"<<'\n'; + + + unsigned long idx = n * w * h * 1; //index for the nth slice + + std::string sss = ss.str(); + stim::cpu2image(&ptr[idx], sss, w, h, stim::cmBrewer); + + + for(unsigned long i = 0; i < N; i++){ + + float pixel = ptr[i+idx]; //get the ith pixel in nth slice + + if(pixel > mPb.data()[i]){ + mPb.data()[i] = pixel; + } + + else{ + } + } + + + + ss.str(""); + } + + stim::cpu2image(mPb.data(), "data_output/mPb.bmp", w, h, stim::cmBrewer); + + double duration2 = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; + std::cout<<"total time:"<< duration2 <<"s"<<'\n'; + + getch(); + + return mPb; + + //---------------my first method------------------------------------ + /* + std::clock_t start; + start = std::clock(); + + stim::image mPb_stack(w,h,theta_n), mPb(w,h), mPb_theta(w,h), A, B, temp; + float* ptr[8]; + + for (unsigned int n = 0; n < theta_n; n++){ + + //int* x = new int(5); + //int* y = x; + //*y = 1; + + float theta = 180 * ((float)n/theta_n); + mPb_theta = func_mPb_theta(lab, theta, w, h); + mPb_stack.getslice(n) = mPb_theta; + float* ptr[n] = mPb_stack.getslice(n).data(); + + double duration1 = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; + std::cout<<"mPb_theta, theta = "<< theta <<" time:"<< duration1 <<"s"<<'\n'; + + + for(unsigned long i = 0; i < N; i++){ + + *(ptr[n]+i) = mPb_theta.data()[i]; + + + //float a = mPb_theta.data()[i]; + //float* B = ptr[n]+i; + //A.data()[i] = mPb_theta.data()[i]; + //float* C = ptr[0]+1; + //*C = 1; + + // + } + stim::cpu2image(ptr[0], "data_output/mPb_theta.bmp", w, h, stim::cmBrewer); + } + + for (unsigned long i = 0; i < N; i++){ + + mPb.data()[i] = 0; + for (unsigned int n = 0; n < theta_n; n++){ + + float* ptr2 = ptr[i]+n; + float temp = *ptr2; + + if(temp > mPb.data()[i]){ + mPb.data()[i] = temp; + } + else{ + } + } + } + + stim::cpu2image(mPb.data(), "data_output/cmap_mPb.bmp", w, h, stim::cmBrewer); + + double duration2 = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; + std::cout<<"total time:"<< duration2 <<"s"<<'\n'; + + getch(); + + return mPb; */ + + + +} \ No newline at end of file diff --git a/gauss_derivative_odd.cpp b/gauss_derivative_odd.cpp new file mode 100644 index 0000000..77389b2 --- /dev/null +++ b/gauss_derivative_odd.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +#define PI 3.1415926 + +stim::image gaussian_derivative_filter_odd(stim::image image, float sigma, unsigned int sigma_n, unsigned int winsize, float theta, unsigned int w, unsigned int h){ + + stim::image mask_x(winsize+1, winsize+1), mask_y(winsize+1, winsize+1), mask_theta(winsize+1, winsize+1), derivative_x, derivative_y, derivative_theta(w, h); + //float* ptr = mask_x.data(); + + //mask_x.load("101087.bmp"); + //float s[169]; + //float *ptr = s; + + // set parameters + unsigned N = w * h; + float theta_r = (theta * PI)/180; + + float step = (2*sigma*sigma_n)/winsize; + + for (unsigned j = 0; j <= winsize; j++){ + for (unsigned i = 0; i<= winsize; i++){ + + float x = (-1)*sigma*sigma_n + i * step; //range of x + float y = (-1)*sigma*sigma_n + j * step; //range of y + + // create the x-oriented gaussian derivative filter mask_x + mask_x.data()[j*(winsize+1) + i] = (-1) * x * exp((-1)*(pow(x, 2))/(2*pow(sigma, 2))) * exp((-1)*(pow(y, 2))/(2*pow(sigma, 2))); + // create the y-oriented gaussian derivative filter mask_y + mask_y.data()[j*(winsize+1) + i] = (-1) * y * exp((-1)*(pow(y, 2))/(2*pow(sigma, 2))) * exp((-1)*(pow(x, 2))/(2*pow(sigma, 2))); + // create the mask_theta + mask_theta.data()[j*(winsize+1) + i] = cos(theta_r) * mask_x.data()[j*(winsize+1) + i] + sin(theta_r) * mask_y.data()[j*(winsize+1) + i] ; + + } + } + + //stim::cpu2image(mask_x.data(), "data_output/cmapgray_mask_x.bmp", winsize+1, winsize+1, stim::cmBrewer); + + //stim::cpu2image(mask_y.data(), "data_output/cmapgray_mask_y.bmp", winsize+1, winsize+1, stim::cmBrewer); + + + //stim::cpu2image(mask_theta.data(), "data_output/cmapgray_mask_theta.bmp", winsize+1, winsize+1, stim::cmBrewer); + + // 2D convolution + derivative_theta = image.convolve2(mask_theta); + + for (unsigned k = 0; k < w * h; k++){ + + derivative_theta.data()[k] = abs(derivative_theta.data()[k]); + + } + + float max = derivative_theta.max(); + + for (unsigned k = 0; k < w * h; k++){ + + derivative_theta.data()[k] = derivative_theta.data()[k]/max; + + } + + //float max2 = derivative_theta.max(); + + //stim::cpu2image(derivative_theta.data(), "data_output/cmap_colorb_gradient_theta90_r5.bmp", w, h, stim::cmBrewer); + //derivative_x.save("data_output/gradient_x.bmp"); + + return derivative_theta; + +} \ No newline at end of file diff --git a/image_contour_detection.h b/image_contour_detection.h new file mode 100644 index 0000000..57ce07c --- /dev/null +++ b/image_contour_detection.h @@ -0,0 +1,5 @@ +#include +//#include +//#include + +stim::image gaussian_derivative_filter_odd(stim::image image, float sigma, unsigned int sigma_n, unsigned int winsize, float theta, unsigned int w, unsigned int h); \ No newline at end of file diff --git a/test result/0829_01_vs_gradient_theta=0.PNG b/test result/0829_01_vs_gradient_theta=0.PNG new file mode 100644 index 0000000..3e8add3 Binary files /dev/null and b/test result/0829_01_vs_gradient_theta=0.PNG differ diff --git a/test result/0829_02_vs_gradient_theta=0.PNG b/test result/0829_02_vs_gradient_theta=0.PNG new file mode 100644 index 0000000..abfb3d9 Binary files /dev/null and b/test result/0829_02_vs_gradient_theta=0.PNG differ diff --git a/test result/0829_03_vs_gradient_theta=0_r=3,5,10.PNG b/test result/0829_03_vs_gradient_theta=0_r=3,5,10.PNG new file mode 100644 index 0000000..b0da69a Binary files /dev/null and b/test result/0829_03_vs_gradient_theta=0_r=3,5,10.PNG differ diff --git a/test result/0829_03_vs_gradient_theta=90_r=3,5,10.PNG b/test result/0829_03_vs_gradient_theta=90_r=3,5,10.PNG new file mode 100644 index 0000000..b6d017d Binary files /dev/null and b/test result/0829_03_vs_gradient_theta=90_r=3,5,10.PNG differ diff --git a/test result/0829_04_compare_colora_theta=90_r=3,5,10.PNG b/test result/0829_04_compare_colora_theta=90_r=3,5,10.PNG new file mode 100644 index 0000000..be3605d Binary files /dev/null and b/test result/0829_04_compare_colora_theta=90_r=3,5,10.PNG differ diff --git a/test result/0829_04_compare_colorb_theta=90_r=3,5,10.PNG b/test result/0829_04_compare_colorb_theta=90_r=3,5,10.PNG new file mode 100644 index 0000000..ac2ed95 Binary files /dev/null and b/test result/0829_04_compare_colorb_theta=90_r=3,5,10.PNG differ diff --git a/test result/0829_04_compare_vs_gradient_theta=90_r=3,5,10.PNG b/test result/0829_04_compare_vs_gradient_theta=90_r=3,5,10.PNG new file mode 100644 index 0000000..013d7d1 Binary files /dev/null and b/test result/0829_04_compare_vs_gradient_theta=90_r=3,5,10.PNG differ diff --git a/test result/0831_01_compare_vs_mPb.PNG b/test result/0831_01_compare_vs_mPb.PNG new file mode 100644 index 0000000..7df4179 Binary files /dev/null and b/test result/0831_01_compare_vs_mPb.PNG differ diff --git a/test result/0831_01_compare_vs_mPb_time.PNG b/test result/0831_01_compare_vs_mPb_time.PNG new file mode 100644 index 0000000..eb1cde6 Binary files /dev/null and b/test result/0831_01_compare_vs_mPb_time.PNG differ diff --git a/test_main.cpp b/test_main.cpp new file mode 100644 index 0000000..a0fa536 --- /dev/null +++ b/test_main.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + + +void main() +{ + stim::image rgb,gaussgradient; //generate an image object + + rgb.load("101087.bmp"); //load the input image + unsigned int w = rgb.width(); //get the image size + unsigned int h = rgb.height(); + unsigned int s = rgb.size(); + unsigned a = sizeof(float); + + stim::image lab; //create an image object for a single-channel (grayscale) image + lab = rgb.srgb2lab(); //create the single-channel image + + /* + stim::image pic_light, pic_colora, pic_colorb; + pic_light = lab.channel(0); + pic_light.save("pic_light.bmp"); + + pic_colora = lab.channel(1); + pic_colorb = lab.channel(2); + + float sigma = 2; + unsigned int sigma_n = 3; + unsigned int r = 5; + unsigned int winsize = r * 2; //window size = winsize + 1 + float theta = 90; + + gaussgradient = gaussian_derivative_filter_odd(pic_colorb, sigma, sigma_n, winsize, theta, w, h); + gaussgradient.save("data_output/pic_gray_gradient.bmp"); + */ + + //float theta = 0; + unsigned int theta_n = 8; + + //stim::image mPb_stack(w,h,theta_n); + + //stim::image mPb_theta; + //mPb_theta = func_mPb_theta(lab, theta, w, h); + //mPb_theta.save("data_output/pic_gray_gradient.bmp"); + + stim::image mPb; + mPb = func_mPb(lab, theta_n, w, h); + + + +} \ No newline at end of file -- libgit2 0.21.4