diff --git a/Question_01_10/answers_cpp/answer_6.cpp b/Question_01_10/answers_cpp/answer_6.cpp index d41e650e..543a2e17 100644 --- a/Question_01_10/answers_cpp/answer_6.cpp +++ b/Question_01_10/answers_cpp/answer_6.cpp @@ -6,8 +6,8 @@ // Dedcrease color cv::Mat decrease_color(cv::Mat img){ - int height = img.cols; - int width = img.rows; + int width = img.cols; + int height = img.rows; int channel = img.channels(); cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); diff --git a/Question_11_20/answers_cpp/answer_11.cpp b/Question_11_20/answers_cpp/answer_11.cpp old mode 100644 new mode 100755 index 5d12deb5..8415fc3c --- a/Question_11_20/answers_cpp/answer_11.cpp +++ b/Question_11_20/answers_cpp/answer_11.cpp @@ -24,20 +24,25 @@ cv::Mat mean_filter(cv::Mat img, int kernel_size){ for (int y = 0; y < height; y++){ for (int x = 0; x < width; x++){ for (int c = 0; c < channel; c++){ - v = 0; - - // get pixel sum - for (int dy = -pad; dy < pad + 1; dy++){ - for (int dx = -pad; dx < pad + 1; dx++){ - if (((y + dy) >= 0) && ((x + dx) >= 0)){ - v += (int)img.at(y + dy, x + dx)[c]; - } - } - } + v = 0; + count = 0; + + // get pixel sum + for (int dy = -pad; dy < pad + 1; dy++){ + for (int dx = -pad; dx < pad + 1; dx++){ + if (((y + dy) >= 0) && ((x + dx) >= 0) + && ((y + dy) < height) && ((x + dx) < width)){ + v += (int)img.at(y + dy, x + dx)[c]; + count++; + } + } + } - // assign mean value - v /= (kernel_size * kernel_size); - out.at(y, x)[c] = (uchar)v; + // assign mean value + if (count > 0) { + v /= count; + out.at(y, x)[c] = (uchar)v; + } } } } diff --git a/Question_21_30/answers_cpp/answer_23.cpp b/Question_21_30/answers_cpp/answer_23.cpp old mode 100644 new mode 100755 index febfa92e..e9b701b6 --- a/Question_21_30/answers_cpp/answer_23.cpp +++ b/Question_21_30/answers_cpp/answer_23.cpp @@ -17,14 +17,14 @@ cv::Mat histogram_equalization(cv::Mat img){ // histogram equalization hyper-parameters double Zmax = 255; - double hist[255]; + double hist[256]; double S = height * width * channel; int val; double hist_sum = 0; // histogram initialization - for (int i = 0; i < 255; i++){ + for (int i = 0; i < 256; i++){ hist[i] = 0; } @@ -46,7 +46,7 @@ cv::Mat histogram_equalization(cv::Mat img){ // get histogram sum <= current pixel value hist_sum = 0; - for (int l = 0; l < val; l++){ + for (int l = 0; l <= val; l++){ hist_sum += hist[l]; } // assign equalized value diff --git a/Question_21_30/answers_cpp/answer_26.cpp b/Question_21_30/answers_cpp/answer_26.cpp index 291b3c26..e64e2b52 100644 --- a/Question_21_30/answers_cpp/answer_26.cpp +++ b/Question_21_30/answers_cpp/answer_26.cpp @@ -38,7 +38,7 @@ cv::Mat bilinear(cv::Mat img, double rx, double ry){ val = (1. - dx) * (1. - dy) * img.at(y_before, x_before)[c] + dx * (1. - dy) * img.at(y_before, x_before + 1)[c] + (1. - dx) * dy * img.at(y_before + 1, x_before)[c] + - dx * dy * img.at(y_before + 1, x_before)[c]; + dx * dy * img.at(y_before + 1, x_before + 1)[c]; // assign pixel to new position out.at(y, x)[c] = (uchar)val;