From 658c74b6ad73ed0b1e35e7dc9ce47bf2f68c9c81 Mon Sep 17 00:00:00 2001 From: nagayosi Date: Mon, 15 Jul 2019 09:58:09 +0900 Subject: [PATCH] add --- Question_21_30/answers/answer_21.py | 11 ++-- Question_21_30/answers_cpp/answer_25.cpp | 41 ++++++++++++ Question_21_30/answers_cpp/answer_26.cpp | 49 +++++++++++++++ Question_21_30/answers_cpp/answer_27.cpp | 79 ++++++++++++++++++++++++ Tutorial/sample_ex.html | 63 +++++++++++++++++++ 5 files changed, 238 insertions(+), 5 deletions(-) create mode 100644 Question_21_30/answers_cpp/answer_25.cpp create mode 100644 Question_21_30/answers_cpp/answer_26.cpp create mode 100644 Question_21_30/answers_cpp/answer_27.cpp create mode 100644 Tutorial/sample_ex.html diff --git a/Question_21_30/answers/answer_21.py b/Question_21_30/answers/answer_21.py index db35d365..00c47e8b 100644 --- a/Question_21_30/answers/answer_21.py +++ b/Question_21_30/answers/answer_21.py @@ -9,13 +9,14 @@ # Trans [0, 255] a, b = 0., 255. -vmin = img.min() -vmax = img.max() +c = img.min() +d = img.max() out = img.copy() -out[outb] = b -out = (b-a) / (vmax - vmin) * (out - vmin) + a + +out = (b-a) / (d - c) * (out - c) + a +out[out < a] = a +out[out > b] = b out = out.astype(np.uint8) # Display histogram diff --git a/Question_21_30/answers_cpp/answer_25.cpp b/Question_21_30/answers_cpp/answer_25.cpp new file mode 100644 index 00000000..41010349 --- /dev/null +++ b/Question_21_30/answers_cpp/answer_25.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +int main(int argc, const char* argv[]){ + cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); + + int width = img.rows; + int height = img.cols; + + double rx = 1.5, ry = 1.5; + + int resized_width = (int)(width * rx); + int resized_height = (int)(height * ry); + int x_before, y_before; + + cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); + + // nearest neighbor interpolation + for (int y = 0; y < resized_height; y++){ + y_before = (int)round(y / ry); + y_before = fmin(y_before, height - 1); + + for (int x = 0; x < resized_width; x++){ + x_before = (int)round(x / rx); + x_before = fmin(x_before, width - 1); + + for (int k = 0; k < 3; k++){ + out.at(y, x)[k] = img.at(y_before, x_before)[k]; + } + } + } + + //cv::imwrite("out.jpg", out); + cv::imshow("answer", out); + cv::waitKey(0); + cv::destroyAllWindows(); + + return 0; +} diff --git a/Question_21_30/answers_cpp/answer_26.cpp b/Question_21_30/answers_cpp/answer_26.cpp new file mode 100644 index 00000000..697ee279 --- /dev/null +++ b/Question_21_30/answers_cpp/answer_26.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +int main(int argc, const char* argv[]){ + cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); + + int width = img.rows; + int height = img.cols; + + double rx = 1.5, ry = 1.5; + + int resized_width = (int)(width * rx); + int resized_height = (int)(height * ry); + int x_before, y_before; + double dx, dy; + double val; + + cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); + + // bi-linear interpolation + for (int y = 0; y < resized_height; y++){ + y_before = (int)floor(y / ry); + y_before = fmin(y_before, height - 1); + dy = y / ry - y_before; + + for (int x = 0; x < resized_width; x++){ + x_before = (int)floor(x / rx); + x_before = fmin(x_before, width - 1); + dx = x / rx - x_before; + + for (int k = 0; k < 3; k++){ + val = (1. - dx) * (1. - dy) * img.at(y_before, x_before)[k] + + dx * (1. - dy) * img.at(y_before, x_before + 1)[k] + + (1. - dx) * dy * img.at(y_before + 1, x_before)[k] + + dx * dy * img.at(y_before + 1, x_before)[k]; + out.at(y, x)[k] = (uchar)val; + } + } + } + + //cv::imwrite("out.jpg", out); + cv::imshow("answer", out); + cv::waitKey(0); + cv::destroyAllWindows(); + + return 0; +} diff --git a/Question_21_30/answers_cpp/answer_27.cpp b/Question_21_30/answers_cpp/answer_27.cpp new file mode 100644 index 00000000..49c3f794 --- /dev/null +++ b/Question_21_30/answers_cpp/answer_27.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include + +double h(double t){ + double a = -1; + if (fabs(t) <= 1){ + return (a + 2) * pow(fabs(t), 3) - (a + 3) * pow(fabs(t), 2) + 1; + } else if(fabs(t) <= 2){ + return a * pow(fabs(t), 3) - 5 * a * pow(fabs(t), 2) + 8 * a * fabs(t) - 4 * a; + } + return 0; +} + +int val_clip(int x, int min, int max){ + return fmin(fmax(x, min), max); +} + + +int main(int argc, const char* argv[]){ + cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); + + int width = img.rows; + int height = img.cols; + + double rx = 1.5, ry = 1.5; + + int resized_width = (int)(width * rx); + int resized_height = (int)(height * ry); + int x_before, y_before; + double dx, dy, wx, wy, w_sum; + double val; + int _x, _y; + + cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); + + // bi-cubic interpolation + for (int y = 0; y < resized_height; y++){ + dy = y / ry; + y_before = (int)floor(dy); + + for (int x = 0; x < resized_width; x++){ + dx = x / rx; + x_before = (int)floor(dx); + + + for (int k = 0; k < 3; k++){ + w_sum = 0; + val = 0; + + for (int j = -1; j < 3; j++){ + _y = val_clip(y_before + j, 0, height - 1); + wy = h(fabs(dy - _y)); + + for (int i = -1; i < 3; i++){ + _x = val_clip(x_before + i, 0, width - 1); + wx = h(fabs(dx - _x)); + w_sum += wy * wx; + val += (double)img.at(_y, _x)[k] * wx * wy; + } + } + + val /= w_sum; + + val = val_clip(val, 0, 255); + out.at(y, x)[k] = (uchar)val; + } + } + } + + //cv::imwrite("out.jpg", out); + cv::imshow("answer", out); + cv::waitKey(0); + cv::destroyAllWindows(); + + return 0; +} + diff --git a/Tutorial/sample_ex.html b/Tutorial/sample_ex.html new file mode 100644 index 00000000..b6986e2c --- /dev/null +++ b/Tutorial/sample_ex.html @@ -0,0 +1,63 @@ + + + + + Image Processing + + + + +

Image Processing!

+ + + + +
+ + + + + +