├── .DS_Store ├── .ipynb_checkpoints └── Image_processing_tips-checkpoint.ipynb ├── Dataset ├── test │ ├── akahara_0009.jpg │ ├── akahara_0010.jpg │ ├── akahara_0011.jpg │ ├── madara_0009.jpg │ ├── madara_0010.jpg │ └── madara_0011.jpg └── train │ ├── akahara_0004.jpg │ ├── akahara_0005.jpg │ ├── akahara_0007.jpg │ ├── madara_0001.jpg │ ├── madara_0003.jpg │ └── madara_0006.jpg ├── Image_processing_tips.ipynb ├── LICENSE ├── Question_01_10 ├── README.md ├── README.pdf ├── answers_cpp │ ├── answer_1.cpp │ ├── answer_10.cpp │ ├── answer_2.cpp │ ├── answer_3.cpp │ ├── answer_4.cpp │ ├── answer_5.cpp │ ├── answer_6.cpp │ ├── answer_7.cpp │ ├── answer_8.cpp │ └── answer_9.cpp ├── answers_image │ ├── answer_1.jpg │ ├── answer_10.jpg │ ├── answer_2.jpg │ ├── answer_3.jpg │ ├── answer_4.jpg │ ├── answer_5.jpg │ ├── answer_6.jpg │ ├── answer_7.jpg │ ├── answer_8.jpg │ └── answer_9.jpg ├── answers_py │ ├── answer_1.py │ ├── answer_10.py │ ├── answer_2.py │ ├── answer_3.py │ ├── answer_4.py │ ├── answer_5.py │ ├── answer_6.py │ ├── answer_7.py │ ├── answer_8.py │ ├── answer_9.py │ └── out.jpg ├── imori.jpg └── imori_noise.jpg ├── Question_11_20 ├── README.md ├── README.pdf ├── answers_cpp │ ├── answer_11.cpp │ ├── answer_12.cpp │ ├── answer_13.cpp │ ├── answer_14.cpp │ ├── answer_15.cpp │ ├── answer_16.cpp │ ├── answer_17.cpp │ ├── answer_18.cpp │ └── answer_19.cpp ├── answers_image │ ├── answer_11.jpg │ ├── answer_12.jpg │ ├── answer_13.jpg │ ├── answer_14_h.jpg │ ├── answer_14_v.jpg │ ├── answer_15_h.jpg │ ├── answer_15_v.jpg │ ├── answer_16_h.jpg │ ├── answer_16_v.jpg │ ├── answer_17.jpg │ ├── answer_18.jpg │ ├── answer_19.jpg │ └── answer_20.png ├── answers_py │ ├── answer_11.py │ ├── answer_12.py │ ├── answer_13.py │ ├── answer_14.py │ ├── answer_15.py │ ├── answer_16.py │ ├── answer_17.py │ ├── answer_18.py │ ├── answer_19.py │ └── answer_20.py ├── imori.jpg ├── imori_dark.jpg └── imori_noise.jpg ├── Question_21_30 ├── README.md ├── README.pdf ├── answers_cpp │ ├── answer_21.cpp │ ├── answer_22.cpp │ ├── answer_23.cpp │ ├── answer_24.cpp │ ├── answer_25.cpp │ ├── answer_26.cpp │ ├── answer_27.cpp │ ├── answer_28.cpp │ ├── answer_29.cpp │ └── answer_30.cpp ├── answers_image │ ├── answer_21_1.jpg │ ├── answer_21_2.png │ ├── answer_22_1.jpg │ ├── answer_22_2.png │ ├── answer_23_1.jpg │ ├── answer_23_2.png │ ├── answer_24.jpg │ ├── answer_25.jpg │ ├── answer_26.jpg │ ├── answer_27.jpg │ ├── answer_28.jpg │ ├── answer_29_1.jpg │ ├── answer_29_2.jpg │ ├── answer_30_1.jpg │ └── answer_30_2.jpg ├── answers_py │ ├── answer_21.py │ ├── answer_22.py │ ├── answer_23.py │ ├── answer_24.py │ ├── answer_25.py │ ├── answer_26.py │ ├── answer_27.py │ ├── answer_28.py │ ├── answer_29.py │ └── answer_30.py ├── assets │ ├── bci_fig.png │ ├── bli_fig.png │ └── nni_fig.png ├── imori.jpg ├── imori_dark.jpg ├── imori_gamma.jpg ├── out.jpg ├── question_24_1.jpg └── question_24_2.jpg ├── Question_31_40 ├── README.md ├── README.pdf ├── answers_cpp │ ├── answer_31.cpp │ ├── answer_32.cpp │ ├── answer_33.cpp │ ├── answer_34.cpp │ ├── answer_35.cpp │ ├── answer_36.cpp │ ├── answer_37.cpp │ ├── answer_38.cpp │ ├── answer_39.cpp │ └── answer_40.cpp ├── answers_image │ ├── answer_31_1.jpg │ ├── answer_31_2.jpg │ ├── answer_31_3.jpg │ ├── answer_32.jpg │ ├── answer_32_ps.jpg │ ├── answer_33.jpg │ ├── answer_34.jpg │ ├── answer_35.jpg │ ├── answer_36.jpg │ ├── answer_37.jpg │ ├── answer_38.jpg │ ├── answer_39.jpg │ └── answer_40.jpg ├── answers_py │ ├── _answer_38.py │ ├── _answer_40.py │ ├── answer_31.py │ ├── answer_32.py │ ├── answer_33.py │ ├── answer_34.py │ ├── answer_35.py │ ├── answer_36.py │ ├── answer_37.py │ ├── answer_38.py │ ├── answer_39.py │ └── answer_40.py ├── assets │ └── lpf.png ├── imori.jpg └── imori_gray.jpg ├── Question_41_50 ├── README.md ├── README.pdf ├── answers_cpp │ ├── answer_41.cpp │ ├── answer_42.cpp │ ├── answer_43.cpp │ ├── answer_44.cpp │ ├── answer_45.cpp │ ├── answer_46.cpp │ ├── answer_47.cpp │ ├── answer_48.cpp │ ├── answer_49.cpp │ └── answer_50.cpp ├── answers_image │ ├── answer_4.jpg │ ├── answer_41_1.jpg │ ├── answer_41_2.jpg │ ├── answer_42.jpg │ ├── answer_42_1.jpg │ ├── answer_42_2.jpg │ ├── answer_43.jpg │ ├── answer_44.jpg │ ├── answer_45.jpg │ ├── answer_46.jpg │ ├── answer_47.jpg │ ├── answer_48.jpg │ ├── answer_49.jpg │ └── answer_50.jpg ├── answers_py │ ├── answer_41.py │ ├── answer_42.py │ ├── answer_43.py │ ├── answer_44.py │ ├── answer_45.py │ ├── answer_46.py │ ├── answer_47.py │ ├── answer_48.py │ ├── answer_49.py │ └── answer_50.py ├── assets │ ├── canny_erode2.jpg │ ├── morphology_closing.png │ ├── morphology_dilate.png │ ├── morphology_erode.png │ └── morphology_opening.png ├── imori.jpg └── thorino.jpg ├── Question_51_60 ├── README.md ├── README.pdf ├── answers │ ├── answer_4.jpg │ ├── answer_51.jpg │ ├── answer_51.py │ ├── answer_52.jpg │ ├── answer_52.py │ ├── answer_53.jpg │ ├── answer_53.py │ ├── answer_54.jpg │ ├── answer_54.py │ ├── answer_55.jpg │ ├── answer_55.py │ ├── answer_56.jpg │ ├── answer_56.py │ ├── answer_57.jpg │ ├── answer_57.py │ ├── answer_58.png │ ├── answer_58.py │ ├── answer_59.png │ ├── answer_59.py │ ├── answer_60.jpg │ └── answer_60.py ├── imori.jpg ├── imori_part.jpg ├── seg.png └── thorino.jpg ├── Question_61_70 ├── README.md ├── README.pdf ├── answers │ ├── answer_61.png │ ├── answer_61.py │ ├── answer_62.png │ ├── answer_62.py │ ├── answer_63.png │ ├── answer_63.py │ ├── answer_64.png │ ├── answer_64.py │ ├── answer_64_a.py │ ├── answer_65.png │ ├── answer_65.py │ ├── answer_66.py │ ├── answer_66_gra.jpg │ ├── answer_66_mag.jpg │ ├── answer_67.png │ ├── answer_67.py │ ├── answer_68.png │ ├── answer_68.py │ ├── answer_69.jpg │ ├── answer_69.py │ ├── answer_70.png │ └── answer_70.py ├── gazo.png ├── imori.jpg └── renketsu.png ├── Question_71_80 ├── README.md ├── README.pdf ├── answers │ ├── answer_70.png │ ├── answer_71.jpg │ ├── answer_71.py │ ├── answer_72.jpg │ ├── answer_72.py │ ├── answer_72_mask.png │ ├── answer_73.jpg │ ├── answer_73.py │ ├── answer_74.jpg │ ├── answer_74.py │ ├── answer_75.py │ ├── answer_75_1.jpg │ ├── answer_75_16.jpg │ ├── answer_75_2.jpg │ ├── answer_75_32.jpg │ ├── answer_75_4.jpg │ ├── answer_75_8.jpg │ ├── answer_76.jpg │ ├── answer_76.py │ ├── answer_77.jpg │ ├── answer_77.py │ ├── answer_78.png │ ├── answer_78.py │ ├── answer_79.png │ ├── answer_79.py │ ├── answer_80.jpg │ └── answer_80.py └── imori.jpg ├── Question_81_90 ├── README.md ├── README.pdf ├── answers │ ├── answer_81.jpg │ ├── answer_81.py │ ├── answer_82.png │ ├── answer_82.py │ ├── answer_83.jpg │ ├── answer_83.py │ ├── answer_84.png │ ├── answer_84.py │ ├── answer_85.py │ ├── answer_86.py │ ├── answer_87.py │ ├── answer_88.py │ ├── answer_89.py │ └── answer_90.py ├── dataset │ ├── test_akahara_1.jpg │ ├── test_akahara_2.jpg │ ├── test_madara_1.jpg │ ├── test_madara_2.jpg │ ├── train_akahara_1.jpg │ ├── train_akahara_2.jpg │ ├── train_akahara_3.jpg │ ├── train_akahara_4.jpg │ ├── train_akahara_5.jpg │ ├── train_madara_1.jpg │ ├── train_madara_2.jpg │ ├── train_madara_3.jpg │ ├── train_madara_4.jpg │ └── train_madara_5.jpg ├── imori.jpg └── thorino.jpg ├── Question_91_100 ├── .DS_Store ├── README.md ├── README.pdf ├── answers │ ├── _answer_100.py │ ├── _answer_95.py │ ├── _answer_96.py │ ├── _answer_98.py │ ├── _answer_99.py │ ├── answer_100.jpg │ ├── answer_100.py │ ├── answer_100_gt.jpg │ ├── answer_91.jpg │ ├── answer_91.py │ ├── answer_92.jpg │ ├── answer_92.py │ ├── answer_92_k10.jpg │ ├── answer_92_m.jpg │ ├── answer_93.py │ ├── answer_94.jpg │ ├── answer_94.py │ ├── answer_95.py │ ├── answer_96.py │ ├── answer_97.py │ ├── answer_98.jpg │ ├── answer_98.py │ ├── answer_98_2.py │ ├── answer_99.jpg │ └── answer_99.py ├── imori.jpg ├── imori_1.jpg ├── imori_2.jpg ├── imori_many.jpg └── madara.jpg ├── README.md ├── Tutorial ├── README.md ├── README_opencv_c_install.md ├── answer.cpp ├── answer.html ├── assets │ ├── out.jpg │ ├── out_cpp.png │ ├── out_practice.jpg │ ├── sampe_html1.jpg │ ├── sample.jpg │ ├── sample.png │ ├── sample1.png │ ├── sample10.png │ ├── sample2.png │ ├── sample3.png │ ├── sample4.png │ ├── sample5.png │ ├── sample6.png │ ├── sample7.png │ ├── sample8.png │ ├── sample9.png │ └── sample_html2.jpg ├── imori.jpg ├── sample.cpp ├── sample.html └── sample_ex.html ├── assets ├── imori.jpg ├── imori_512x512.jpg └── sample.png ├── requirements.txt └── yobi ├── crop.py ├── crop_hog.py ├── crop_hog_nn.py ├── hog.py ├── neural.py ├── neuralnet.py ├── neuralnet2.py └── nn.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/.DS_Store -------------------------------------------------------------------------------- /Dataset/test/akahara_0009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/test/akahara_0009.jpg -------------------------------------------------------------------------------- /Dataset/test/akahara_0010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/test/akahara_0010.jpg -------------------------------------------------------------------------------- /Dataset/test/akahara_0011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/test/akahara_0011.jpg -------------------------------------------------------------------------------- /Dataset/test/madara_0009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/test/madara_0009.jpg -------------------------------------------------------------------------------- /Dataset/test/madara_0010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/test/madara_0010.jpg -------------------------------------------------------------------------------- /Dataset/test/madara_0011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/test/madara_0011.jpg -------------------------------------------------------------------------------- /Dataset/train/akahara_0004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/train/akahara_0004.jpg -------------------------------------------------------------------------------- /Dataset/train/akahara_0005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/train/akahara_0005.jpg -------------------------------------------------------------------------------- /Dataset/train/akahara_0007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/train/akahara_0007.jpg -------------------------------------------------------------------------------- /Dataset/train/madara_0001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/train/madara_0001.jpg -------------------------------------------------------------------------------- /Dataset/train/madara_0003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/train/madara_0003.jpg -------------------------------------------------------------------------------- /Dataset/train/madara_0006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Dataset/train/madara_0006.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Yoshito Nagaoka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Question_01_10/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/README.pdf -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | // Channel swap 7 | cv::Mat channel_swap(cv::Mat img){ 8 | // get height and width 9 | int width = img.cols; 10 | int height = img.rows; 11 | 12 | // prepare output 13 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 14 | 15 | // each y, x 16 | for (int y = 0; y < height; y++){ 17 | for (int x = 0; x < width; x++){ 18 | // R -> B 19 | out.at(y, x)[0] = img.at(y, x)[2]; 20 | // B -> R 21 | out.at(y, x)[2] = img.at(y, x)[0]; 22 | // G -> G 23 | out.at(y, x)[1] = img.at(y, x)[1]; 24 | } 25 | } 26 | 27 | return out; 28 | } 29 | 30 | 31 | int main(int argc, const char* argv[]){ 32 | // read image 33 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 34 | 35 | // channel swap 36 | cv::Mat out = channel_swap(img); 37 | 38 | //cv::imwrite("out.jpg", out); 39 | cv::imshow("sample", out); 40 | cv::waitKey(0); 41 | cv::destroyAllWindows(); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // median filter 8 | cv::Mat median_filter(cv::Mat img, int kernel_size){ 9 | int height = img.rows; 10 | int width = img.cols; 11 | int channel = img.channels(); 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 15 | 16 | // prepare kernel 17 | int pad = floor(kernel_size / 2); 18 | 19 | // filtering 20 | double v = 0; 21 | int vs[kernel_size * kernel_size]; 22 | int count = 0; 23 | 24 | for (int y = 0; y < height; y++){ 25 | for (int x = 0; x < width; x++){ 26 | for (int c = 0; c < channel; c++){ 27 | v = 0; 28 | count = 0; 29 | 30 | for (int i = 0; i < kernel_size * kernel_size; i++){ 31 | vs[i] = 999; 32 | } 33 | 34 | // get neighbor pixels 35 | for (int dy = -pad; dy < pad + 1; dy++){ 36 | for (int dx = -pad; dx < pad + 1; dx++){ 37 | if (((y + dy) >= 0) && ((x + dx) >= 0)){ 38 | vs[count++] = (int)img.at(y + dy, x + dx)[c]; 39 | } 40 | } 41 | } 42 | 43 | // get and assign median 44 | std::sort(vs, vs + (kernel_size * kernel_size)); 45 | out.at(y, x)[c] = (uchar)vs[int(floor(count / 2)) + 1]; 46 | } 47 | } 48 | } 49 | return out; 50 | } 51 | 52 | int main(int argc, const char* argv[]){ 53 | // read image 54 | cv::Mat img = cv::imread("imori_noise.jpg", cv::IMREAD_COLOR); 55 | 56 | // median filter 57 | cv::Mat out = median_filter(img, 3); 58 | 59 | //cv::imwrite("out.jpg", out); 60 | cv::imshow("answer", out); 61 | cv::waitKey(0); 62 | cv::destroyAllWindows(); 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | // BGR -> Gray 7 | cv::Mat BGR2GRAY(cv::Mat img){ 8 | // get height and width 9 | int width = img.cols; 10 | int height = img.rows; 11 | 12 | // prepare output 13 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 14 | 15 | // each y, x 16 | for (int y = 0; y < height; y++){ 17 | for (int x = 0; x < width; x++){ 18 | // BGR -> Gray 19 | out.at(y, x) = 0.2126 * (float)img.at(y, x)[2] \ 20 | + 0.7152 * (float)img.at(y, x)[1] \ 21 | + 0.0722 * (float)img.at(y, x)[0]; 22 | } 23 | } 24 | 25 | return out; 26 | } 27 | 28 | 29 | int main(int argc, const char* argv[]){ 30 | // read image 31 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 32 | 33 | // BGR -> Gray 34 | cv::Mat out = BGR2GRAY(img); 35 | 36 | //cv::imwrite("out.jpg", out); 37 | cv::imshow("sample", out); 38 | cv::waitKey(0); 39 | cv::destroyAllWindows(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | // BGR -> Gray 7 | cv::Mat BGR2GRAY(cv::Mat img){ 8 | // get height and width 9 | int width = img.cols; 10 | int height = img.rows; 11 | 12 | // prepare output 13 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 14 | 15 | // each y, x 16 | for (int y = 0; y < height; y++){ 17 | for (int x = 0; x < width; x++){ 18 | // BGR -> Gray 19 | out.at(y, x) = 0.2126 * (float)img.at(y, x)[2] \ 20 | + 0.7152 * (float)img.at(y, x)[1] \ 21 | + 0.0722 * (float)img.at(y, x)[0]; 22 | } 23 | } 24 | 25 | return out; 26 | } 27 | 28 | // Gray -> Binary 29 | cv::Mat Binarize(cv::Mat gray, int th){ 30 | int width = gray.cols; 31 | int height = gray.rows; 32 | 33 | // prepare output 34 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 35 | 36 | // each y, x 37 | for (int y = 0; y < height; y++){ 38 | for (int x = 0; x < width; x++){ 39 | // Binarize 40 | if (gray.at(y, x) > th){ 41 | out.at(y, x) = 255; 42 | } else { 43 | out.at(y, x) = 0; 44 | } 45 | 46 | } 47 | } 48 | 49 | return out; 50 | } 51 | 52 | 53 | int main(int argc, const char* argv[]){ 54 | // read image 55 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 56 | 57 | // BGR -> Gray 58 | cv::Mat gray = BGR2GRAY(img); 59 | 60 | // Gray -> Binary 61 | cv::Mat out = Binarize(gray, 128); 62 | 63 | //cv::imwrite("out.jpg", out); 64 | cv::imshow("sample", out); 65 | cv::waitKey(0); 66 | cv::destroyAllWindows(); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // Dedcrease color 8 | cv::Mat decrease_color(cv::Mat img){ 9 | int height = img.cols; 10 | int width = img.rows; 11 | int channel = img.channels(); 12 | 13 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 14 | 15 | for(int y = 0; y < height; y++){ 16 | for(int x = 0; x < width; x++){ 17 | for(int c = 0; c < channel; c++){ 18 | out.at(y, x)[c] = (uchar)(floor((double)img.at(y, x)[c] / 64) * 64 + 32); 19 | } 20 | } 21 | } 22 | return out; 23 | } 24 | 25 | int main(int argc, const char* argv[]){ 26 | // read image 27 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 28 | 29 | // decrease color 30 | cv::Mat out = decrease_color(img); 31 | 32 | //cv::imwrite("out.jpg", out); 33 | cv::imshow("answer", out); 34 | cv::waitKey(0); 35 | cv::destroyAllWindows(); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // average pooling 8 | cv::Mat average_pooling(cv::Mat img){ 9 | int height = img.rows; 10 | int width = img.cols; 11 | int channel = img.channels(); 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 15 | 16 | int r = 8; 17 | double v = 0; 18 | 19 | for (int y = 0; y < height; y+=r){ 20 | for (int x = 0; x < width; x+=r){ 21 | for (int c = 0; c < channel; c++){ 22 | v = 0; 23 | for (int dy = 0; dy < r; dy++){ 24 | for (int dx = 0; dx < r; dx++){ 25 | v += (double)img.at(y + dy, x + dx)[c]; 26 | } 27 | } 28 | v /= (r * r); 29 | for (int dy = 0; dy < r; dy++){ 30 | for (int dx = 0; dx < r; dx++){ 31 | out.at(y + dy, x + dx)[c] = (uchar)v; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | return out; 38 | } 39 | 40 | int main(int argc, const char* argv[]){ 41 | // read image 42 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 43 | 44 | // average pooling 45 | cv::Mat out = average_pooling(img); 46 | 47 | //cv::imwrite("out.jpg", out); 48 | cv::imshow("answer", out); 49 | cv::waitKey(0); 50 | cv::destroyAllWindows(); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // max pooling 8 | cv::Mat max_pooling(cv::Mat img){ 9 | int height = img.rows; 10 | int width = img.cols; 11 | int channel = img.channels(); 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 15 | 16 | int r = 8; 17 | double v = 0; 18 | 19 | for (int y = 0; y < height; y+=r){ 20 | for (int x = 0; x < width; x+=r){ 21 | for (int c = 0; c < channel; c++){ 22 | v = 0; 23 | for (int dy = 0; dy < r; dy++){ 24 | for (int dx = 0; dx < r; dx++){ 25 | v = fmax(img.at(y + dy, x + dx)[c], v); 26 | } 27 | } 28 | 29 | for (int dy = 0; dy < r; dy++){ 30 | for (int dx = 0; dx < r; dx++){ 31 | out.at(y + dy, x + dx)[c] = (uchar)v; 32 | } 33 | } 34 | } 35 | } 36 | } 37 | return out; 38 | } 39 | 40 | int main(int argc, const char* argv[]){ 41 | // read image 42 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 43 | 44 | // max pooling 45 | cv::Mat out = max_pooling(img); 46 | 47 | //cv::imwrite("out.jpg", out); 48 | cv::imshow("answer", out); 49 | cv::waitKey(0); 50 | cv::destroyAllWindows(); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Question_01_10/answers_cpp/answer_9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // gaussian filter 8 | cv::Mat gaussian_filter(cv::Mat img, double sigma, int kernel_size){ 9 | int height = img.rows; 10 | int width = img.cols; 11 | int channel = img.channels(); 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 15 | 16 | // prepare kernel 17 | int pad = floor(kernel_size / 2); 18 | int _x = 0, _y = 0; 19 | double kernel_sum = 0; 20 | 21 | // get gaussian kernel 22 | float kernel[kernel_size][kernel_size]; 23 | 24 | for (int y = 0; y < kernel_size; y++){ 25 | for (int x = 0; x < kernel_size; x++){ 26 | _y = y - pad; 27 | _x = x - pad; 28 | kernel[y][x] = 1 / (2 * M_PI * sigma * sigma) * exp( - (_x * _x + _y * _y) / (2 * sigma * sigma)); 29 | kernel_sum += kernel[y][x]; 30 | } 31 | } 32 | 33 | for (int y = 0; y < kernel_size; y++){ 34 | for (int x = 0; x < kernel_size; x++){ 35 | kernel[y][x] /= kernel_sum; 36 | } 37 | } 38 | 39 | 40 | // filtering 41 | double v = 0; 42 | 43 | for (int y = 0; y < height; y++){ 44 | for (int x = 0; x < width; x++){ 45 | for (int c = 0; c < channel; c++){ 46 | 47 | v = 0; 48 | 49 | for (int dy = -pad; dy < pad + 1; dy++){ 50 | for (int dx = -pad; dx < pad + 1; dx++){ 51 | if (((x + dx) >= 0) && ((y + dy) >= 0)){ 52 | v += (double)img.at(y + dy, x + dx)[c] * kernel[dy + pad][dx + pad]; 53 | } 54 | } 55 | } 56 | out.at(y, x)[c] = v; 57 | } 58 | } 59 | } 60 | return out; 61 | } 62 | 63 | int main(int argc, const char* argv[]){ 64 | // read image 65 | cv::Mat img = cv::imread("imori_noise.jpg", cv::IMREAD_COLOR); 66 | 67 | // gaussian filter 68 | cv::Mat out = gaussian_filter(img, 1.3, 3); 69 | 70 | //cv::imwrite("out.jpg", out); 71 | cv::imshow("answer", out); 72 | cv::waitKey(0); 73 | cv::destroyAllWindows(); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_1.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_10.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_2.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_3.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_4.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_5.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_6.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_7.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_8.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_image/answer_9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_image/answer_9.jpg -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | # function: BGR -> RGB 4 | 5 | 6 | def BGR2RGB(img): 7 | b = img[:, :, 0].copy() 8 | g = img[:, :, 1].copy() 9 | r = img[:, :, 2].copy() 10 | 11 | # RGB > BGR 12 | img[:, :, 0] = r 13 | img[:, :, 1] = g 14 | img[:, :, 2] = b 15 | 16 | return img 17 | 18 | 19 | # Read image 20 | img = cv2.imread("../imori.jpg") 21 | 22 | # BGR -> RGB 23 | img = BGR2RGB(img) 24 | 25 | # Save result 26 | cv2.imwrite("out.jpg", img) 27 | cv2.imshow("result", img) 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_10.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # Median filter 6 | def median_filter(img, K_size=3): 7 | H, W, C = img.shape 8 | 9 | ## Zero padding 10 | pad = K_size // 2 11 | out = np.zeros((H + pad*2, W + pad*2, C), dtype=np.float) 12 | out[pad:pad+H, pad:pad+W] = img.copy().astype(np.float) 13 | 14 | tmp = out.copy() 15 | 16 | # filtering 17 | for y in range(H): 18 | for x in range(W): 19 | for c in range(C): 20 | out[pad+y, pad+x, c] = np.median(tmp[y:y+K_size, x:x+K_size, c]) 21 | 22 | out = out[pad:pad+H, pad:pad+W].astype(np.uint8) 23 | 24 | return out 25 | 26 | 27 | # Read image 28 | img = cv2.imread("imori_noise.jpg") 29 | 30 | 31 | # Median Filter 32 | out = median_filter(img, K_size=3) 33 | 34 | 35 | # Save result 36 | cv2.imwrite("out.jpg", out) 37 | cv2.imshow("result", out) 38 | cv2.waitKey(0) 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Gray scale 5 | def BGR2GRAY(img): 6 | b = img[:, :, 0].copy() 7 | g = img[:, :, 1].copy() 8 | r = img[:, :, 2].copy() 9 | 10 | # Gray scale 11 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 | out = out.astype(np.uint8) 13 | 14 | return out 15 | 16 | 17 | # Read image 18 | img = cv2.imread("imori.jpg").astype(np.float) 19 | 20 | # Grayscale 21 | out = BGR2GRAY(img) 22 | 23 | # Save result 24 | cv2.imwrite("out.jpg", out) 25 | cv2.imshow("result", out) 26 | cv2.waitKey(0) 27 | cv2.destroyAllWindows() 28 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_3.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Gray scale 5 | 6 | 7 | def BGR2GRAY(img): 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | out = out.astype(np.uint8) 15 | 16 | return out 17 | 18 | # binalization 19 | 20 | 21 | def binarization(img, th=128): 22 | img[img < th] = 0 23 | img[img >= th] = 255 24 | return img 25 | 26 | 27 | # Read image 28 | img = cv2.imread("../imori.jpg").astype(np.float32) 29 | 30 | # Grayscale 31 | out = BGR2GRAY(img) 32 | 33 | # Binarization 34 | out = binarization(out) 35 | 36 | # Save result 37 | cv2.imwrite("out.jpg", out) 38 | cv2.imshow("result", out) 39 | cv2.waitKey(0) 40 | cv2.destroyAllWindows() 41 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_4.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # Gray scale 6 | def BGR2GRAY(img): 7 | b = img[:, :, 0].copy() 8 | g = img[:, :, 1].copy() 9 | r = img[:, :, 2].copy() 10 | 11 | # Gray scale 12 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 13 | out = out.astype(np.uint8) 14 | 15 | return out 16 | 17 | # Otsu Binarization 18 | def otsu_binarization(img, th=128): 19 | max_sigma = 0 20 | max_t = 0 21 | 22 | # determine threshold 23 | for _t in range(1, 255): 24 | v0 = out[np.where(out < _t)] 25 | m0 = np.mean(v0) if len(v0) > 0 else 0. 26 | w0 = len(v0) / (H * W) 27 | v1 = out[np.where(out >= _t)] 28 | m1 = np.mean(v1) if len(v1) > 0 else 0. 29 | w1 = len(v1) / (H * W) 30 | sigma = w0 * w1 * ((m0 - m1) ** 2) 31 | if sigma > max_sigma: 32 | max_sigma = sigma 33 | max_t = _t 34 | 35 | # Binarization 36 | print("threshold >>", max_t) 37 | th = max_t 38 | out[out < th] = 0 39 | out[out >= th] = 255 40 | 41 | return out 42 | 43 | 44 | # Read image 45 | img = cv2.imread("imori.jpg").astype(np.float32) 46 | H, W, C =img.shape 47 | 48 | 49 | # Grayscale 50 | out = BGR2GRAY(img) 51 | 52 | # Otsu's binarization 53 | out = otsu_binarization(out) 54 | 55 | # Save result 56 | cv2.imwrite("out.jpg", out) 57 | cv2.imshow("result", out) 58 | cv2.waitKey(0) 59 | cv2.destroyAllWindows() 60 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_5.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # BGR -> HSV 6 | def BGR2HSV(_img): 7 | img = _img.copy() / 255. 8 | 9 | hsv = np.zeros_like(img, dtype=np.float32) 10 | 11 | # get max and min 12 | max_v = np.max(img, axis=2).copy() 13 | min_v = np.min(img, axis=2).copy() 14 | min_arg = np.argmin(img, axis=2) 15 | 16 | # H 17 | hsv[..., 0][np.where(max_v == min_v)]= 0 18 | ## if min == B 19 | ind = np.where(min_arg == 0) 20 | hsv[..., 0][ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 21 | ## if min == R 22 | ind = np.where(min_arg == 2) 23 | hsv[..., 0][ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 24 | ## if min == G 25 | ind = np.where(min_arg == 1) 26 | hsv[..., 0][ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 27 | 28 | # S 29 | hsv[..., 1] = max_v.copy() - min_v.copy() 30 | 31 | # V 32 | hsv[..., 2] = max_v.copy() 33 | 34 | return hsv 35 | 36 | 37 | def HSV2BGR(_img, hsv): 38 | img = _img.copy() / 255. 39 | 40 | # get max and min 41 | max_v = np.max(img, axis=2).copy() 42 | min_v = np.min(img, axis=2).copy() 43 | 44 | out = np.zeros_like(img) 45 | 46 | H = hsv[..., 0] 47 | S = hsv[..., 1] 48 | V = hsv[..., 2] 49 | 50 | C = S 51 | H_ = H / 60. 52 | X = C * (1 - np.abs( H_ % 2 - 1)) 53 | Z = np.zeros_like(H) 54 | 55 | vals = [[Z,X,C], [Z,C,X], [X,C,Z], [C,X,Z], [C,Z,X], [X,Z,C]] 56 | 57 | for i in range(6): 58 | ind = np.where((i <= H_) & (H_ < (i+1))) 59 | out[..., 0][ind] = (V - C)[ind] + vals[i][0][ind] 60 | out[..., 1][ind] = (V - C)[ind] + vals[i][1][ind] 61 | out[..., 2][ind] = (V - C)[ind] + vals[i][2][ind] 62 | 63 | out[np.where(max_v == min_v)] = 0 64 | out = np.clip(out, 0, 1) 65 | out = (out * 255).astype(np.uint8) 66 | 67 | return out 68 | 69 | 70 | # Read image 71 | img = cv2.imread("imori.jpg").astype(np.float32) 72 | 73 | # RGB > HSV 74 | hsv = BGR2HSV(img) 75 | 76 | # Transpose Hue 77 | hsv[..., 0] = (hsv[..., 0] + 180) % 360 78 | 79 | # HSV > RGB 80 | out = HSV2BGR(img, hsv) 81 | 82 | # Save result 83 | cv2.imwrite("out.jpg", out) 84 | cv2.imshow("result", out) 85 | cv2.waitKey(0) 86 | cv2.destroyAllWindows() 87 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_6.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # Dicrease color 6 | def dicrease_color(img): 7 | out = img.copy() 8 | 9 | out = out // 64 * 64 + 32 10 | 11 | return out 12 | 13 | 14 | # Read image 15 | img = cv2.imread("imori.jpg") 16 | 17 | # Dicrease color 18 | out = dicrease_color(img) 19 | 20 | cv2.imwrite("out.jpg", out) 21 | cv2.imshow("result", out) 22 | cv2.waitKey(0) 23 | cv2.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_7.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # average pooling 6 | def average_pooling(img, G=8): 7 | out = img.copy() 8 | 9 | H, W, C = img.shape 10 | Nh = int(H / G) 11 | Nw = int(W / G) 12 | 13 | for y in range(Nh): 14 | for x in range(Nw): 15 | for c in range(C): 16 | out[G*y:G*(y+1), G*x:G*(x+1), c] = np.mean(out[G*y:G*(y+1), G*x:G*(x+1), c]).astype(np.int) 17 | 18 | return out 19 | 20 | 21 | # Read image 22 | img = cv2.imread("imori.jpg") 23 | 24 | # Average Pooling 25 | out = average_pooling(img) 26 | 27 | # Save result 28 | cv2.imwrite("out.jpg", out) 29 | cv2.imshow("result", out) 30 | cv2.waitKey(0) 31 | cv2.destroyAllWindows() 32 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_8.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # max pooling 5 | def max_pooling(img, G=8): 6 | # Max Pooling 7 | out = img.copy() 8 | 9 | H, W, C = img.shape 10 | Nh = int(H / G) 11 | Nw = int(W / G) 12 | 13 | for y in range(Nh): 14 | for x in range(Nw): 15 | for c in range(C): 16 | out[G*y:G*(y+1), G*x:G*(x+1), c] = np.max(out[G*y:G*(y+1), G*x:G*(x+1), c]) 17 | 18 | return out 19 | 20 | 21 | # Read image 22 | img = cv2.imread("imori.jpg") 23 | 24 | # Max pooling 25 | out = max_pooling(img) 26 | 27 | # Save result 28 | cv2.imwrite("out.jpg", out) 29 | cv2.imshow("result", out) 30 | cv2.waitKey(0) 31 | cv2.destroyAllWindows() 32 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/answer_9.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # Gaussian filter 6 | def gaussian_filter(img, K_size=3, sigma=1.3): 7 | if len(img.shape) == 3: 8 | H, W, C = img.shape 9 | else: 10 | img = np.expand_dims(img, axis=-1) 11 | H, W, C = img.shape 12 | 13 | 14 | ## Zero padding 15 | pad = K_size // 2 16 | out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float) 17 | out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float) 18 | 19 | ## prepare Kernel 20 | K = np.zeros((K_size, K_size), dtype=np.float) 21 | for x in range(-pad, -pad + K_size): 22 | for y in range(-pad, -pad + K_size): 23 | K[y + pad, x + pad] = np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2))) 24 | K /= (2 * np.pi * sigma * sigma) 25 | K /= K.sum() 26 | 27 | tmp = out.copy() 28 | 29 | # filtering 30 | for y in range(H): 31 | for x in range(W): 32 | for c in range(C): 33 | out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c]) 34 | 35 | out = np.clip(out, 0, 255) 36 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 37 | 38 | return out 39 | 40 | 41 | # Read image 42 | img = cv2.imread("imori_noise.jpg") 43 | 44 | 45 | # Gaussian Filter 46 | out = gaussian_filter(img, K_size=3, sigma=1.3) 47 | 48 | 49 | # Save result 50 | cv2.imwrite("out.jpg", out) 51 | cv2.imshow("result", out) 52 | cv2.waitKey(0) 53 | cv2.destroyAllWindows() 54 | -------------------------------------------------------------------------------- /Question_01_10/answers_py/out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/answers_py/out.jpg -------------------------------------------------------------------------------- /Question_01_10/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/imori.jpg -------------------------------------------------------------------------------- /Question_01_10/imori_noise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_01_10/imori_noise.jpg -------------------------------------------------------------------------------- /Question_11_20/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/README.pdf -------------------------------------------------------------------------------- /Question_11_20/answers_cpp/answer_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // mean filter 8 | cv::Mat mean_filter(cv::Mat img, int kernel_size){ 9 | int height = img.rows; 10 | int width = img.cols; 11 | int channel = img.channels(); 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 15 | 16 | // prepare kernel 17 | int pad = floor(kernel_size / 2); 18 | 19 | // filtering 20 | double v = 0; 21 | int vs[kernel_size * kernel_size]; 22 | int count = 0; 23 | 24 | for (int y = 0; y < height; y++){ 25 | for (int x = 0; x < width; x++){ 26 | for (int c = 0; c < channel; c++){ 27 | v = 0; 28 | 29 | // get pixel sum 30 | for (int dy = -pad; dy < pad + 1; dy++){ 31 | for (int dx = -pad; dx < pad + 1; dx++){ 32 | if (((y + dy) >= 0) && ((x + dx) >= 0)){ 33 | v += (int)img.at(y + dy, x + dx)[c]; 34 | } 35 | } 36 | } 37 | 38 | // assign mean value 39 | v /= (kernel_size * kernel_size); 40 | out.at(y, x)[c] = (uchar)v; 41 | } 42 | } 43 | } 44 | return out; 45 | } 46 | 47 | int main(int argc, const char* argv[]){ 48 | // read image 49 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 50 | 51 | // mean filter 52 | cv::Mat out = mean_filter(img, 3); 53 | 54 | //cv::imwrite("out.jpg", out); 55 | cv::imshow("answer", out); 56 | cv::waitKey(0); 57 | cv::destroyAllWindows(); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Question_11_20/answers_cpp/answer_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // motion filter 8 | cv::Mat motion_filter(cv::Mat img, int kernel_size){ 9 | int height = img.rows; 10 | int width = img.cols; 11 | int channel = img.channels(); 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 15 | 16 | // prepare kernel 17 | int pad = floor(kernel_size / 2); 18 | 19 | double kernel[kernel_size][kernel_size];//{{1./3, 0, 0}, {0, 1./3, 0}, {0, 0, 1./3}}; 20 | 21 | for(int y = 0; y < kernel_size; y++){ 22 | for(int x = 0; x < kernel_size; x++){ 23 | if (y == x){ 24 | kernel[y][x] = 1. / kernel_size; 25 | } else { 26 | kernel[y][x] = 0; 27 | } 28 | } 29 | } 30 | 31 | // filtering 32 | double v = 0; 33 | 34 | for (int y = 0; y < height; y++){ 35 | for (int x = 0; x < width; x++){ 36 | for (int c = 0; c < channel; c++){ 37 | 38 | v = 0; 39 | 40 | for (int dy = -pad; dy < pad + 1; dy++){ 41 | for (int dx = -pad; dx < pad + 1; dx++){ 42 | if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){ 43 | v += (double)img.at(y + dy, x + dx)[c] * kernel[dy + pad][dx + pad]; 44 | } 45 | } 46 | } 47 | out.at(y, x)[c] = (uchar)v; 48 | } 49 | } 50 | } 51 | return out; 52 | } 53 | 54 | int main(int argc, const char* argv[]){ 55 | // read image 56 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 57 | 58 | // motion filter 59 | cv::Mat out = motion_filter(img, 3); 60 | 61 | //cv::imwrite("out.jpg", out); 62 | cv::imshow("answer", out); 63 | cv::waitKey(0); 64 | cv::destroyAllWindows(); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Question_11_20/answers_cpp/answer_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | // BGR -> Gray 7 | cv::Mat BGR2GRAY(cv::Mat img){ 8 | // get height and width 9 | int width = img.cols; 10 | int height = img.rows; 11 | 12 | // prepare output 13 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 14 | 15 | // each y, x 16 | for (int y = 0; y < height; y++){ 17 | for (int x = 0; x < width; x++){ 18 | // BGR -> Gray 19 | out.at(y, x) = 0.2126 * (float)img.at(y, x)[2] \ 20 | + 0.7152 * (float)img.at(y, x)[1] \ 21 | + 0.0722 * (float)img.at(y, x)[0]; 22 | } 23 | } 24 | return out; 25 | } 26 | 27 | // max min filter 28 | cv::Mat max_min_filter(cv::Mat img, int kernel_size){ 29 | int height = img.rows; 30 | int width = img.cols; 31 | int channel = img.channels(); 32 | 33 | // prepare output 34 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 35 | 36 | 37 | int pad = floor(kernel_size / 2); 38 | 39 | double vmax = 0, vmin = 999, v = 0; 40 | 41 | // filtering 42 | for (int y = 0; y < height; y++){ 43 | for (int x = 0; x < width; x++){ 44 | vmax = 0; 45 | vmin = 999; 46 | for (int dy = -pad; dy < pad + 1; dy++){ 47 | for (int dx = -pad; dx < pad + 1; dx++){ 48 | if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){ 49 | v = (double)img.at(y + dy, x + dx); 50 | if (v > vmax){ 51 | vmax = v; 52 | } 53 | if (v < vmin){ 54 | vmin = v; 55 | } 56 | } 57 | } 58 | } 59 | out.at(y, x) = (uchar)(vmax - vmin); 60 | } 61 | } 62 | return out; 63 | } 64 | 65 | int main(int argc, const char* argv[]){ 66 | // read image 67 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 68 | 69 | // BGR -> Gray 70 | cv::Mat gray = BGR2GRAY(img); 71 | 72 | // max min filter 73 | cv::Mat out = max_min_filter(gray, 3); 74 | 75 | //cv::imwrite("out.jpg", out); 76 | cv::imshow("answer", out); 77 | cv::waitKey(0); 78 | cv::destroyAllWindows(); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Question_11_20/answers_cpp/answer_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // BGR -> Gray 8 | cv::Mat BGR2GRAY(cv::Mat img){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 15 | 16 | // each y, x 17 | for (int y = 0; y < height; y++){ 18 | for (int x = 0; x < width; x++){ 19 | // BGR -> Gray 20 | out.at(y, x) = 0.2126 * (float)img.at(y, x)[2] \ 21 | + 0.7152 * (float)img.at(y, x)[1] \ 22 | + 0.0722 * (float)img.at(y, x)[0]; 23 | } 24 | } 25 | return out; 26 | } 27 | 28 | // max min filter 29 | cv::Mat diff_filter(cv::Mat img, int kernel_size, bool horizontal){ 30 | int height = img.rows; 31 | int width = img.cols; 32 | int channel = img.channels(); 33 | 34 | // prepare output 35 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 36 | 37 | // prepare kernel 38 | double kernel[kernel_size][kernel_size] = {{0, -1, 0}, {0, 1, 0}, {0, 0, 0}}; 39 | 40 | if (horizontal){ 41 | kernel[0][1] = 0; 42 | kernel[1][0] = -1; 43 | } 44 | 45 | int pad = floor(kernel_size / 2); 46 | 47 | double v = 0; 48 | 49 | // filtering 50 | for (int y = 0; y < height; y++){ 51 | for (int x = 0; x < width; x++){ 52 | v = 0; 53 | for (int dy = -pad; dy < pad + 1; dy++){ 54 | for (int dx = -pad; dx < pad + 1; dx++){ 55 | if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){ 56 | v += img.at(y + dy, x + dx) * kernel[dy + pad][dx + pad]; 57 | } 58 | } 59 | } 60 | v = fmax(v, 0); 61 | v = fmin(v, 255); 62 | out.at(y, x) = (uchar)v; 63 | } 64 | } 65 | return out; 66 | } 67 | 68 | int main(int argc, const char* argv[]){ 69 | // read image 70 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 71 | 72 | // BGR -> Gray 73 | cv::Mat gray = BGR2GRAY(img); 74 | 75 | // diff filter (vertical) 76 | cv::Mat out_v = diff_filter(gray, 3, false); 77 | 78 | // diff filter (horizontal) 79 | cv::Mat out_h = diff_filter(gray, 3, true); 80 | 81 | //cv::imwrite("out.jpg", out); 82 | cv::imshow("answer (vertical)", out_v); 83 | cv::imshow("answer (horizontal)", out_h); 84 | cv::waitKey(0); 85 | cv::destroyAllWindows(); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Question_11_20/answers_cpp/answer_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // BGR -> Gray 8 | cv::Mat BGR2GRAY(cv::Mat img){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 15 | 16 | // each y, x 17 | for (int y = 0; y < height; y++){ 18 | for (int x = 0; x < width; x++){ 19 | // BGR -> Gray 20 | out.at(y, x) = 0.2126 * (float)img.at(y, x)[2] \ 21 | + 0.7152 * (float)img.at(y, x)[1] \ 22 | + 0.0722 * (float)img.at(y, x)[0]; 23 | } 24 | } 25 | return out; 26 | } 27 | 28 | // laplacian filter 29 | cv::Mat laplacian_filter(cv::Mat img, int kernel_size){ 30 | int height = img.rows; 31 | int width = img.cols; 32 | int channel = img.channels(); 33 | 34 | // prepare output 35 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 36 | 37 | // prepare kernel 38 | double kernel[kernel_size][kernel_size] = {{0, 1, 0}, {1, -4, 1}, {0, 1, 0}}; 39 | 40 | int pad = floor(kernel_size / 2); 41 | 42 | double v = 0; 43 | 44 | // filtering 45 | for (int y = 0; y < height; y++){ 46 | for (int x = 0; x < width; x++){ 47 | v = 0; 48 | for (int dy = -pad; dy < pad + 1; dy++){ 49 | for (int dx = -pad; dx < pad + 1; dx++){ 50 | if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){ 51 | v += img.at(y + dy, x + dx) * kernel[dy + pad][dx + pad]; 52 | } 53 | } 54 | } 55 | v = fmax(v, 0); 56 | v = fmin(v, 255); 57 | out.at(y, x) = (uchar)v; 58 | } 59 | } 60 | return out; 61 | } 62 | 63 | int main(int argc, const char* argv[]){ 64 | // read image 65 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 66 | 67 | // BGR -> Gray 68 | cv::Mat gray = BGR2GRAY(img); 69 | 70 | // laplacian filter 71 | cv::Mat out = laplacian_filter(gray, 3); 72 | 73 | //cv::imwrite("out.jpg", out); 74 | cv::imshow("answer", out); 75 | cv::waitKey(0); 76 | cv::destroyAllWindows(); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Question_11_20/answers_cpp/answer_18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // BGR -> Gray 8 | cv::Mat BGR2GRAY(cv::Mat img){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | 13 | // prepare output 14 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 15 | 16 | // each y, x 17 | for (int y = 0; y < height; y++){ 18 | for (int x = 0; x < width; x++){ 19 | // BGR -> Gray 20 | out.at(y, x) = 0.2126 * (float)img.at(y, x)[2] \ 21 | + 0.7152 * (float)img.at(y, x)[1] \ 22 | + 0.0722 * (float)img.at(y, x)[0]; 23 | } 24 | } 25 | return out; 26 | } 27 | 28 | // emboss filter 29 | cv::Mat emboss_filter(cv::Mat img, int kernel_size){ 30 | int height = img.rows; 31 | int width = img.cols; 32 | int channel = img.channels(); 33 | 34 | // prepare output 35 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1); 36 | 37 | // prepare kernel 38 | double kernel[kernel_size][kernel_size] = {{-2, -1, 0}, {-1, 1, 1}, {0, 1, 2}}; 39 | 40 | int pad = floor(kernel_size / 2); 41 | 42 | double v = 0; 43 | 44 | // filtering 45 | for (int y = 0; y < height; y++){ 46 | for (int x = 0; x < width; x++){ 47 | v = 0; 48 | for (int dy = -pad; dy < pad + 1; dy++){ 49 | for (int dx = -pad; dx < pad + 1; dx++){ 50 | if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){ 51 | v += img.at(y + dy, x + dx) * kernel[dy + pad][dx + pad]; 52 | } 53 | } 54 | } 55 | v = fmax(v, 0); 56 | v = fmin(v, 255); 57 | out.at(y, x) = (uchar)v; 58 | } 59 | } 60 | return out; 61 | } 62 | 63 | int main(int argc, const char* argv[]){ 64 | // read image 65 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 66 | 67 | // BGR -> Gray 68 | cv::Mat gray = BGR2GRAY(img); 69 | 70 | // emboss filter 71 | cv::Mat out = emboss_filter(gray, 3); 72 | 73 | //cv::imwrite("out.jpg", out); 74 | cv::imshow("answer", out); 75 | cv::waitKey(0); 76 | cv::destroyAllWindows(); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_11.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_12.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_13.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_14_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_14_h.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_14_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_14_v.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_15_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_15_h.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_15_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_15_v.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_16_h.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_16_h.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_16_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_16_v.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_17.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_18.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_19.jpg -------------------------------------------------------------------------------- /Question_11_20/answers_image/answer_20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/answers_image/answer_20.png -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_11.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # mean filter 5 | def mean_filter(img, K_size=3): 6 | H, W, C = img.shape 7 | 8 | # zero padding 9 | pad = K_size // 2 10 | out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float) 11 | out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float) 12 | tmp = out.copy() 13 | 14 | # filtering 15 | for y in range(H): 16 | for x in range(W): 17 | for c in range(C): 18 | out[pad + y, pad + x, c] = np.mean(tmp[y: y + K_size, x: x + K_size, c]) 19 | 20 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 21 | 22 | return out 23 | 24 | # Read image 25 | img = cv2.imread("imori.jpg") 26 | 27 | # Mean Filter 28 | out = mean_filter(img, K_size=3) 29 | 30 | # Save result 31 | cv2.imwrite("out.jpg", out) 32 | cv2.imshow("result", out) 33 | cv2.waitKey(0) 34 | cv2.destroyAllWindows() 35 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_12.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # motion filter 5 | def motion_filter(img, K_size=3): 6 | H, W, C = img.shape 7 | 8 | # Kernel 9 | K = np.diag( [1] * K_size ).astype(np.float) 10 | K /= K_size 11 | 12 | # zero padding 13 | pad = K_size // 2 14 | out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float) 15 | out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float) 16 | tmp = out.copy() 17 | 18 | # filtering 19 | for y in range(H): 20 | for x in range(W): 21 | for c in range(C): 22 | out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c]) 23 | 24 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 25 | 26 | return out 27 | 28 | 29 | # Read image 30 | img = cv2.imread("imori.jpg") 31 | 32 | # motion filtering 33 | out = motion_filter(img, K_size=3) 34 | 35 | # Save result 36 | cv2.imwrite("out.jpg", out) 37 | cv2.imshow("result", out) 38 | cv2.waitKey(0) 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_13.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Gray scale 5 | 6 | 7 | def BGR2GRAY(img): 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | out = out.astype(np.uint8) 15 | 16 | return out 17 | 18 | # max-min filter 19 | 20 | 21 | def max_min_filter(img, K_size=3): 22 | H, W = img.shape 23 | 24 | # Zero padding 25 | pad = K_size // 2 26 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 27 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 28 | tmp = out.copy() 29 | 30 | # filtering 31 | for y in range(H): 32 | for x in range(W): 33 | out[pad + y, pad + x] = np.max(tmp[y: y + K_size, x: x + K_size]) - \ 34 | np.min(tmp[y: y + K_size, x: x + K_size]) 35 | 36 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 37 | 38 | return out 39 | 40 | 41 | # Read image 42 | img = cv2.imread("../imori.jpg").astype(np.float) 43 | 44 | # grayscale 45 | gray = BGR2GRAY(img) 46 | 47 | # Max-Min filtering 48 | out = max_min_filter(gray, K_size=3) 49 | 50 | # Save result 51 | cv2.imwrite("out.jpg", out) 52 | cv2.imshow("result", out) 53 | cv2.waitKey(0) 54 | cv2.destroyAllWindows() 55 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_14.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Gray scale 5 | def BGR2GRAY(img): 6 | b = img[:, :, 0].copy() 7 | g = img[:, :, 1].copy() 8 | r = img[:, :, 2].copy() 9 | 10 | # Gray scale 11 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 | out = out.astype(np.uint8) 13 | 14 | return out 15 | 16 | # different filter 17 | def different_filter(img, K_size=3): 18 | H, W, C = img.shape 19 | 20 | # Zero padding 21 | pad = K_size // 2 22 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 23 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 24 | tmp = out.copy() 25 | 26 | out_v = out.copy() 27 | out_h = out.copy() 28 | 29 | # vertical kernel 30 | Kv = [[0., -1., 0.],[0., 1., 0.],[0., 0., 0.]] 31 | # horizontal kernel 32 | Kh = [[0., 0., 0.],[-1., 1., 0.], [0., 0., 0.]] 33 | 34 | # filtering 35 | for y in range(H): 36 | for x in range(W): 37 | out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y: y + K_size, x: x + K_size])) 38 | out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y: y + K_size, x: x + K_size])) 39 | 40 | out_v = np.clip(out_v, 0, 255) 41 | out_h = np.clip(out_h, 0, 255) 42 | 43 | out_v = out_v[pad: pad + H, pad: pad + W].astype(np.uint8) 44 | out_h = out_h[pad: pad + H, pad: pad + W].astype(np.uint8) 45 | 46 | return out_v, out_h 47 | 48 | # Read image 49 | img = cv2.imread("imori.jpg").astype(np.float) 50 | 51 | # grayscale 52 | gray = BGR2GRAY(img) 53 | 54 | # different filtering 55 | out_v, out_h = different_filter(gray, K_size=3) 56 | 57 | 58 | 59 | # Save result 60 | cv2.imwrite("out_v.jpg", out_v) 61 | cv2.imshow("result_v", out_v) 62 | while cv2.waitKey(100) != 27:# loop if not get ESC 63 | if cv2.getWindowProperty('result_v',cv2.WND_PROP_VISIBLE) <= 0: 64 | break 65 | cv2.destroyWindow('result_v') 66 | 67 | cv2.imwrite("out_h.jpg", out_h) 68 | cv2.imshow("result_h", out_h) 69 | # loop if not get ESC or click x 70 | while cv2.waitKey(100) != 27: 71 | if cv2.getWindowProperty('result_h',cv2.WND_PROP_VISIBLE) <= 0: 72 | break 73 | cv2.destroyWindow('result_h') 74 | cv2.destroyAllWindows() 75 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_15.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Gray scale 5 | def BGR2GRAY(img): 6 | b = img[:, :, 0].copy() 7 | g = img[:, :, 1].copy() 8 | r = img[:, :, 2].copy() 9 | 10 | # Gray scale 11 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 | out = out.astype(np.uint8) 13 | 14 | return out 15 | 16 | 17 | # sobel filter 18 | def sobel_filter(img, K_size=3): 19 | if len(img.shape) == 3: 20 | H, W, C = img.shape 21 | else: 22 | img = np.expand_dims(img, axis=-1) 23 | H, W, C = img.shape 24 | 25 | # Zero padding 26 | pad = K_size // 2 27 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 28 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 29 | tmp = out.copy() 30 | 31 | out_v = out.copy() 32 | out_h = out.copy() 33 | 34 | ## Sobel vertical 35 | Kv = [[1., 2., 1.],[0., 0., 0.], [-1., -2., -1.]] 36 | ## Sobel horizontal 37 | Kh = [[1., 0., -1.],[2., 0., -2.],[1., 0., -1.]] 38 | 39 | # filtering 40 | for y in range(H): 41 | for x in range(W): 42 | out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y: y + K_size, x: x + K_size])) 43 | out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y: y + K_size, x: x + K_size])) 44 | 45 | out_v = np.clip(out_v, 0, 255) 46 | out_h = np.clip(out_h, 0, 255) 47 | 48 | out_v = out_v[pad: pad + H, pad: pad + W].astype(np.uint8) 49 | out_h = out_h[pad: pad + H, pad: pad + W].astype(np.uint8) 50 | 51 | return out_v, out_h 52 | 53 | # Read image 54 | img = cv2.imread("imori.jpg").astype(np.float) 55 | 56 | # grayscale 57 | gray = BGR2GRAY(img) 58 | 59 | # sobel filtering 60 | out_v, out_h = sobel_filter(gray, K_size=3) 61 | 62 | # Save result 63 | cv2.imwrite("out_v.jpg", out_v) 64 | cv2.imshow("result_v", out_v) 65 | while cv2.waitKey(100) != 27:# loop if not get ESC 66 | if cv2.getWindowProperty('result_v',cv2.WND_PROP_VISIBLE) <= 0: 67 | break 68 | cv2.destroyWindow('result_v') 69 | 70 | cv2.imwrite("out_h.jpg", out_h) 71 | cv2.imshow("result_h", out_h) 72 | # loop if not get ESC or click x 73 | while cv2.waitKey(100) != 27: 74 | if cv2.getWindowProperty('result_h',cv2.WND_PROP_VISIBLE) <= 0: 75 | break 76 | cv2.destroyWindow('result_h') 77 | cv2.destroyAllWindows() 78 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_16.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # Gray scale 6 | def BGR2GRAY(img): 7 | b = img[:, :, 0].copy() 8 | g = img[:, :, 1].copy() 9 | r = img[:, :, 2].copy() 10 | 11 | # Gray scale 12 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 13 | out = out.astype(np.uint8) 14 | 15 | return out 16 | 17 | 18 | # prewitt filter 19 | def prewitt_filter(img, K_size=3): 20 | if len(img.shape) == 3: 21 | H, W, C = img.shape 22 | else: 23 | img = np.expand_dims(img, axis=-1) 24 | H, W, C = img.shape 25 | 26 | # Zero padding 27 | pad = K_size // 2 28 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 29 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 30 | tmp = out.copy() 31 | 32 | out_v = out.copy() 33 | out_h = out.copy() 34 | 35 | ## prewitt vertical kernel 36 | Kv = [[-1., -1., -1.],[0., 0., 0.], [1., 1., 1.]] 37 | ## prewitt horizontal kernel 38 | Kh = [[-1., 0., 1.],[-1., 0., 1.],[-1., 0., 1.]] 39 | 40 | # filtering 41 | for y in range(H): 42 | for x in range(W): 43 | out_v[pad + y, pad + x] = np.sum(Kv * (tmp[y: y + K_size, x: x + K_size])) 44 | out_h[pad + y, pad + x] = np.sum(Kh * (tmp[y: y + K_size, x: x + K_size])) 45 | 46 | 47 | out_v = np.clip(out_v, 0, 255) 48 | out_h = np.clip(out_h, 0, 255) 49 | 50 | out_v = out_v[pad: pad + H, pad: pad + W].astype(np.uint8) 51 | out_h = out_h[pad: pad + H, pad: pad + W].astype(np.uint8) 52 | 53 | return out_v, out_h 54 | 55 | # Read image 56 | img = cv2.imread("imori.jpg").astype(np.float) 57 | 58 | # grayscale 59 | gray = BGR2GRAY(img) 60 | 61 | # prewitt filtering 62 | out_v, out_h = prewitt_filter(gray, K_size=3) 63 | 64 | 65 | # Save result 66 | cv2.imwrite("out_v.jpg", out_v) 67 | cv2.imshow("result_v", out_v) 68 | while cv2.waitKey(100) != 27:# loop if not get ESC 69 | if cv2.getWindowProperty('result_v',cv2.WND_PROP_VISIBLE) <= 0: 70 | break 71 | cv2.destroyWindow('result_v') 72 | 73 | cv2.imwrite("out_h.jpg", out_h) 74 | cv2.imshow("result_h", out_h) 75 | # loop if not get ESC or click x 76 | while cv2.waitKey(100) != 27: 77 | if cv2.getWindowProperty('result_h',cv2.WND_PROP_VISIBLE) <= 0: 78 | break 79 | cv2.destroyWindow('result_h') 80 | cv2.destroyAllWindows() 81 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_17.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | # Gray scale 6 | def BGR2GRAY(img): 7 | b = img[:, :, 0].copy() 8 | g = img[:, :, 1].copy() 9 | r = img[:, :, 2].copy() 10 | 11 | # Gray scale 12 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 13 | out = out.astype(np.uint8) 14 | 15 | return out 16 | 17 | # laplacian filter 18 | def laplacian_filter(img, K_size=3): 19 | H, W, C = img.shape 20 | 21 | # zero padding 22 | pad = K_size // 2 23 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 24 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 25 | tmp = out.copy() 26 | 27 | # laplacian kernle 28 | K = [[0., 1., 0.],[1., -4., 1.], [0., 1., 0.]] 29 | 30 | # filtering 31 | for y in range(H): 32 | for x in range(W): 33 | out[pad + y, pad + x] = np.sum(K * (tmp[y: y + K_size, x: x + K_size])) 34 | 35 | out = np.clip(out, 0, 255) 36 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 37 | 38 | return out 39 | 40 | # Read image 41 | img = cv2.imread("imori.jpg").astype(np.float) 42 | 43 | # grayscale 44 | gray = BGR2GRAY(img) 45 | 46 | # prewitt filtering 47 | out = laplacian_filter(gray, K_size=3) 48 | 49 | 50 | # Save result 51 | cv2.imwrite("out.jpg", out) 52 | cv2.imshow("result", out) 53 | cv2.waitKey(0) 54 | cv2.destroyAllWindows() 55 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_18.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Gray scale 5 | def BGR2GRAY(img): 6 | b = img[:, :, 0].copy() 7 | g = img[:, :, 1].copy() 8 | r = img[:, :, 2].copy() 9 | 10 | # Gray scale 11 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 12 | out = out.astype(np.uint8) 13 | 14 | return out 15 | 16 | # emboss filter 17 | def emboss_filter(img, K_size=3): 18 | H, W, C = img.shape 19 | 20 | # zero padding 21 | pad = K_size // 2 22 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 23 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 24 | tmp = out.copy() 25 | 26 | # emboss kernel 27 | K = [[-2., -1., 0.],[-1., 1., 1.], [0., 1., 2.]] 28 | 29 | # filtering 30 | for y in range(H): 31 | for x in range(W): 32 | out[pad + y, pad + x] = np.sum(K * (tmp[y: y + K_size, x: x + K_size])) 33 | 34 | out = np.clip(out, 0, 255) 35 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 36 | 37 | return out 38 | 39 | 40 | # Read image 41 | img = cv2.imread("imori.jpg").astype(np.float) 42 | 43 | # grayscale 44 | gray = BGR2GRAY(img) 45 | 46 | # emboss filtering 47 | out = emboss_filter(gray, K_size=3) 48 | 49 | 50 | # Save result 51 | cv2.imwrite("out.jpg", out) 52 | cv2.imshow("result", out) 53 | cv2.waitKey(0) 54 | cv2.destroyAllWindows() 55 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_19.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | 6 | # Gray scale 7 | def BGR2GRAY(img): 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | out = out.astype(np.uint8) 15 | 16 | return out 17 | 18 | # LoG filter 19 | def LoG_filter(img, K_size=5, sigma=3): 20 | H, W, C = img.shape 21 | 22 | # zero padding 23 | pad = K_size // 2 24 | out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float) 25 | out[pad: pad + H, pad: pad + W] = gray.copy().astype(np.float) 26 | tmp = out.copy() 27 | 28 | # LoG Kernel 29 | K = np.zeros((K_size, K_size), dtype=np.float) 30 | for x in range(-pad, -pad + K_size): 31 | for y in range(-pad, -pad + K_size): 32 | K[y + pad, x + pad] = (x ** 2 + y ** 2 - sigma ** 2) * np.exp( -(x ** 2 + y ** 2) / (2 * (sigma ** 2))) 33 | K /= (2 * np.pi * (sigma ** 6)) 34 | K /= K.sum() 35 | 36 | # filtering 37 | for y in range(H): 38 | for x in range(W): 39 | out[pad + y, pad + x] = np.sum(K * tmp[y: y + K_size, x: x + K_size]) 40 | 41 | out = np.clip(out, 0, 255) 42 | out = out[pad: pad + H, pad: pad + W].astype(np.uint8) 43 | 44 | return out 45 | 46 | 47 | # Read image 48 | img = cv2.imread("imori_noise.jpg") 49 | 50 | # grayscale 51 | gray = BGR2GRAY(img) 52 | 53 | # LoG filtering 54 | out = LoG_filter(gray, K_size=5, sigma=3) 55 | 56 | # Save result 57 | cv2.imwrite("out.jpg", out) 58 | cv2.imshow("result", out) 59 | cv2.waitKey(0) 60 | cv2.destroyAllWindows() 61 | -------------------------------------------------------------------------------- /Question_11_20/answers_py/answer_20.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori_dark.jpg").astype(np.float) 7 | 8 | # Display histogram 9 | plt.hist(img.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 10 | plt.savefig("out.png") 11 | plt.show() 12 | -------------------------------------------------------------------------------- /Question_11_20/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/imori.jpg -------------------------------------------------------------------------------- /Question_11_20/imori_dark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/imori_dark.jpg -------------------------------------------------------------------------------- /Question_11_20/imori_noise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_11_20/imori_noise.jpg -------------------------------------------------------------------------------- /Question_21_30/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/README.pdf -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // histogram normalization 8 | cv::Mat histogram_normalization(cv::Mat img, int a, int b){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | int c, d; 15 | int val; 16 | 17 | // prepare output 18 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 19 | 20 | // get [c, d] 21 | for (int y = 0; y < height; y++){ 22 | for (int x = 0; x < width; x++){ 23 | for (int _c = 0; _c < channel; _c++){ 24 | val = (float)img.at(y, x)[_c]; 25 | c = fmin(c, val); 26 | d = fmax(d, val); 27 | } 28 | } 29 | } 30 | 31 | // histogram transformation 32 | for (int y = 0; y < height; y++){ 33 | for ( int x = 0; x < width; x++){ 34 | for ( int _c = 0; _c < 3; _c++){ 35 | val = img.at(y, x)[_c]; 36 | 37 | if (val < a){ 38 | out.at(y, x)[_c] = (uchar)a; 39 | } 40 | else if (val <= b){ 41 | out.at(y, x)[_c] = (uchar)((b - a) / (d - c) * (val - c) + a); 42 | } 43 | else { 44 | out.at(y, x)[_c] = (uchar)b; 45 | } 46 | } 47 | } 48 | } 49 | 50 | return out; 51 | } 52 | 53 | 54 | int main(int argc, const char* argv[]){ 55 | // read image 56 | cv::Mat img = cv::imread("imori_dark.jpg", cv::IMREAD_COLOR); 57 | 58 | // histogram normalization 59 | cv::Mat out = histogram_normalization(img, 0, 255); 60 | 61 | //cv::imwrite("out.jpg", out); 62 | cv::imshow("answer", out); 63 | cv::waitKey(0); 64 | cv::destroyAllWindows(); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // histogram transform 8 | cv::Mat histogram_transform(cv::Mat img, int m0, int s0){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | // histogram transformation hyper-parameters 15 | double m, s; 16 | double sum = 0., squared_sum = 0.; 17 | double val; 18 | 19 | // output image 20 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 21 | 22 | // get sum 23 | for (int y = 0; y < height; y++){ 24 | for (int x = 0; x < width; x++){ 25 | for (int c = 0; c < 3; c++){ 26 | val = (float)img.at(y, x)[c]; 27 | sum += val; 28 | squared_sum += (val * val); 29 | } 30 | } 31 | } 32 | 33 | // get standard deviation 34 | m = sum / (height * width * channel); 35 | s = sqrt(squared_sum / (height * width * channel) - m * m); 36 | 37 | 38 | // histogram transformation 39 | for (int y = 0; y < height; y++){ 40 | for ( int x = 0; x < width; x++){ 41 | for ( int c = 0; c < 3; c++){ 42 | val = img.at(y, x)[c]; 43 | 44 | out.at(y, x)[c] = (uchar)(s0 / s * (val - m) + m0); 45 | } 46 | } 47 | } 48 | 49 | return out; 50 | } 51 | 52 | 53 | int main(int argc, const char* argv[]){ 54 | // read image 55 | cv::Mat img = cv::imread("imori_dark.jpg", cv::IMREAD_COLOR); 56 | 57 | // histogram transform 58 | cv::Mat out = histogram_transform(img, 128, 52); 59 | 60 | //cv::imwrite("out.jpg", out); 61 | cv::imshow("answer", out); 62 | cv::waitKey(0); 63 | cv::destroyAllWindows(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | 8 | // histogram equalization 9 | cv::Mat histogram_equalization(cv::Mat img){ 10 | // get height and width 11 | int width = img.cols; 12 | int height = img.rows; 13 | int channel = img.channels(); 14 | 15 | // output image 16 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 17 | 18 | // histogram equalization hyper-parameters 19 | double Zmax = 255; 20 | double hist[255]; 21 | double S = height * width * channel; 22 | int val; 23 | double hist_sum = 0; 24 | 25 | 26 | // histogram initialization 27 | for (int i = 0; i < 255; i++){ 28 | hist[i] = 0; 29 | } 30 | 31 | // get histogram sum 32 | for (int y = 0; y < height; y++){ 33 | for (int x = 0; x < width; x++){ 34 | for (int c = 0; c < channel; c++){ 35 | val = (int)img.at(y, x)[c]; 36 | hist[val] ++; 37 | } 38 | } 39 | } 40 | 41 | // histogram equalization 42 | for (int y = 0; y < height; y++){ 43 | for (int x = 0; x < width; x++){ 44 | for (int c = 0; c < channel; c++){ 45 | val = (int)img.at(y, x)[c]; 46 | 47 | // get histogram sum <= current pixel value 48 | hist_sum = 0; 49 | for (int l = 0; l < val; l++){ 50 | hist_sum += hist[l]; 51 | } 52 | // assign equalized value 53 | out.at(y, x)[c] = (uchar)(Zmax / S * hist_sum); 54 | } 55 | } 56 | } 57 | 58 | return out; 59 | } 60 | 61 | 62 | int main(int argc, const char* argv[]){ 63 | // read image 64 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 65 | 66 | // histogram equalization 67 | cv::Mat out = histogram_equalization(img); 68 | 69 | //cv::imwrite("out.jpg", out); 70 | cv::imshow("answer", out); 71 | cv::waitKey(0); 72 | cv::destroyAllWindows(); 73 | 74 | return 0; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // gamma correction 8 | cv::Mat gamma_correction(cv::Mat img, double gamma_c, double gamma_g){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | // output image 15 | cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3); 16 | 17 | double val; 18 | 19 | // gamma correction 20 | for (int y = 0; y< height; y++){ 21 | for (int x = 0; x < width; x++){ 22 | for (int c = 0; c < channel; c++){ 23 | val = (double)img.at(y, x)[c] / 255; 24 | 25 | out.at(y, x)[c] = (uchar)(pow(val / gamma_c, 1 / gamma_g) * 255); 26 | } 27 | } 28 | } 29 | 30 | return out; 31 | } 32 | 33 | 34 | int main(int argc, const char* argv[]){ 35 | // read image 36 | cv::Mat img = cv::imread("imori_gamma.jpg", cv::IMREAD_COLOR); 37 | 38 | // gamma correction 39 | cv::Mat out = gamma_correction(img, 1, 2.2); 40 | 41 | //cv::imwrite("out.jpg", out); 42 | cv::imshow("answer", out); 43 | cv::waitKey(0); 44 | cv::destroyAllWindows(); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // nearest nieghbor 8 | cv::Mat nearest_neighbor(cv::Mat img, double rx, double ry){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | 15 | // get resized shape 16 | int resized_width = (int)(width * rx); 17 | int resized_height = (int)(height * ry); 18 | int x_before, y_before; 19 | 20 | // output image 21 | cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); 22 | 23 | // nearest neighbor interpolation 24 | for (int y = 0; y < resized_height; y++){ 25 | y_before = (int)round(y / ry); 26 | y_before = fmin(y_before, height - 1); 27 | 28 | for (int x = 0; x < resized_width; x++){ 29 | x_before = (int)round(x / rx); 30 | x_before = fmin(x_before, width - 1); 31 | 32 | // assign pixel to new position 33 | for (int c = 0; c < channel; c++){ 34 | out.at(y, x)[c] = img.at(y_before, x_before)[c]; 35 | } 36 | } 37 | } 38 | 39 | return out; 40 | } 41 | 42 | 43 | int main(int argc, const char* argv[]){ 44 | // read image 45 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 46 | 47 | // nearest neighbor 48 | cv::Mat out = nearest_neighbor(img, 1.5, 1.5); 49 | 50 | //cv::imwrite("out.jpg", out); 51 | cv::imshow("answer", out); 52 | cv::waitKey(0); 53 | cv::destroyAllWindows(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // bilinear 8 | cv::Mat bilinear(cv::Mat img, double rx, double ry){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | 15 | // get resized shape 16 | int resized_width = (int)(width * rx); 17 | int resized_height = (int)(height * ry); 18 | int x_before, y_before; 19 | double dx, dy; 20 | double val; 21 | 22 | // output image 23 | cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); 24 | 25 | // bi-linear interpolation 26 | for (int y = 0; y < resized_height; y++){ 27 | y_before = (int)floor(y / ry); 28 | y_before = fmin(y_before, height - 1); 29 | dy = y / ry - y_before; 30 | 31 | for (int x = 0; x < resized_width; x++){ 32 | x_before = (int)floor(x / rx); 33 | x_before = fmin(x_before, width - 1); 34 | dx = x / rx - x_before; 35 | 36 | // compute bi-linear 37 | for (int c = 0; c < channel; c++){ 38 | val = (1. - dx) * (1. - dy) * img.at(y_before, x_before)[c] + 39 | dx * (1. - dy) * img.at(y_before, x_before + 1)[c] + 40 | (1. - dx) * dy * img.at(y_before + 1, x_before)[c] + 41 | dx * dy * img.at(y_before + 1, x_before)[c]; 42 | 43 | // assign pixel to new position 44 | out.at(y, x)[c] = (uchar)val; 45 | } 46 | } 47 | } 48 | 49 | return out; 50 | } 51 | 52 | 53 | int main(int argc, const char* argv[]){ 54 | // read image 55 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 56 | 57 | // bilinear 58 | cv::Mat out = bilinear(img, 1.5, 1.5); 59 | 60 | //cv::imwrite("out.jpg", out); 61 | cv::imshow("answer", out); 62 | cv::waitKey(0); 63 | cv::destroyAllWindows(); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // affine 8 | cv::Mat affine(cv::Mat img, double a, double b, double c, double d, double tx, double ty){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | // get detriment 15 | double det = a * d - b * c; 16 | 17 | // Resize width and height 18 | int resized_width = (int)(width * a); 19 | int resized_height = (int)(height * d); 20 | 21 | // other parameters 22 | int x_before, y_before; 23 | double dx, dy, wx, wy, w_sum; 24 | double val; 25 | int _x, _y; 26 | 27 | // output 28 | cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); 29 | 30 | // Affine transformation 31 | for (int y = 0; y < resized_height; y++){ 32 | for (int x = 0; x < resized_width; x++){ 33 | 34 | // get original position x 35 | x_before = (int)((d * x - b * y) / det - tx); 36 | 37 | if ((x_before < 0) || (x_before >= width)){ 38 | continue; 39 | } 40 | 41 | // get original position y 42 | y_before = (int)((-c * x + a * y) / det - ty); 43 | 44 | if ((y_before < 0) || (y_before >= height)){ 45 | continue; 46 | } 47 | 48 | // assign pixel to new position 49 | for (int c = 0; c < channel; c++){ 50 | out.at(y, x)[c] = img.at(y_before, x_before)[c]; 51 | } 52 | } 53 | } 54 | 55 | return out; 56 | } 57 | 58 | 59 | int main(int argc, const char* argv[]){ 60 | // read image 61 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 62 | 63 | // affine 64 | cv::Mat out = affine(img, 1, 0, 0, 1, 30, -30); 65 | 66 | //cv::imwrite("out.jpg", out); 67 | cv::imshow("answer", out); 68 | cv::waitKey(0); 69 | cv::destroyAllWindows(); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Question_21_30/answers_cpp/answer_29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | // affine 8 | cv::Mat affine(cv::Mat img, double a, double b, double c, double d, double tx, double ty){ 9 | // get height and width 10 | int width = img.cols; 11 | int height = img.rows; 12 | int channel = img.channels(); 13 | 14 | // get detriment 15 | double det = a * d - b * c; 16 | 17 | // Resize width and height 18 | int resized_width = (int)(width * a); 19 | int resized_height = (int)(height * d); 20 | 21 | // other parameters 22 | int x_before, y_before; 23 | double dx, dy, wx, wy, w_sum; 24 | double val; 25 | int _x, _y; 26 | 27 | // output 28 | cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3); 29 | 30 | // Affine transformation 31 | for (int y = 0; y < resized_height; y++){ 32 | for (int x = 0; x < resized_width; x++){ 33 | 34 | // get original position x 35 | x_before = (int)((d * x - b * y) / det - tx); 36 | 37 | if ((x_before < 0) || (x_before >= width)){ 38 | continue; 39 | } 40 | 41 | // get original position y 42 | y_before = (int)((-c * x + a * y) / det - ty); 43 | 44 | if ((y_before < 0) || (y_before >= height)){ 45 | continue; 46 | } 47 | 48 | // assign pixel to new position 49 | for (int c = 0; c < channel; c++){ 50 | out.at(y, x)[c] = img.at(y_before, x_before)[c]; 51 | } 52 | } 53 | } 54 | 55 | return out; 56 | } 57 | 58 | 59 | int main(int argc, const char* argv[]){ 60 | // read image 61 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 62 | 63 | // affine 64 | cv::Mat out = affine(img, 1.3, 0, 0, 0.8, 30, -30); 65 | 66 | //cv::imwrite("out.jpg", out); 67 | cv::imshow("answer", out); 68 | cv::waitKey(0); 69 | cv::destroyAllWindows(); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_21_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_21_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_21_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_21_2.png -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_22_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_22_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_22_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_22_2.png -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_23_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_23_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_23_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_23_2.png -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_24.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_24.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_25.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_26.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_27.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_27.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_28.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_28.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_29_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_29_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_29_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_29_2.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_30_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_30_1.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_image/answer_30_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/answers_image/answer_30_2.jpg -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_21.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # histogram normalization 6 | def hist_normalization(img, a=0, b=255): 7 | # get max and min 8 | c = img.min() 9 | d = img.max() 10 | 11 | out = img.copy() 12 | 13 | # normalization 14 | out = (b-a) / (d - c) * (out - c) + a 15 | out[out < a] = a 16 | out[out > b] = b 17 | out = out.astype(np.uint8) 18 | 19 | return out 20 | 21 | 22 | # Read image 23 | img = cv2.imread("imori_dark.jpg").astype(np.float) 24 | H, W, C = img.shape 25 | 26 | # histogram normalization 27 | out = hist_normalization(img) 28 | 29 | # Display histogram 30 | plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 31 | plt.savefig("out_his.png") 32 | plt.show() 33 | 34 | # Save result 35 | cv2.imshow("result", out) 36 | cv2.waitKey(0) 37 | cv2.imwrite("out.jpg", out) 38 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_22.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # histogram manipulation 7 | def hist_mani(img, m0=128, s0=52): 8 | m = np.mean(img) 9 | s = np.std(img) 10 | 11 | out = img.copy() 12 | 13 | # normalize 14 | out = s0 / s * (out - m) + m0 15 | out[out < 0] = 0 16 | out[out > 255] = 255 17 | out = out.astype(np.uint8) 18 | 19 | return out 20 | 21 | 22 | # Read image 23 | img = cv2.imread("imori_dark.jpg").astype(np.float) 24 | 25 | out = hist_mani(img) 26 | 27 | # Display histogram 28 | plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 29 | plt.savefig("out_his.png") 30 | plt.show() 31 | 32 | # Save result 33 | cv2.imshow("result", out) 34 | cv2.waitKey(0) 35 | cv2.imwrite("out.jpg", out) 36 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_23.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # histogram equalization 6 | def hist_equal(img, z_max=255): 7 | H, W, C = img.shape 8 | S = H * W * C * 1. 9 | 10 | out = img.copy() 11 | 12 | sum_h = 0. 13 | 14 | for i in range(1, 255): 15 | ind = np.where(img == i) 16 | sum_h += len(img[ind]) 17 | z_prime = z_max / S * sum_h 18 | out[ind] = z_prime 19 | 20 | out = out.astype(np.uint8) 21 | 22 | return out 23 | 24 | 25 | # Read image 26 | img = cv2.imread("imori.jpg").astype(np.float) 27 | 28 | # histogram normalization 29 | out = hist_equal(img) 30 | 31 | # Display histogram 32 | plt.hist(out.ravel(), bins=255, rwidth=0.8, range=(0, 255)) 33 | plt.savefig("out_his.png") 34 | plt.show() 35 | 36 | # Save result 37 | cv2.imshow("result", out) 38 | cv2.waitKey(0) 39 | cv2.imwrite("out.jpg", out) 40 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_24.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # gamma correction 7 | def gamma_correction(img, c=1, g=2.2): 8 | out = img.copy() 9 | out /= 255. 10 | out = (1/c * out) ** (1/g) 11 | 12 | out *= 255 13 | out = out.astype(np.uint8) 14 | 15 | return out 16 | 17 | 18 | # Read image 19 | img = cv2.imread("imori_gamma.jpg").astype(np.float) 20 | 21 | # Gammma correction 22 | out = gamma_correction(img) 23 | 24 | # Save result 25 | cv2.imshow("result", out) 26 | cv2.waitKey(0) 27 | cv2.imwrite("out.jpg", out) 28 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_25.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Nereset Neighbor interpolation 7 | def nn_interpolate(img, ax=1, ay=1): 8 | H, W, C = img.shape 9 | 10 | aH = int(ay * H) 11 | aW = int(ax * W) 12 | 13 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 14 | x = np.tile(np.arange(aW), (aH, 1)) 15 | y = np.round(y / ay).astype(np.int) 16 | x = np.round(x / ax).astype(np.int) 17 | 18 | out = img[y,x] 19 | 20 | out = out.astype(np.uint8) 21 | 22 | return out 23 | 24 | 25 | # Read image 26 | img = cv2.imread("imori.jpg").astype(np.float) 27 | 28 | # Nearest Neighbor 29 | out = nn_interpolate(img, ax=1.5, ay=1.5) 30 | 31 | # Save result 32 | cv2.imshow("result", out) 33 | cv2.waitKey(0) 34 | cv2.imwrite("out.jpg", out) 35 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_26.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Bi-Linear interpolation 7 | def bl_interpolate(img, ax=1., ay=1.): 8 | H, W, C = img.shape 9 | 10 | aH = int(ay * H) 11 | aW = int(ax * W) 12 | 13 | # get position of resized image 14 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 15 | x = np.tile(np.arange(aW), (aH, 1)) 16 | 17 | # get position of original position 18 | y = (y / ay) 19 | x = (x / ax) 20 | 21 | ix = np.floor(x).astype(np.int) 22 | iy = np.floor(y).astype(np.int) 23 | 24 | ix = np.minimum(ix, W-2) 25 | iy = np.minimum(iy, H-2) 26 | 27 | # get distance 28 | dx = x - ix 29 | dy = y - iy 30 | 31 | dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1) 32 | dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1) 33 | 34 | # interpolation 35 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 36 | 37 | out = np.clip(out, 0, 255) 38 | out = out.astype(np.uint8) 39 | 40 | return out 41 | 42 | 43 | # Read image 44 | img = cv2.imread("imori.jpg").astype(np.float) 45 | 46 | # Bilinear interpolation 47 | out = bl_interpolate(img, ax=1.5, ay=1.5) 48 | 49 | # Save result 50 | cv2.imshow("result", out) 51 | cv2.waitKey(0) 52 | cv2.imwrite("out.jpg", out) 53 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_27.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Bi-cubic interpolation 7 | def bc_interpolate(img, ax=1., ay=1.): 8 | H, W, C = img.shape 9 | 10 | aH = int(ay * H) 11 | aW = int(ax * W) 12 | 13 | # get positions of resized image 14 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 15 | x = np.tile(np.arange(aW), (aH, 1)) 16 | y = (y / ay) 17 | x = (x / ax) 18 | 19 | # get positions of original image 20 | ix = np.floor(x).astype(np.int) 21 | iy = np.floor(y).astype(np.int) 22 | 23 | ix = np.minimum(ix, W-1) 24 | iy = np.minimum(iy, H-1) 25 | 26 | # get distance of each position of original image 27 | dx2 = x - ix 28 | dy2 = y - iy 29 | dx1 = dx2 + 1 30 | dy1 = dy2 + 1 31 | dx3 = 1 - dx2 32 | dy3 = 1 - dy2 33 | dx4 = 1 + dx3 34 | dy4 = 1 + dy3 35 | 36 | dxs = [dx1, dx2, dx3, dx4] 37 | dys = [dy1, dy2, dy3, dy4] 38 | 39 | # bi-cubic weight 40 | def weight(t): 41 | a = -1. 42 | at = np.abs(t) 43 | w = np.zeros_like(t) 44 | ind = np.where(at <= 1) 45 | w[ind] = ((a+2) * np.power(at, 3) - (a+3) * np.power(at, 2) + 1)[ind] 46 | ind = np.where((at > 1) & (at <= 2)) 47 | w[ind] = (a*np.power(at, 3) - 5*a*np.power(at, 2) + 8*a*at - 4*a)[ind] 48 | return w 49 | 50 | w_sum = np.zeros((aH, aW, C), dtype=np.float32) 51 | out = np.zeros((aH, aW, C), dtype=np.float32) 52 | 53 | # interpolate 54 | for j in range(-1, 3): 55 | for i in range(-1, 3): 56 | ind_x = np.minimum(np.maximum(ix + i, 0), W-1) 57 | ind_y = np.minimum(np.maximum(iy + j, 0), H-1) 58 | 59 | wx = weight(dxs[i+1]) 60 | wy = weight(dys[j+1]) 61 | wx = np.repeat(np.expand_dims(wx, axis=-1), 3, axis=-1) 62 | wy = np.repeat(np.expand_dims(wy, axis=-1), 3, axis=-1) 63 | 64 | w_sum += wx * wy 65 | out += wx * wy * img[ind_y, ind_x] 66 | 67 | out /= w_sum 68 | out = np.clip(out, 0, 255) 69 | out = out.astype(np.uint8) 70 | 71 | return out 72 | 73 | 74 | # Read image 75 | img = cv2.imread("imori.jpg").astype(np.float32) 76 | 77 | # Bi-cubic interpolation 78 | out = bc_interpolate(img, ax=1.5, ay=1.5) 79 | 80 | # Save result 81 | cv2.imshow("result", out) 82 | cv2.waitKey(0) 83 | cv2.imwrite("out.jpg", out) 84 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_28.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Affine 7 | def affine(img, a, b, c, d, tx, ty): 8 | H, W, C = img.shape 9 | 10 | # temporary image 11 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 12 | img[1:H+1, 1:W+1] = _img 13 | 14 | # get new image shape 15 | H_new = np.round(H * d).astype(np.int) 16 | W_new = np.round(W * a).astype(np.int) 17 | out = np.zeros((H_new+1, W_new+1, C), dtype=np.float32) 18 | 19 | # get position of new image 20 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 21 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 22 | 23 | # get position of original image by affine 24 | adbc = a * d - b * c 25 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 26 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 27 | 28 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 29 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 30 | 31 | # assgin pixcel to new image 32 | out[y_new, x_new] = img[y, x] 33 | 34 | out = out[:H_new, :W_new] 35 | out = out.astype(np.uint8) 36 | 37 | return out 38 | 39 | 40 | # Read image 41 | img = cv2.imread("imori.jpg").astype(np.float32) 42 | 43 | # Affine 44 | out = affine(img, a=1, b=0, c=0, d=1, tx=30, ty=-30) 45 | 46 | # Save result 47 | cv2.imshow("result", out) 48 | cv2.waitKey(0) 49 | cv2.imwrite("out.jpg", out) 50 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_29.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Affine 7 | def affine(img, a, b, c, d, tx, ty): 8 | H, W, C = img.shape 9 | 10 | # temporary image 11 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 12 | img[1:H+1, 1:W+1] = _img 13 | 14 | # get new image shape 15 | H_new = np.round(H * d).astype(np.int) 16 | W_new = np.round(W * a).astype(np.int) 17 | out = np.zeros((H_new+1, W_new+1, C), dtype=np.float32) 18 | 19 | # get position of new image 20 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 21 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 22 | 23 | # get position of original image by affine 24 | adbc = a * d - b * c 25 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 26 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 27 | 28 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 29 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 30 | 31 | # assgin pixcel to new image 32 | out[y_new, x_new] = img[y, x] 33 | 34 | out = out[:H_new, :W_new] 35 | out = out.astype(np.uint8) 36 | 37 | return out 38 | 39 | 40 | # Read image 41 | _img = cv2.imread("imori.jpg").astype(np.float32) 42 | 43 | # Affine 44 | out = affine(img, a=1.3, b=0, c=0, d=0.8, tx=30, ty=-30) 45 | 46 | 47 | # Save result 48 | cv2.imshow("result", out) 49 | cv2.waitKey(0) 50 | cv2.imwrite("out.jpg", out) 51 | -------------------------------------------------------------------------------- /Question_21_30/answers_py/answer_30.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # affine 7 | def affine(img, a, b, c, d, tx, ty): 8 | H, W, C = _img.shape 9 | 10 | # temporary image 11 | img = np.zeros((H+2, W+2, C), dtype=np.float32) 12 | img[1:H+1, 1:W+1] = _img 13 | 14 | # get shape of new image 15 | H_new = np.round(H).astype(np.int) 16 | W_new = np.round(W).astype(np.int) 17 | out = np.zeros((H_new, W_new, C), dtype=np.float32) 18 | 19 | # get position of new image 20 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 21 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 22 | 23 | # get position of original image by affine 24 | adbc = a * d - b * c 25 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 26 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 27 | 28 | # adjust center by affine 29 | dcx = (x.max() + x.min()) // 2 - W // 2 30 | dcy = (y.max() + y.min()) // 2 - H // 2 31 | 32 | x -= dcx 33 | y -= dcy 34 | 35 | x = np.clip(x, 0, W + 1) 36 | y = np.clip(y, 0, H + 1) 37 | 38 | # assign pixcel 39 | out[y_new, x_new] = img[y, x] 40 | out = out.astype(np.uint8) 41 | 42 | return out 43 | 44 | # Read image 45 | _img = cv2.imread("imori.jpg").astype(np.float32) 46 | 47 | 48 | # Affine 49 | A = 30. 50 | theta = - np.pi * A / 180. 51 | 52 | out = affine(img, a=np.cos(theta), b=-np.sin(theta), c=np.sin(theta), d=np.cos(theta), 53 | tx=0, ty=0) 54 | 55 | 56 | # Save result 57 | cv2.imshow("result", out) 58 | cv2.waitKey(0) 59 | cv2.imwrite("out.jpg", out) 60 | -------------------------------------------------------------------------------- /Question_21_30/assets/bci_fig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/assets/bci_fig.png -------------------------------------------------------------------------------- /Question_21_30/assets/bli_fig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/assets/bli_fig.png -------------------------------------------------------------------------------- /Question_21_30/assets/nni_fig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/assets/nni_fig.png -------------------------------------------------------------------------------- /Question_21_30/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/imori.jpg -------------------------------------------------------------------------------- /Question_21_30/imori_dark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/imori_dark.jpg -------------------------------------------------------------------------------- /Question_21_30/imori_gamma.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/imori_gamma.jpg -------------------------------------------------------------------------------- /Question_21_30/out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/out.jpg -------------------------------------------------------------------------------- /Question_21_30/question_24_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/question_24_1.jpg -------------------------------------------------------------------------------- /Question_21_30/question_24_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_21_30/question_24_2.jpg -------------------------------------------------------------------------------- /Question_31_40/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/README.pdf -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_31_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_31_1.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_31_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_31_2.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_31_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_31_3.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_32.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_32.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_32_ps.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_32_ps.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_33.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_33.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_34.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_34.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_35.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_35.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_36.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_36.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_37.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_37.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_38.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_38.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_39.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_39.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_image/answer_40.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/answers_image/answer_40.jpg -------------------------------------------------------------------------------- /Question_31_40/answers_py/answer_31.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Affine 6 | def affine(img, dx=30, dy=30): 7 | # get shape 8 | H, W, C = img.shape 9 | 10 | # Affine hyper parameters 11 | a = 1. 12 | b = dx / H 13 | c = dy / W 14 | d = 1. 15 | tx = 0. 16 | ty = 0. 17 | 18 | # prepare temporary 19 | _img = np.zeros((H+2, W+2, C), dtype=np.float32) 20 | 21 | # insert image to center of temporary 22 | _img[1:H+1, 1:W+1] = img 23 | 24 | # prepare affine image temporary 25 | H_new = np.ceil(dy + H).astype(np.int) 26 | W_new = np.ceil(dx + W).astype(np.int) 27 | out = np.zeros((H_new, W_new, C), dtype=np.float32) 28 | 29 | # preprare assigned index 30 | x_new = np.tile(np.arange(W_new), (H_new, 1)) 31 | y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1) 32 | 33 | # prepare inverse matrix for affine 34 | adbc = a * d - b * c 35 | x = np.round((d * x_new - b * y_new) / adbc).astype(np.int) - tx + 1 36 | y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1 37 | 38 | x = np.minimum(np.maximum(x, 0), W+1).astype(np.int) 39 | y = np.minimum(np.maximum(y, 0), H+1).astype(np.int) 40 | 41 | # assign value from original to affine image 42 | out[y_new, x_new] = _img[y, x] 43 | out = out.astype(np.uint8) 44 | 45 | return out 46 | 47 | 48 | # Read image 49 | img = cv2.imread("imori.jpg").astype(np.float32) 50 | 51 | # Affine 52 | out = affine(img, dx=30, dy=30) 53 | 54 | # Save result 55 | cv2.imshow("result", out) 56 | cv2.waitKey(0) 57 | cv2.imwrite("out.jpg", out) 58 | -------------------------------------------------------------------------------- /Question_31_40/answers_py/answer_32.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # DFT hyper-parameters 7 | K, L = 128, 128 8 | channel = 3 9 | 10 | 11 | # DFT 12 | def dft(img): 13 | H, W, _ = img.shape 14 | 15 | # Prepare DFT coefficient 16 | G = np.zeros((L, K, channel), dtype=np.complex) 17 | 18 | # prepare processed index corresponding to original image positions 19 | x = np.tile(np.arange(W), (H, 1)) 20 | y = np.arange(H).repeat(W).reshape(H, -1) 21 | 22 | # dft 23 | for c in range(channel): 24 | for l in range(L): 25 | for k in range(K): 26 | G[l, k, c] = np.sum(img[..., c] * np.exp(-2j * np.pi * (x * k / K + y * l / L))) / np.sqrt(K * L) 27 | #for n in range(N): 28 | # for m in range(M): 29 | # v += gray[n, m] * np.exp(-2j * np.pi * (m * k / M + n * l / N)) 30 | #G[l, k] = v / np.sqrt(M * N) 31 | 32 | return G 33 | 34 | # IDFT 35 | def idft(G): 36 | # prepare out image 37 | H, W, _ = G.shape 38 | out = np.zeros((H, W, channel), dtype=np.float32) 39 | 40 | # prepare processed index corresponding to original image positions 41 | x = np.tile(np.arange(W), (H, 1)) 42 | y = np.arange(H).repeat(W).reshape(H, -1) 43 | 44 | # idft 45 | for c in range(channel): 46 | for l in range(H): 47 | for k in range(W): 48 | out[l, k, c] = np.abs(np.sum(G[..., c] * np.exp(2j * np.pi * (x * k / W + y * l / H)))) / np.sqrt(W * H) 49 | 50 | # clipping 51 | out = np.clip(out, 0, 255) 52 | out = out.astype(np.uint8) 53 | 54 | return out 55 | 56 | 57 | 58 | # Read image 59 | img = cv2.imread("imori.jpg").astype(np.float32) 60 | 61 | # DFT 62 | G = dft(img) 63 | 64 | # write poser spectal to image 65 | ps = (np.abs(G) / np.abs(G).max() * 255).astype(np.uint8) 66 | cv2.imwrite("out_ps.jpg", ps) 67 | 68 | # IDFT 69 | out = idft(G) 70 | 71 | # Save result 72 | cv2.imshow("result", out) 73 | cv2.waitKey(0) 74 | cv2.imwrite("out.jpg", out) 75 | 76 | 77 | 78 | """ 79 | fimg = np.fft.fft2(gray) 80 | 81 | # 第1象限と第3象限, 第2象限と第4象限を入れ替え 82 | fimg = np.fft.fftshift(fimg) 83 | print(fimg.shape) 84 | # パワースペクトルの計算 85 | mag = 20*np.log(np.abs(fimg)) 86 | 87 | # 入力画像とスペクトル画像をグラフ描画 88 | plt.subplot(121) 89 | plt.imshow(gray, cmap = 'gray') 90 | plt.subplot(122) 91 | plt.imshow(mag, cmap = 'gray') 92 | plt.show() 93 | """ -------------------------------------------------------------------------------- /Question_31_40/answers_py/answer_36.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # DCT hyoer-parameter 6 | T = 8 7 | K = 8 8 | channel = 3 9 | 10 | # DCT weight 11 | def w(x, y, u, v): 12 | cu = 1. 13 | cv = 1. 14 | if u == 0: 15 | cu /= np.sqrt(2) 16 | if v == 0: 17 | cv /= np.sqrt(2) 18 | theta = np.pi / (2 * T) 19 | return (( 2 * cu * cv / T) * np.cos((2*x+1)*u*theta) * np.cos((2*y+1)*v*theta)) 20 | 21 | # DCT 22 | def dct(img): 23 | H, W, _ = img.shape 24 | 25 | F = np.zeros((H, W, channel), dtype=np.float32) 26 | 27 | for c in range(channel): 28 | for yi in range(0, H, T): 29 | for xi in range(0, W, T): 30 | for v in range(T): 31 | for u in range(T): 32 | for y in range(T): 33 | for x in range(T): 34 | F[v+yi, u+xi, c] += img[y+yi, x+xi, c] * w(x,y,u,v) 35 | 36 | return F 37 | 38 | 39 | # IDCT 40 | def idct(F): 41 | H, W, _ = F.shape 42 | 43 | out = np.zeros((H, W, channel), dtype=np.float32) 44 | 45 | for c in range(channel): 46 | for yi in range(0, H, T): 47 | for xi in range(0, W, T): 48 | for y in range(T): 49 | for x in range(T): 50 | for v in range(K): 51 | for u in range(K): 52 | out[y+yi, x+xi, c] += F[v+yi, u+xi, c] * w(x,y,u,v) 53 | 54 | out = np.clip(out, 0, 255) 55 | out = np.round(out).astype(np.uint8) 56 | 57 | return out 58 | 59 | 60 | 61 | # Read image 62 | img = cv2.imread("imori.jpg").astype(np.float32) 63 | 64 | # DCT 65 | F = dct(img) 66 | 67 | # IDCT 68 | out = idct(F) 69 | 70 | # Save result 71 | cv2.imshow("result", out) 72 | cv2.waitKey(0) 73 | cv2.imwrite("out.jpg", out) 74 | -------------------------------------------------------------------------------- /Question_31_40/answers_py/answer_37.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # DCT hyoer-parameter 6 | T = 8 7 | K = 4 8 | channel = 3 9 | 10 | # DCT weight 11 | def w(x, y, u, v): 12 | cu = 1. 13 | cv = 1. 14 | if u == 0: 15 | cu /= np.sqrt(2) 16 | if v == 0: 17 | cv /= np.sqrt(2) 18 | theta = np.pi / (2 * T) 19 | return (( 2 * cu * cv / T) * np.cos((2*x+1)*u*theta) * np.cos((2*y+1)*v*theta)) 20 | 21 | # DCT 22 | def dct(img): 23 | H, W, _ = img.shape 24 | 25 | F = np.zeros((H, W, channel), dtype=np.float32) 26 | 27 | for c in range(channel): 28 | for yi in range(0, H, T): 29 | for xi in range(0, W, T): 30 | for v in range(T): 31 | for u in range(T): 32 | for y in range(T): 33 | for x in range(T): 34 | F[v+yi, u+xi, c] += img[y+yi, x+xi, c] * w(x,y,u,v) 35 | 36 | return F 37 | 38 | 39 | # IDCT 40 | def idct(F): 41 | H, W, _ = F.shape 42 | 43 | out = np.zeros((H, W, channel), dtype=np.float32) 44 | 45 | for c in range(channel): 46 | for yi in range(0, H, T): 47 | for xi in range(0, W, T): 48 | for y in range(T): 49 | for x in range(T): 50 | for v in range(K): 51 | for u in range(K): 52 | out[y+yi, x+xi, c] += F[v+yi, u+xi, c] * w(x,y,u,v) 53 | 54 | out = np.clip(out, 0, 255) 55 | out = np.round(out).astype(np.uint8) 56 | 57 | return out 58 | 59 | 60 | # MSE 61 | def MSE(img1, img2): 62 | H, W, _ = img1.shape 63 | mse = np.sum((img1 - img2) ** 2) / (H * W * channel) 64 | return mse 65 | 66 | # PSNR 67 | def PSNR(mse, vmax=255): 68 | return 10 * np.log10(vmax * vmax / mse) 69 | 70 | # bitrate 71 | def BITRATE(): 72 | return 1. * T * K * K / T / T 73 | 74 | 75 | # Read image 76 | img = cv2.imread("imori.jpg").astype(np.float32) 77 | 78 | # DCT 79 | F = dct(img) 80 | 81 | # IDCT 82 | out = idct(F) 83 | 84 | # MSE 85 | mse = MSE(img, out) 86 | 87 | # PSNR 88 | psnr = PSNR(mse) 89 | 90 | # bitrate 91 | bitrate = BITRATE() 92 | 93 | print("MSE:", mse) 94 | print("PSNR:", psnr) 95 | print("bitrate:", bitrate) 96 | 97 | # Save result 98 | cv2.imshow("result", out) 99 | cv2.waitKey(0) 100 | cv2.imwrite("out.jpg", out) 101 | -------------------------------------------------------------------------------- /Question_31_40/answers_py/answer_39.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | channel = 3 6 | 7 | # BGR -> Y Cb Cr 8 | def BGR2YCbCr(img): 9 | H, W, _ = img.shape 10 | 11 | ycbcr = np.zeros([H, W, 3], dtype=np.float32) 12 | 13 | ycbcr[..., 0] = 0.2990 * img[..., 2] + 0.5870 * img[..., 1] + 0.1140 * img[..., 0] 14 | ycbcr[..., 1] = -0.1687 * img[..., 2] - 0.3313 * img[..., 1] + 0.5 * img[..., 0] + 128. 15 | ycbcr[..., 2] = 0.5 * img[..., 2] - 0.4187 * img[..., 1] - 0.0813 * img[..., 0] + 128. 16 | 17 | return ycbcr 18 | 19 | # Y Cb Cr -> BGR 20 | def YCbCr2BGR(ycbcr): 21 | H, W, _ = ycbcr.shape 22 | 23 | out = np.zeros([H, W, channel], dtype=np.float32) 24 | out[..., 2] = ycbcr[..., 0] + (ycbcr[..., 2] - 128.) * 1.4020 25 | out[..., 1] = ycbcr[..., 0] - (ycbcr[..., 1] - 128.) * 0.3441 - (ycbcr[..., 2] - 128.) * 0.7139 26 | out[..., 0] = ycbcr[..., 0] + (ycbcr[..., 1] - 128.) * 1.7718 27 | 28 | out = np.clip(out, 0, 255) 29 | out = out.astype(np.uint8) 30 | 31 | return out 32 | 33 | 34 | # Read image 35 | img = cv2.imread("imori.jpg").astype(np.float32) 36 | 37 | # bgr -> Y Cb Cr 38 | ycbcr = BGR2YCbCr(img) 39 | 40 | # process 41 | ycbcr[..., 0] *= 0.7 42 | 43 | # YCbCr > RGB 44 | out = YCbCr2BGR(ycbcr) 45 | 46 | # Save result 47 | cv2.imshow("result", out) 48 | cv2.waitKey(0) 49 | cv2.imwrite("out.jpg", out) 50 | -------------------------------------------------------------------------------- /Question_31_40/assets/lpf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/assets/lpf.png -------------------------------------------------------------------------------- /Question_31_40/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/imori.jpg -------------------------------------------------------------------------------- /Question_31_40/imori_gray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_31_40/imori_gray.jpg -------------------------------------------------------------------------------- /Question_41_50/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/README.pdf -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_4.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_41_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_41_1.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_41_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_41_2.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_42.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_42.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_42_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_42_1.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_42_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_42_2.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_43.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_43.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_44.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_44.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_45.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_45.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_46.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_46.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_47.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_47.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_48.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_48.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_49.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_49.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_image/answer_50.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/answers_image/answer_50.jpg -------------------------------------------------------------------------------- /Question_41_50/answers_py/answer_47.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Gray scale 7 | def BGR2GRAY(img): 8 | b = img[:, :, 0].copy() 9 | g = img[:, :, 1].copy() 10 | r = img[:, :, 2].copy() 11 | 12 | # Gray scale 13 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 14 | out = out.astype(np.uint8) 15 | 16 | return out 17 | 18 | # Otsu Binalization 19 | def otsu_binarization(img, th=128): 20 | H, W = img.shape 21 | out = img.copy() 22 | 23 | max_sigma = 0 24 | max_t = 0 25 | 26 | # determine threshold 27 | for _t in range(1, 255): 28 | v0 = out[np.where(out < _t)] 29 | m0 = np.mean(v0) if len(v0) > 0 else 0. 30 | w0 = len(v0) / (H * W) 31 | v1 = out[np.where(out >= _t)] 32 | m1 = np.mean(v1) if len(v1) > 0 else 0. 33 | w1 = len(v1) / (H * W) 34 | sigma = w0 * w1 * ((m0 - m1) ** 2) 35 | if sigma > max_sigma: 36 | max_sigma = sigma 37 | max_t = _t 38 | 39 | # Binarization 40 | print("threshold >>", max_t) 41 | th = max_t 42 | out[out < th] = 0 43 | out[out >= th] = 255 44 | 45 | return out 46 | 47 | 48 | # Morphology Erode 49 | def Morphology_Erode(img, Dil_time=1): 50 | H, W = img.shape 51 | 52 | # kernel 53 | MF = np.array(((0, 1, 0), 54 | (1, 0, 1), 55 | (0, 1, 0)), dtype=np.int) 56 | 57 | # each dilate time 58 | out = img.copy() 59 | for i in range(Dil_time): 60 | tmp = np.pad(out, (1, 1), 'edge') 61 | for y in range(1, H+1): 62 | for x in range(1, W+1): 63 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 64 | out[y-1, x-1] = 255 65 | 66 | return out 67 | 68 | # Read image 69 | img = cv2.imread("imori.jpg").astype(np.float32) 70 | 71 | 72 | # Grayscale 73 | gray = BGR2GRAY(img) 74 | 75 | # Otsu's binarization 76 | otsu = otsu_binarization(gray) 77 | 78 | # Morphology - dilate 79 | out = Morphology_Erode(otsu, Dil_time=2) 80 | 81 | # Save result 82 | cv2.imwrite("out.jpg", out) 83 | cv2.imshow("result", out) 84 | cv2.waitKey(0) 85 | cv2.destroyAllWindows() 86 | -------------------------------------------------------------------------------- /Question_41_50/answers_py/answer_48.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Gray scale 6 | def BGR2GRAY(img): 7 | b = img[:, :, 0].copy() 8 | g = img[:, :, 1].copy() 9 | r = img[:, :, 2].copy() 10 | 11 | # Gray scale 12 | out = 0.2126 * r + 0.7152 * g + 0.0722 * b 13 | out = out.astype(np.uint8) 14 | 15 | return out 16 | 17 | # Otsu Binalization 18 | def otsu_binarization(img, th=128): 19 | H, W = img.shape 20 | out = img.copy() 21 | 22 | max_sigma = 0 23 | max_t = 0 24 | 25 | # determine threshold 26 | for _t in range(1, 255): 27 | v0 = out[np.where(out < _t)] 28 | m0 = np.mean(v0) if len(v0) > 0 else 0. 29 | w0 = len(v0) / (H * W) 30 | v1 = out[np.where(out >= _t)] 31 | m1 = np.mean(v1) if len(v1) > 0 else 0. 32 | w1 = len(v1) / (H * W) 33 | sigma = w0 * w1 * ((m0 - m1) ** 2) 34 | if sigma > max_sigma: 35 | max_sigma = sigma 36 | max_t = _t 37 | 38 | # Binarization 39 | print("threshold >>", max_t) 40 | th = max_t 41 | out[out < th] = 0 42 | out[out >= th] = 255 43 | 44 | return out 45 | 46 | 47 | # Morphology Dilate 48 | def Morphology_Dilate(img, Erode_time=1): 49 | H, W = img.shape 50 | out = img.copy() 51 | 52 | # kernel 53 | MF = np.array(((0, 1, 0), 54 | (1, 0, 1), 55 | (0, 1, 0)), dtype=np.int) 56 | 57 | # each erode 58 | for i in range(Erode_time): 59 | tmp = np.pad(out, (1, 1), 'edge') 60 | # erode 61 | for y in range(1, H+1): 62 | for x in range(1, W+1): 63 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 64 | out[y-1, x-1] = 0 65 | 66 | return out 67 | 68 | 69 | # Read image 70 | img = cv2.imread("imori.jpg").astype(np.float32) 71 | 72 | 73 | # Grayscale 74 | gray = BGR2GRAY(img) 75 | 76 | # Otsu's binarization 77 | otsu = otsu_binarization(gray) 78 | 79 | # Morphology - dilate 80 | out = Morphology_Dilate(otsu, Erode_time=2) 81 | 82 | 83 | # Save result 84 | cv2.imwrite("out.jpg", out) 85 | cv2.imshow("result", out) 86 | cv2.waitKey(0) 87 | cv2.destroyAllWindows() 88 | -------------------------------------------------------------------------------- /Question_41_50/assets/canny_erode2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/assets/canny_erode2.jpg -------------------------------------------------------------------------------- /Question_41_50/assets/morphology_closing.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/assets/morphology_closing.png -------------------------------------------------------------------------------- /Question_41_50/assets/morphology_dilate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/assets/morphology_dilate.png -------------------------------------------------------------------------------- /Question_41_50/assets/morphology_erode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/assets/morphology_erode.png -------------------------------------------------------------------------------- /Question_41_50/assets/morphology_opening.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/assets/morphology_opening.png -------------------------------------------------------------------------------- /Question_41_50/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/imori.jpg -------------------------------------------------------------------------------- /Question_41_50/thorino.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_41_50/thorino.jpg -------------------------------------------------------------------------------- /Question_51_60/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/README.pdf -------------------------------------------------------------------------------- /Question_51_60/answers/answer_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_4.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_51.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_51.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_51.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)[0]] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)[0]] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - erode 42 | Erode_time = 1 43 | erode = out.copy() 44 | 45 | for i in range(Erode_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 50 | erode[y-1, x-1] = 0 51 | 52 | # Morphology - dilate 53 | Dil_time = 1 54 | dilate = out.copy() 55 | 56 | for i in range(Dil_time): 57 | tmp = np.pad(out, (1, 1), 'edge') 58 | for y in range(1, H+1): 59 | for x in range(1, W+1): 60 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 61 | dilate[y-1, x-1] = 255 62 | 63 | out = np.abs(erode - dilate) * 255 64 | 65 | # Save result 66 | cv2.imwrite("out.jpg", out) 67 | cv2.imshow("result", out) 68 | cv2.waitKey(0) 69 | cv2.destroyAllWindows() 70 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_52.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_52.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_52.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - erode 42 | Erode_time = 3 43 | mor = out.copy() 44 | 45 | for i in range(Erode_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 50 | mor[y-1, x-1] = 0 51 | 52 | # Morphology - dilate 53 | Dil_time = 3 54 | 55 | for i in range(Dil_time): 56 | tmp = np.pad(mor, (1, 1), 'edge') 57 | for y in range(1, H+1): 58 | for x in range(1, W+1): 59 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 60 | mor[y-1, x-1] = 255 61 | 62 | out = out - mor 63 | 64 | # Save result 65 | cv2.imwrite("out.jpg", out) 66 | cv2.imshow("result", out) 67 | cv2.waitKey(0) 68 | cv2.destroyAllWindows() 69 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_53.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_53.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_53.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Otsu binary 10 | ## Grayscale 11 | out = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 12 | out = out.astype(np.uint8) 13 | 14 | ## Determine threshold of Otsu's binarization 15 | max_sigma = 0 16 | max_t = 0 17 | 18 | for _t in range(1, 255): 19 | v0 = out[np.where(out < _t)] 20 | m0 = np.mean(v0) if len(v0) > 0 else 0. 21 | w0 = len(v0) / (H * W) 22 | v1 = out[np.where(out >= _t)] 23 | m1 = np.mean(v1) if len(v1) > 0 else 0. 24 | w1 = len(v1) / (H * W) 25 | sigma = w0 * w1 * ((m0 - m1) ** 2) 26 | if sigma > max_sigma: 27 | max_sigma = sigma 28 | max_t = _t 29 | 30 | ## Binarization 31 | #print("threshold >>", max_t) 32 | th = max_t 33 | out[out < th] = 0 34 | out[out >= th] = 255 35 | 36 | # Morphology filter 37 | MF = np.array(((0, 1, 0), 38 | (1, 0, 1), 39 | (0, 1, 0)), dtype=np.int) 40 | 41 | # Morphology - dilate 42 | Dil_time = 3 43 | mor = out.copy() 44 | 45 | for i in range(Dil_time): 46 | tmp = np.pad(out, (1, 1), 'edge') 47 | for y in range(1, H+1): 48 | for x in range(1, W+1): 49 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) >= 255: 50 | mor[y-1, x-1] = 255 51 | 52 | # Morphology - erode 53 | Erode_time = 3 54 | 55 | for i in range(Erode_time): 56 | tmp = np.pad(mor, (1, 1), 'edge') 57 | for y in range(1, H+1): 58 | for x in range(1, W+1): 59 | if np.sum(MF * tmp[y-1:y+2, x-1:x+2]) < 255*4: 60 | mor[y-1, x-1] = 0 61 | 62 | out = mor - out 63 | 64 | # Save result 65 | cv2.imwrite("out.jpg", out) 66 | cv2.imshow("result", out) 67 | cv2.waitKey(0) 68 | cv2.destroyAllWindows() 69 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_54.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_54.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_54.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Read templete image 10 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 11 | Ht, Wt, Ct = temp.shape 12 | 13 | 14 | # Templete matching 15 | i, j = -1, -1 16 | v = 255 * H * W * C 17 | for y in range(H-Ht): 18 | for x in range(W-Wt): 19 | _v = np.sum((img[y:y+Ht, x:x+Wt] - temp) ** 2) 20 | if _v < v: 21 | v = _v 22 | i, j = x, y 23 | 24 | out = img.copy() 25 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 26 | out = out.astype(np.uint8) 27 | 28 | # Save result 29 | cv2.imwrite("out.jpg", out) 30 | cv2.imshow("result", out) 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_55.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_55.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_55.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Read templete image 10 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 11 | Ht, Wt, Ct = temp.shape 12 | 13 | 14 | # Templete matching 15 | i, j = -1, -1 16 | v = 255 * H * W * C 17 | for y in range(H-Ht): 18 | for x in range(W-Wt): 19 | _v = np.sum(np.abs(img[y:y+Ht, x:x+Wt] - temp)) 20 | if _v < v: 21 | v = _v 22 | i, j = x, y 23 | 24 | out = img.copy() 25 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 26 | out = out.astype(np.uint8) 27 | 28 | # Save result 29 | cv2.imwrite("out.jpg", out) 30 | cv2.imshow("result", out) 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_56.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_56.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_56.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Read templete image 10 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 11 | Ht, Wt, Ct = temp.shape 12 | 13 | 14 | # Templete matching 15 | i, j = -1, -1 16 | v = -1 17 | for y in range(H-Ht): 18 | for x in range(W-Wt): 19 | _v = np.sum(img[y:y+Ht, x:x+Wt] * temp) 20 | _v /= (np.sqrt(np.sum(img[y:y+Ht, x:x+Wt]**2)) * np.sqrt(np.sum(temp**2))) 21 | if _v > v: 22 | v = _v 23 | i, j = x, y 24 | 25 | out = img.copy() 26 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 27 | out = out.astype(np.uint8) 28 | 29 | # Save result 30 | cv2.imwrite("out.jpg", out) 31 | cv2.imshow("result", out) 32 | cv2.waitKey(0) 33 | cv2.destroyAllWindows() 34 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_57.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_57.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_57.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | mi = np.mean(img) 10 | 11 | # Read templete image 12 | temp = cv2.imread("imori_part.jpg").astype(np.float32) 13 | Ht, Wt, Ct = temp.shape 14 | 15 | mt = np.mean(temp) 16 | 17 | # Templete matching 18 | i, j = -1, -1 19 | v = -1 20 | for y in range(H-Ht): 21 | for x in range(W-Wt): 22 | _v = np.sum((img[y:y+Ht, x:x+Wt]-mi) * (temp-mt)) 23 | _v /= (np.sqrt(np.sum((img[y:y+Ht, x:x+Wt]-mi)**2)) * np.sqrt(np.sum((temp-mt)**2))) 24 | if _v > v: 25 | v = _v 26 | i, j = x, y 27 | 28 | out = img.copy() 29 | cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1) 30 | out = out.astype(np.uint8) 31 | 32 | # Save result 33 | cv2.imwrite("out.jpg", out) 34 | cv2.imshow("result", out) 35 | cv2.waitKey(0) 36 | cv2.destroyAllWindows() 37 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_58.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_58.png -------------------------------------------------------------------------------- /Question_51_60/answers/answer_58.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("seg.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | label = np.zeros((H, W), dtype=np.int) 10 | label[img[..., 0]>0] = 1 11 | 12 | LUT = [0 for _ in range(H*W)] 13 | 14 | n = 1 15 | 16 | for y in range(H): 17 | for x in range(W): 18 | if label[y, x] == 0: 19 | continue 20 | c3 = label[max(y-1,0), x] 21 | c5 = label[y, max(x-1,0)] 22 | if c3 < 2 and c5 < 2: 23 | n += 1 24 | label[y, x] = n 25 | else: 26 | _vs = [c3, c5] 27 | vs = [a for a in _vs if a > 1] 28 | v = min(vs) 29 | label[y, x] = v 30 | 31 | minv = v 32 | for _v in vs: 33 | if LUT[_v] != 0: 34 | minv = min(minv, LUT[_v]) 35 | for _v in vs: 36 | LUT[_v] = minv 37 | 38 | count = 1 39 | 40 | for l in range(2, n+1): 41 | flag = True 42 | for i in range(n+1): 43 | if LUT[i] == l: 44 | if flag: 45 | count += 1 46 | flag = False 47 | LUT[i] = count 48 | 49 | COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]] 50 | out = np.zeros((H, W, C), dtype=np.uint8) 51 | 52 | for i, lut in enumerate(LUT[2:]): 53 | out[label == (i+2)] = COLORS[lut-2] 54 | 55 | # Save result 56 | cv2.imwrite("out.png", out) 57 | cv2.imshow("result", out) 58 | cv2.waitKey(0) 59 | cv2.destroyAllWindows() 60 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_59.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_59.png -------------------------------------------------------------------------------- /Question_51_60/answers/answer_59.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("seg.png").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | label = np.zeros((H, W), dtype=np.int) 10 | label[img[..., 0]>0] = 1 11 | 12 | LUT = [0 for _ in range(H*W)] 13 | 14 | n = 1 15 | 16 | for y in range(H): 17 | for x in range(W): 18 | if label[y, x] == 0: 19 | continue 20 | c2 = label[max(y-1,0), min(x+1, W-1)] 21 | c3 = label[max(y-1,0), x] 22 | c4 = label[max(y-1,0), max(x-1,0)] 23 | c5 = label[y, max(x-1,0)] 24 | if c3 < 2 and c5 < 2 and c2 < 2 and c4 < 2: 25 | n += 1 26 | label[y, x] = n 27 | else: 28 | _vs = [c3, c5, c2, c4] 29 | vs = [a for a in _vs if a > 1] 30 | v = min(vs) 31 | label[y, x] = v 32 | 33 | minv = v 34 | for _v in vs: 35 | if LUT[_v] != 0: 36 | minv = min(minv, LUT[_v]) 37 | for _v in vs: 38 | LUT[_v] = minv 39 | 40 | count = 1 41 | 42 | for l in range(2, n+1): 43 | flag = True 44 | for i in range(n+1): 45 | if LUT[i] == l: 46 | if flag: 47 | count += 1 48 | flag = False 49 | LUT[i] = count 50 | 51 | COLORS = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0]] 52 | out = np.zeros((H, W, C), dtype=np.uint8) 53 | 54 | for i, lut in enumerate(LUT[2:]): 55 | out[label == (i+2)] = COLORS[lut-2] 56 | 57 | # Save result 58 | cv2.imwrite("out.png", out) 59 | cv2.imshow("result", out) 60 | cv2.waitKey(0) 61 | cv2.destroyAllWindows() 62 | -------------------------------------------------------------------------------- /Question_51_60/answers/answer_60.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/answers/answer_60.jpg -------------------------------------------------------------------------------- /Question_51_60/answers/answer_60.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Read image 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | img2 = cv2.imread("thorino.jpg").astype(np.float32) 10 | 11 | a = 0.6 12 | out = img * a + img2 * (1 - a) 13 | out = out.astype(np.uint8) 14 | 15 | # Save result 16 | cv2.imwrite("out.jpg", out) 17 | cv2.imshow("result", out) 18 | cv2.waitKey(0) 19 | cv2.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /Question_51_60/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/imori.jpg -------------------------------------------------------------------------------- /Question_51_60/imori_part.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/imori_part.jpg -------------------------------------------------------------------------------- /Question_51_60/seg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/seg.png -------------------------------------------------------------------------------- /Question_51_60/thorino.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_51_60/thorino.jpg -------------------------------------------------------------------------------- /Question_61_70/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/README.pdf -------------------------------------------------------------------------------- /Question_61_70/answers/answer_61.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_61.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_61.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Connect 4 6 | def connect_4(img): 7 | # get shape 8 | H, W, C = img.shape 9 | 10 | # prepare temporary image 11 | tmp = np.zeros((H, W), dtype=np.int) 12 | 13 | # binarize 14 | tmp[img[..., 0] > 0] = 1 15 | 16 | # prepare out image 17 | out = np.zeros((H, W, 3), dtype=np.uint8) 18 | 19 | # each pixel 20 | for y in range(H): 21 | for x in range(W): 22 | if tmp[y, x] < 1: 23 | continue 24 | 25 | S = 0 26 | S += (tmp[y,min(x+1,W-1)] - tmp[y,min(x+1,W-1)] * tmp[max(y-1,0),min(x+1,W-1)] * tmp[max(y-1,0),x]) 27 | S += (tmp[max(y-1,0),x] - tmp[max(y-1,0),x] * tmp[max(y-1,0),max(x-1,0)] * tmp[y,max(x-1,0)]) 28 | S += (tmp[y,max(x-1,0)] - tmp[y,max(x-1,0)] * tmp[min(y+1,H-1),max(x-1,0)] * tmp[min(y+1,H-1),x]) 29 | S += (tmp[min(y+1,H-1),x] - tmp[min(y+1,H-1),x] * tmp[min(y+1,H-1),min(x+1,W-1)] * tmp[y,min(x+1,W-1)]) 30 | 31 | if S == 0: 32 | out[y,x] = [0, 0, 255] 33 | elif S == 1: 34 | out[y,x] = [0, 255, 0] 35 | elif S == 2: 36 | out[y,x] = [255, 0, 0] 37 | elif S == 3: 38 | out[y,x] = [255, 255, 0] 39 | elif S == 4: 40 | out[y,x] = [255, 0, 255] 41 | 42 | out = out.astype(np.uint8) 43 | 44 | return out 45 | 46 | 47 | 48 | # Read image 49 | img = cv2.imread("renketsu.png").astype(np.float32) 50 | 51 | # connect 4 52 | out = connect_4(img) 53 | 54 | # Save result 55 | cv2.imwrite("out.png", out) 56 | cv2.imshow("result", out) 57 | cv2.waitKey(0) 58 | cv2.destroyAllWindows() 59 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_62.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_62.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_62.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # connect 8 6 | def connect_8(img): 7 | # get shape 8 | H, W, C = img.shape 9 | 10 | # prepare temporary 11 | _tmp = np.zeros((H, W), dtype=np.int) 12 | 13 | # get binarize 14 | _tmp[img[..., 0] > 0] = 1 15 | 16 | # inverse for connect 8 17 | tmp = 1 - _tmp 18 | 19 | # prepare image 20 | out = np.zeros((H, W, 3), dtype=np.uint8) 21 | 22 | # each pixel 23 | for y in range(H): 24 | for x in range(W): 25 | if _tmp[y, x] < 1: 26 | continue 27 | 28 | S = 0 29 | S += (tmp[y,min(x+1,W-1)] - tmp[y,min(x+1,W-1)] * tmp[max(y-1,0),min(x+1,W-1)] * tmp[max(y-1,0),x]) 30 | S += (tmp[max(y-1,0),x] - tmp[max(y-1,0),x] * tmp[max(y-1,0),max(x-1,0)] * tmp[y,max(x-1,0)]) 31 | S += (tmp[y,max(x-1,0)] - tmp[y,max(x-1,0)] * tmp[min(y+1,H-1),max(x-1,0)] * tmp[min(y+1,H-1),x]) 32 | S += (tmp[min(y+1,H-1),x] - tmp[min(y+1,H-1),x] * tmp[min(y+1,H-1),min(x+1,W-1)] * tmp[y,min(x+1,W-1)]) 33 | 34 | if S == 0: 35 | out[y,x] = [0, 0, 255] 36 | elif S == 1: 37 | out[y,x] = [0, 255, 0] 38 | elif S == 2: 39 | out[y,x] = [255, 0, 0] 40 | elif S == 3: 41 | out[y,x] = [255, 255, 0] 42 | elif S == 4: 43 | out[y,x] = [255, 0, 255] 44 | 45 | out = out.astype(np.uint8) 46 | 47 | return out 48 | 49 | 50 | # Read image 51 | img = cv2.imread("renketsu.png").astype(np.float32) 52 | 53 | # connect 8 54 | out = connect_8(img) 55 | 56 | 57 | # Save result 58 | cv2.imwrite("out.png", out) 59 | cv2.imshow("result", out) 60 | cv2.waitKey(0) 61 | cv2.destroyAllWindows() 62 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_63.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_63.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_63.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # thining algorythm 7 | def thining(img): 8 | # get shape 9 | H, W, C = img.shape 10 | 11 | # prepare out image 12 | out = np.zeros((H, W), dtype=np.int) 13 | out[img[..., 0] > 0] = 1 14 | 15 | count = 1 16 | while count > 0: 17 | count = 0 18 | tmp = out.copy() 19 | # each pixel ( rasta scan ) 20 | for y in range(H): 21 | for x in range(W): 22 | # skip black pixel 23 | if out[y, x] < 1: 24 | continue 25 | 26 | # count satisfied conditions 27 | judge = 0 28 | 29 | ## condition 1 30 | if (tmp[y, min(x+1, W-1)] + tmp[max(y-1, 0), x] + tmp[y, max(x-1, 0)] + tmp[min(y+1, H-1), x]) < 4: 31 | judge += 1 32 | 33 | ## condition 2 34 | c = 0 35 | c += (tmp[y,min(x+1, W-1)] - tmp[y, min(x+1, W-1)] * tmp[max(y-1, 0),min(x+1, W-1)] * tmp[max(y-1, 0), x]) 36 | c += (tmp[max(y-1,0), x] - tmp[max(y-1,0), x] * tmp[max(y-1, 0), max(x-1, 0)] * tmp[y, max(x-1, 0)]) 37 | c += (tmp[y, max(x-1, 0)] - tmp[y,max(x-1, 0)] * tmp[min(y+1, H-1), max(x-1, 0)] * tmp[min(y+1, H-1), x]) 38 | c += (tmp[min(y+1, H-1), x] - tmp[min(y+1, H-1), x] * tmp[min(y+1, H-1), min(x+1, W-1)] * tmp[y, min(x+1, W-1)]) 39 | if c == 1: 40 | judge += 1 41 | 42 | ##x condition 3 43 | if np.sum(tmp[max(y-1, 0) : min(y+2, H), max(x-1, 0) : min(x+2, W)]) >= 4: 44 | judge += 1 45 | 46 | # if all conditions are satisfied 47 | if judge == 3: 48 | out[y, x] = 0 49 | count += 1 50 | 51 | out = out.astype(np.uint8) * 255 52 | 53 | return out 54 | 55 | 56 | # Read image 57 | img = cv2.imread("gazo.png").astype(np.float32) 58 | 59 | # thining 60 | out = thining(img) 61 | 62 | # Save result 63 | cv2.imwrite("out.png", out) 64 | cv2.imshow("result", out) 65 | cv2.waitKey(0) 66 | cv2.destroyAllWindows() 67 | -------------------------------------------------------------------------------- /Question_61_70/answers/answer_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_64.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_65.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_65.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_66_gra.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_66_gra.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_66_mag.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_66_mag.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_67.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_67.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_68.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_68.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_69.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_69.jpg -------------------------------------------------------------------------------- /Question_61_70/answers/answer_70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/answers/answer_70.png -------------------------------------------------------------------------------- /Question_61_70/answers/answer_70.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # BGR -> HSV 6 | def BGR2HSV(_img): 7 | img = _img.copy() / 255. 8 | 9 | hsv = np.zeros_like(img, dtype=np.float32) 10 | 11 | # get max and min 12 | max_v = np.max(img, axis=2).copy() 13 | min_v = np.min(img, axis=2).copy() 14 | min_arg = np.argmin(img, axis=2) 15 | 16 | # H 17 | hsv[..., 0][np.where(max_v == min_v)]= 0 18 | ## if min == B 19 | ind = np.where(min_arg == 0) 20 | hsv[..., 0][ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 21 | ## if min == R 22 | ind = np.where(min_arg == 2) 23 | hsv[..., 0][ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 24 | ## if min == G 25 | ind = np.where(min_arg == 1) 26 | hsv[..., 0][ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 27 | 28 | # S 29 | hsv[..., 1] = max_v.copy() - min_v.copy() 30 | 31 | # V 32 | hsv[..., 2] = max_v.copy() 33 | 34 | return hsv 35 | 36 | # make mask 37 | def get_mask(hsv): 38 | mask = np.zeros_like(hsv[..., 0]) 39 | #mask[np.where((hsv > 180) & (hsv[0] < 260))] = 255 40 | mask[np.logical_and((hsv[..., 0] > 180), (hsv[..., 0] < 260))] = 255 41 | return mask 42 | 43 | 44 | # Read image 45 | img = cv2.imread("imori.jpg").astype(np.float32) 46 | 47 | # RGB > HSV 48 | hsv = BGR2HSV(img) 49 | 50 | 51 | # color tracking 52 | mask = get_mask(hsv) 53 | 54 | out = mask.astype(np.uint8) 55 | 56 | # Save result 57 | cv2.imwrite("out.png", out) 58 | cv2.imshow("result", out) 59 | cv2.waitKey(0) 60 | cv2.destroyAllWindows() 61 | -------------------------------------------------------------------------------- /Question_61_70/gazo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/gazo.png -------------------------------------------------------------------------------- /Question_61_70/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/imori.jpg -------------------------------------------------------------------------------- /Question_61_70/renketsu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_61_70/renketsu.png -------------------------------------------------------------------------------- /Question_71_80/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/README.pdf -------------------------------------------------------------------------------- /Question_71_80/answers/answer_70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_70.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_71.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_71.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_71.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # BGR -> HSV 6 | def BGR2HSV(_img): 7 | img = _img.copy() / 255. 8 | 9 | hsv = np.zeros_like(img, dtype=np.float32) 10 | 11 | # get max and min 12 | max_v = np.max(img, axis=2).copy() 13 | min_v = np.min(img, axis=2).copy() 14 | min_arg = np.argmin(img, axis=2) 15 | 16 | # H 17 | hsv[..., 0][np.where(max_v == min_v)]= 0 18 | ## if min == B 19 | ind = np.where(min_arg == 0) 20 | hsv[..., 0][ind] = 60 * (img[..., 1][ind] - img[..., 2][ind]) / (max_v[ind] - min_v[ind]) + 60 21 | ## if min == R 22 | ind = np.where(min_arg == 2) 23 | hsv[..., 0][ind] = 60 * (img[..., 0][ind] - img[..., 1][ind]) / (max_v[ind] - min_v[ind]) + 180 24 | ## if min == G 25 | ind = np.where(min_arg == 1) 26 | hsv[..., 0][ind] = 60 * (img[..., 2][ind] - img[..., 0][ind]) / (max_v[ind] - min_v[ind]) + 300 27 | 28 | # S 29 | hsv[..., 1] = max_v.copy() - min_v.copy() 30 | 31 | # V 32 | hsv[..., 2] = max_v.copy() 33 | 34 | return hsv 35 | 36 | # make mask 37 | def get_mask(hsv): 38 | mask = np.zeros_like(hsv[..., 0]) 39 | #mask[np.where((hsv > 180) & (hsv[0] < 260))] = 255 40 | mask[np.logical_and((hsv[..., 0] > 180), (hsv[..., 0] < 260))] = 1 41 | return mask 42 | 43 | # masking 44 | def masking(img, mask): 45 | mask = 1 - mask 46 | out = img.copy() 47 | # mask [h, w] -> [h, w, channel] 48 | mask = np.tile(mask, [3, 1, 1]).transpose([1, 2, 0]) 49 | out *= mask 50 | 51 | return out 52 | 53 | 54 | # Read image 55 | img = cv2.imread("imori.jpg").astype(np.float32) 56 | 57 | # RGB > HSV 58 | hsv = BGR2HSV(img / 255.) 59 | 60 | # color tracking 61 | mask = get_mask(hsv) 62 | 63 | # masking 64 | out = masking(img, mask) 65 | 66 | out = out.astype(np.uint8) 67 | 68 | # Save result 69 | cv2.imwrite("out.jpg", out) 70 | cv2.imshow("result", out) 71 | cv2.waitKey(0) 72 | cv2.destroyAllWindows() 73 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_72.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_72.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_72_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_72_mask.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_73.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_73.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_73.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Grayscale 6 | def BGR2GRAY(img): 7 | # Grayscale 8 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 9 | return gray 10 | 11 | # Bi-Linear interpolation 12 | def bl_interpolate(img, ax=1., ay=1.): 13 | if len(img.shape) > 2: 14 | H, W, C = img.shape 15 | else: 16 | H, W = img.shape 17 | C = 1 18 | 19 | aH = int(ay * H) 20 | aW = int(ax * W) 21 | 22 | # get position of resized image 23 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 24 | x = np.tile(np.arange(aW), (aH, 1)) 25 | 26 | # get position of original position 27 | y = (y / ay) 28 | x = (x / ax) 29 | 30 | ix = np.floor(x).astype(np.int) 31 | iy = np.floor(y).astype(np.int) 32 | 33 | ix = np.minimum(ix, W-2) 34 | iy = np.minimum(iy, H-2) 35 | 36 | # get distance 37 | dx = x - ix 38 | dy = y - iy 39 | 40 | if C > 1: 41 | dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) 42 | dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) 43 | 44 | # interpolation 45 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 46 | 47 | out = np.clip(out, 0, 255) 48 | out = out.astype(np.uint8) 49 | 50 | return out 51 | 52 | 53 | # Read image 54 | img = cv2.imread("imori.jpg").astype(np.float) 55 | 56 | gray = BGR2GRAY(img) 57 | 58 | # Bilinear interpolation 59 | out = bl_interpolate(gray.astype(np.float32), ax=0.5, ay=0.5) 60 | 61 | # Bilinear interpolation 62 | out = bl_interpolate(out, ax=2., ay=2.) 63 | 64 | out = out.astype(np.uint8) 65 | 66 | # Save result 67 | cv2.imshow("result", out) 68 | cv2.waitKey(0) 69 | cv2.imwrite("out.jpg", out) 70 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_74.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_74.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_74.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Grayscale 6 | def BGR2GRAY(img): 7 | # Grayscale 8 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 9 | return gray 10 | 11 | # Bi-Linear interpolation 12 | def bl_interpolate(img, ax=1., ay=1.): 13 | if len(img.shape) > 2: 14 | H, W, C = img.shape 15 | else: 16 | H, W = img.shape 17 | C = 1 18 | 19 | aH = int(ay * H) 20 | aW = int(ax * W) 21 | 22 | # get position of resized image 23 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 24 | x = np.tile(np.arange(aW), (aH, 1)) 25 | 26 | # get position of original position 27 | y = (y / ay) 28 | x = (x / ax) 29 | 30 | ix = np.floor(x).astype(np.int) 31 | iy = np.floor(y).astype(np.int) 32 | 33 | ix = np.minimum(ix, W-2) 34 | iy = np.minimum(iy, H-2) 35 | 36 | # get distance 37 | dx = x - ix 38 | dy = y - iy 39 | 40 | if C > 1: 41 | dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) 42 | dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) 43 | 44 | # interpolation 45 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 46 | 47 | out = np.clip(out, 0, 255) 48 | out = out.astype(np.uint8) 49 | 50 | return out 51 | 52 | 53 | # Read image 54 | img = cv2.imread("imori.jpg").astype(np.float) 55 | 56 | gray = BGR2GRAY(img) 57 | 58 | # Bilinear interpolation 59 | out = bl_interpolate(gray.astype(np.float32), ax=0.5, ay=0.5) 60 | 61 | # Bilinear interpolation 62 | out = bl_interpolate(out, ax=2., ay=2.) 63 | 64 | out = np.abs(out - gray) 65 | 66 | out = out / out.max() * 255 67 | 68 | out = out.astype(np.uint8) 69 | 70 | # Save result 71 | cv2.imshow("result", out) 72 | cv2.waitKey(0) 73 | cv2.imwrite("out.jpg", out) 74 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Grayscale 6 | def BGR2GRAY(img): 7 | # Grayscale 8 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 9 | return gray 10 | 11 | # Bi-Linear interpolation 12 | def bl_interpolate(img, ax=1., ay=1.): 13 | if len(img.shape) > 2: 14 | H, W, C = img.shape 15 | else: 16 | H, W = img.shape 17 | C = 1 18 | 19 | aH = int(ay * H) 20 | aW = int(ax * W) 21 | 22 | # get position of resized image 23 | y = np.arange(aH).repeat(aW).reshape(aW, -1) 24 | x = np.tile(np.arange(aW), (aH, 1)) 25 | 26 | # get position of original position 27 | y = (y / ay) 28 | x = (x / ax) 29 | 30 | ix = np.floor(x).astype(np.int) 31 | iy = np.floor(y).astype(np.int) 32 | 33 | ix = np.minimum(ix, W-2) 34 | iy = np.minimum(iy, H-2) 35 | 36 | # get distance 37 | dx = x - ix 38 | dy = y - iy 39 | 40 | if C > 1: 41 | dx = np.repeat(np.expand_dims(dx, axis=-1), C, axis=-1) 42 | dy = np.repeat(np.expand_dims(dy, axis=-1), C, axis=-1) 43 | 44 | # interpolation 45 | out = (1-dx) * (1-dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix+1] + (1 - dx) * dy * img[iy+1, ix] + dx * dy * img[iy+1, ix+1] 46 | 47 | out = np.clip(out, 0, 255) 48 | out = out.astype(np.uint8) 49 | 50 | return out 51 | 52 | # make image pyramid 53 | def make_pyramid(gray): 54 | # first element 55 | pyramid = [gray] 56 | # each scale 57 | for i in range(1, 6): 58 | # define scale 59 | a = 2. ** i 60 | 61 | # down scale 62 | p = bl_interpolate(gray, ax=1./a, ay=1. / a) 63 | 64 | # add pyramid list 65 | pyramid.append(p) 66 | 67 | return pyramid 68 | 69 | # Read image 70 | img = cv2.imread("imori.jpg").astype(np.float) 71 | 72 | gray = BGR2GRAY(img) 73 | 74 | # pyramid 75 | pyramid = make_pyramid(gray) 76 | 77 | for i in range(6): 78 | cv2.imwrite("out_{}.jpg".format(2**i), pyramid[i].astype(np.uint8)) 79 | plt.subplot(1, 6, i+1) 80 | plt.imshow(pyramid[i], cmap='gray') 81 | plt.axis('off') 82 | plt.xticks(color="None") 83 | plt.yticks(color="None") 84 | 85 | plt.show() 86 | 87 | 88 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_75_1.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_75_16.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_75_2.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_32.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_75_32.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_75_4.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_75_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_75_8.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_76.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_76.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_77.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_77.jpg -------------------------------------------------------------------------------- /Question_71_80/answers/answer_77.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Gabor 6 | def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): 7 | # get half size 8 | d = K_size // 2 9 | 10 | # prepare kernel 11 | gabor = np.zeros((K_size, K_size), dtype=np.float32) 12 | 13 | # each value 14 | for y in range(K_size): 15 | for x in range(K_size): 16 | # distance from center 17 | px = x - d 18 | py = y - d 19 | 20 | # degree -> radian 21 | theta = angle / 180. * np.pi 22 | 23 | # get kernel x 24 | _x = np.cos(theta) * px + np.sin(theta) * py 25 | 26 | # get kernel y 27 | _y = -np.sin(theta) * px + np.cos(theta) * py 28 | 29 | # fill kernel 30 | gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) 31 | 32 | # kernel normalization 33 | gabor /= np.sum(np.abs(gabor)) 34 | 35 | return gabor 36 | 37 | 38 | # get gabor kernel 39 | gabor = Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0) 40 | 41 | # Visualize 42 | # normalize to [0, 255] 43 | out = gabor - np.min(gabor) 44 | out /= np.max(out) 45 | out *= 255 46 | 47 | out = out.astype(np.uint8) 48 | cv2.imwrite("out.jpg", out) 49 | cv2.imshow("result", out) 50 | cv2.waitKey(0) 51 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_78.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_78.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_78.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | # Gabor 7 | def Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=0): 8 | # get half size 9 | d = K_size // 2 10 | 11 | # prepare kernel 12 | gabor = np.zeros((K_size, K_size), dtype=np.float32) 13 | 14 | # each value 15 | for y in range(K_size): 16 | for x in range(K_size): 17 | # distance from center 18 | px = x - d 19 | py = y - d 20 | 21 | # degree -> radian 22 | theta = angle / 180. * np.pi 23 | 24 | # get kernel x 25 | _x = np.cos(theta) * px + np.sin(theta) * py 26 | 27 | # get kernel y 28 | _y = -np.sin(theta) * px + np.cos(theta) * py 29 | 30 | # fill kernel 31 | gabor[y, x] = np.exp(-(_x**2 + Gamma**2 * _y**2) / (2 * Sigma**2)) * np.cos(2*np.pi*_x/Lambda + Psi) 32 | 33 | # kernel normalization 34 | gabor /= np.sum(np.abs(gabor)) 35 | 36 | return gabor 37 | 38 | 39 | # define each angle 40 | As = [0, 45, 90, 135] 41 | 42 | # prepare pyplot 43 | plt.subplots_adjust(left=0, right=1, top=1, bottom=0, hspace=0, wspace=0.2) 44 | 45 | # each angle 46 | for i, A in enumerate(As): 47 | # get gabor kernel 48 | gabor = Gabor_filter(K_size=111, Sigma=10, Gamma=1.2, Lambda=10, Psi=0, angle=A) 49 | 50 | # normalize to [0, 255] 51 | out = gabor - np.min(gabor) 52 | out /= np.max(out) 53 | out *= 255 54 | 55 | out = out.astype(np.uint8) 56 | plt.subplot(1, 4, i+1) 57 | plt.imshow(out, cmap='gray') 58 | plt.axis('off') 59 | plt.title("Angle "+str(A)) 60 | 61 | plt.savefig("out.png") 62 | plt.show() 63 | -------------------------------------------------------------------------------- /Question_71_80/answers/answer_79.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_79.png -------------------------------------------------------------------------------- /Question_71_80/answers/answer_80.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/answers/answer_80.jpg -------------------------------------------------------------------------------- /Question_71_80/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_71_80/imori.jpg -------------------------------------------------------------------------------- /Question_81_90/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/README.pdf -------------------------------------------------------------------------------- /Question_81_90/answers/answer_81.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/answers/answer_81.jpg -------------------------------------------------------------------------------- /Question_81_90/answers/answer_81.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Hessian corner detection 6 | def Hessian_corner(img): 7 | 8 | ## Grayscale 9 | def BGR2GRAY(img): 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | gray = gray.astype(np.uint8) 12 | return gray 13 | 14 | ## Sobel 15 | def Sobel_filtering(gray): 16 | # get shape 17 | H, W = gray.shape 18 | 19 | # sobel kernel 20 | sobely = np.array(((1, 2, 1), 21 | (0, 0, 0), 22 | (-1, -2, -1)), dtype=np.float32) 23 | 24 | sobelx = np.array(((1, 0, -1), 25 | (2, 0, -2), 26 | (1, 0, -1)), dtype=np.float32) 27 | 28 | # padding 29 | tmp = np.pad(gray, (1, 1), 'edge') 30 | 31 | # prepare 32 | Ix = np.zeros_like(gray, dtype=np.float32) 33 | Iy = np.zeros_like(gray, dtype=np.float32) 34 | 35 | # get differential 36 | for y in range(H): 37 | for x in range(W): 38 | Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx) 39 | Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely) 40 | 41 | Ix2 = Ix ** 2 42 | Iy2 = Iy ** 2 43 | Ixy = Ix * Iy 44 | 45 | return Ix2, Iy2, Ixy 46 | 47 | 48 | 49 | ## Hessian 50 | def corner_detect(gray, Ix2, Iy2, Ixy): 51 | # get shape 52 | H, W = gray.shape 53 | 54 | # prepare for show detection 55 | out = np.array((gray, gray, gray)) 56 | out = np.transpose(out, (1,2,0)) 57 | 58 | # get Hessian value 59 | Hes = np.zeros((H, W)) 60 | 61 | for y in range(H): 62 | for x in range(W): 63 | Hes[y,x] = Ix2[y,x] * Iy2[y,x] - Ixy[y,x] ** 2 64 | 65 | ## Detect Corner and show 66 | for y in range(H): 67 | for x in range(W): 68 | if Hes[y,x] == np.max(Hes[max(y-1, 0) : min(y+2, H), max(x-1, 0) : min(x+2, W)]) and Hes[y, x] > np.max(Hes) * 0.1: 69 | out[y, x] = [0, 0, 255] 70 | 71 | out = out.astype(np.uint8) 72 | 73 | return out 74 | 75 | 76 | # 1. grayscale 77 | gray = BGR2GRAY(img) 78 | 79 | # 2. get difference image 80 | Ix2, Iy2, Ixy = Sobel_filtering(gray) 81 | 82 | # 3. corner detection 83 | out = corner_detect(gray, Ix2, Iy2, Ixy) 84 | 85 | return out 86 | 87 | 88 | # Read image 89 | img = cv2.imread("thorino.jpg").astype(np.float32) 90 | 91 | # Hessian corner detection 92 | out = Hessian_corner(img) 93 | 94 | cv2.imwrite("out.jpg", out) 95 | cv2.imshow("result", out) 96 | cv2.waitKey(0) 97 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_82.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/answers/answer_82.png -------------------------------------------------------------------------------- /Question_81_90/answers/answer_83.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/answers/answer_83.jpg -------------------------------------------------------------------------------- /Question_81_90/answers/answer_84.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/answers/answer_84.png -------------------------------------------------------------------------------- /Question_81_90/answers/answer_84.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | ## Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | ## Database 13 | def get_DB(): 14 | # get image paths 15 | train = glob("dataset/train_*") 16 | train.sort() 17 | 18 | # prepare database 19 | db = np.zeros((len(train), 13), dtype=np.int32) 20 | 21 | # each image 22 | for i, path in enumerate(train): 23 | img = dic_color(cv2.imread(path)) 24 | # get histogram 25 | for j in range(4): 26 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 27 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 28 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 29 | 30 | # get class 31 | if 'akahara' in path: 32 | cls = 0 33 | elif 'madara' in path: 34 | cls = 1 35 | 36 | # store class label 37 | db[i, -1] = cls 38 | 39 | img_h = img.copy() // 64 40 | img_h[..., 1] += 4 41 | img_h[..., 2] += 8 42 | plt.subplot(2, 5, i+1) 43 | plt.hist(img_h.ravel(), bins=12, rwidth=0.8) 44 | plt.title(path) 45 | 46 | print(db) 47 | plt.show() 48 | 49 | # get database 50 | get_DB() 51 | -------------------------------------------------------------------------------- /Question_81_90/answers/answer_85.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | # Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | # Database 13 | def get_DB(): 14 | # get training image path 15 | train = glob("dataset/train_*") 16 | train.sort() 17 | 18 | # prepare database 19 | db = np.zeros((len(train), 13), dtype=np.int32) 20 | 21 | # prepare path database 22 | pdb = [] 23 | 24 | # each image 25 | for i, path in enumerate(train): 26 | # read image 27 | img = dic_color(cv2.imread(path)) 28 | 29 | #get histogram 30 | for j in range(4): 31 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 32 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 33 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 34 | 35 | # get class 36 | if 'akahara' in path: 37 | cls = 0 38 | elif 'madara' in path: 39 | cls = 1 40 | 41 | # store class label 42 | db[i, -1] = cls 43 | 44 | # store image path 45 | pdb.append(path) 46 | 47 | return db, pdb 48 | 49 | # test 50 | def test_DB(db, pdb): 51 | # get test image path 52 | test = glob("dataset/test_*") 53 | test.sort() 54 | 55 | success_num = 0. 56 | 57 | # each image 58 | for path in test: 59 | # read image 60 | img = dic_color(cv2.imread(path)) 61 | 62 | # get histogram 63 | hist = np.zeros(12, dtype=np.int32) 64 | for j in range(4): 65 | hist[j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 66 | hist[j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 67 | hist[j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 68 | 69 | # get histogram difference 70 | difs = np.abs(db[:, :12] - hist) 71 | difs = np.sum(difs, axis=1) 72 | 73 | # get argmin of difference 74 | pred_i = np.argmin(difs) 75 | 76 | # get prediction label 77 | pred = db[pred_i, -1] 78 | 79 | if pred == 0: 80 | pl = "akahara" 81 | elif pred == 1: 82 | pl = "madara" 83 | 84 | print(path, "is similar >>", pdb[pred_i], " Pred >>", pl) 85 | 86 | db, pdb = get_DB() 87 | test_DB(db, pdb) -------------------------------------------------------------------------------- /Question_81_90/answers/answer_88.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | # Dicrease color 7 | def dic_color(img): 8 | img //= 63 9 | img = img * 64 + 32 10 | return img 11 | 12 | 13 | # Database 14 | def get_DB(): 15 | # get training image path 16 | train = glob("dataset/test_*") 17 | train.sort() 18 | 19 | # prepare database 20 | db = np.zeros((len(train), 13), dtype=np.int32) 21 | pdb = [] 22 | 23 | # each train 24 | for i, path in enumerate(train): 25 | # read image 26 | img = dic_color(cv2.imread(path)) 27 | # histogram 28 | for j in range(4): 29 | db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0]) 30 | db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0]) 31 | db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0]) 32 | 33 | # get class 34 | if 'akahara' in path: 35 | cls = 0 36 | elif 'madara' in path: 37 | cls = 1 38 | 39 | # store class label 40 | db[i, -1] = cls 41 | 42 | # add image path 43 | pdb.append(path) 44 | 45 | return db, pdb 46 | 47 | # k-Means step1 48 | def k_means_step1(db, pdb, Class=2): 49 | # copy database 50 | feats = db.copy() 51 | 52 | # initiate random seed 53 | np.random.seed(1) 54 | 55 | # assign random class 56 | for i in range(len(feats)): 57 | if np.random.random() < 0.5: 58 | feats[i, -1] = 0 59 | else: 60 | feats[i, -1] = 1 61 | 62 | # prepare gravity 63 | gs = np.zeros((Class, 12), dtype=np.float32) 64 | 65 | # get gravity 66 | for i in range(Class): 67 | gs[i] = np.mean(feats[np.where(feats[..., -1] == i)[0], :12], axis=0) 68 | print("assigned label") 69 | print(feats) 70 | print("Grabity") 71 | print(gs) 72 | 73 | 74 | db, pdb = get_DB() 75 | k_means_step1(db, pdb) -------------------------------------------------------------------------------- /Question_81_90/dataset/test_akahara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/test_akahara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/test_akahara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/test_akahara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/test_madara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/test_madara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/test_madara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/test_madara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_akahara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_akahara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_akahara_3.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_akahara_4.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_akahara_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_akahara_5.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_madara_1.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_madara_2.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_madara_3.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_madara_4.jpg -------------------------------------------------------------------------------- /Question_81_90/dataset/train_madara_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/dataset/train_madara_5.jpg -------------------------------------------------------------------------------- /Question_81_90/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/imori.jpg -------------------------------------------------------------------------------- /Question_81_90/thorino.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_81_90/thorino.jpg -------------------------------------------------------------------------------- /Question_91_100/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/.DS_Store -------------------------------------------------------------------------------- /Question_91_100/README.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/README.pdf -------------------------------------------------------------------------------- /Question_91_100/answers/_answer_95.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(0) 4 | 5 | 6 | class NN: 7 | def __init__(self, ind=2, w=64, w2=64, outd=1, lr=0.1): 8 | self.w2 = np.random.randn(ind, w) 9 | self.b2 = np.random.randn(w) 10 | self.w3 = np.random.randn(w, w2) 11 | self.b3 = np.random.randn(w2) 12 | self.wout = np.random.randn(w2, outd) 13 | self.bout = np.random.randn(outd) 14 | self.lr = lr 15 | 16 | def forward(self, x): 17 | self.z1 = x 18 | self.z2 = self.sigmoid(np.dot(self.z1, self.w2) + self.b2) 19 | self.z3 = self.sigmoid(np.dot(self.z2, self.w3) + self.b3) 20 | self.out = self.sigmoid(np.dot(self.z3, self.wout) + self.bout) 21 | return self.out 22 | 23 | def train(self, x, t): 24 | # backpropagation output layer 25 | out_d = 2*(self.out - t) * self.out * (1 - self.out) 26 | out_dW = np.dot(self.z3.T, out_d) 27 | out_dB = np.dot(np.ones([1, out_d.shape[0]]), out_d) 28 | self.wout -= self.lr * out_dW 29 | self.bout -= self.lr * out_dB[0] 30 | 31 | w3_d = np.dot(out_d, self.wout.T) * self.z3 * (1 - self.z3) 32 | w3_dW = np.dot(self.z2.T, w3_d) 33 | w3_dB = np.dot(np.ones([1, w3_d.shape[0]]), w3_d) 34 | self.w3 -= self.lr * w3_dW 35 | self.b3 -= self.lr * w3_dB[0] 36 | 37 | # backpropagation inter layer 38 | w2_d = np.dot(w3_d, self.w3.T) * self.z2 * (1 - self.z2) 39 | w2_dW = np.dot(self.z1.T, w2_d) 40 | w2_dB = np.dot(np.ones([1, w2_d.shape[0]]), w2_d) 41 | self.w2 -= self.lr * w2_dW 42 | self.b2 -= self.lr * w2_dB[0] 43 | 44 | def sigmoid(self, x): 45 | return 1. / (1. + np.exp(-x)) 46 | 47 | train_x = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32) 48 | train_t = np.array([[0], [1], [1], [0]], dtype=np.float32) 49 | 50 | nn = NN(ind=train_x.shape[1]) 51 | 52 | # train 53 | for i in range(1000): 54 | nn.forward(train_x) 55 | nn.train(train_x, train_t) 56 | 57 | # test 58 | for j in range(4): 59 | x = train_x[j] 60 | t = train_t[j] 61 | print("in:", x, "pred:", nn.forward(x)) 62 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_100.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_100.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_100_gt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_100_gt.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_91.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_91.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_91.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | 7 | # K-means step1 8 | def k_means_step1(img, Class=5): 9 | # get shape 10 | H, W, C = img.shape 11 | 12 | # initiate random seed 13 | np.random.seed(0) 14 | 15 | # reshape 16 | img = np.reshape(img, (H * W, -1)) 17 | 18 | # select one index randomly 19 | i = np.random.choice(np.arange(H * W), Class, replace=False) 20 | Cs = img[i].copy() 21 | 22 | print(Cs) 23 | 24 | clss = np.zeros((H * W), dtype=int) 25 | 26 | # each pixel 27 | for i in range(H * W): 28 | # get distance from base pixel 29 | dis = np.sqrt(np.sum((Cs - img[i]) ** 2, axis=1)) 30 | # get argmin distance 31 | clss[i] = np.argmin(dis) 32 | 33 | # show 34 | out = np.reshape(clss, (H, W)) * 50 35 | out = out.astype(np.uint8) 36 | 37 | return out 38 | 39 | 40 | # read image 41 | img = cv2.imread("imori.jpg").astype(np.float32) 42 | 43 | # K-means step2 44 | out = k_means_step1(img) 45 | 46 | cv2.imwrite("out.jpg", out) 47 | cv2.imshow("result", out) 48 | cv2.waitKey(0) 49 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_92.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | def k_means(img, Class=5): 7 | # get shape 8 | H, W, C = img.shape 9 | 10 | # initiate random seed 11 | np.random.seed(0) 12 | 13 | # reshape image 14 | img = np.reshape(img, (H * W, -1)) 15 | 16 | # get index randomly 17 | i = np.random.choice(np.arange(H * W), Class, replace=False) 18 | Cs = img[i].copy() 19 | 20 | while True: 21 | # prepare pixel class label 22 | clss = np.zeros((H * W), dtype=int) 23 | 24 | # each pixel 25 | for i in range(H * W): 26 | # get distance from index pixel 27 | dis = np.sqrt(np.sum((Cs - img[i])**2, axis=1)) 28 | # get argmin distance 29 | clss[i] = np.argmin(dis) 30 | 31 | # selected pixel values 32 | Cs_tmp = np.zeros((Class, 3)) 33 | 34 | # each class label 35 | for i in range(Class): 36 | Cs_tmp[i] = np.mean(img[clss == i], axis=0) 37 | 38 | # if not any change 39 | if (Cs == Cs_tmp).all(): 40 | break 41 | else: 42 | Cs = Cs_tmp.copy() 43 | 44 | # prepare out image 45 | out = np.zeros((H * W, 3), dtype=np.float32) 46 | 47 | # assign selected pixel values 48 | for i in range(Class): 49 | out[clss == i] = Cs[i] 50 | 51 | print(Cs) 52 | 53 | out = np.clip(out, 0, 255) 54 | 55 | # reshape out image 56 | out = np.reshape(out, (H, W, 3)) 57 | out = out.astype(np.uint8) 58 | 59 | return out 60 | 61 | # read image 62 | img = cv2.imread("imori.jpg").astype(np.float32) 63 | 64 | # K-means 65 | out = k_means(img) 66 | 67 | cv2.imwrite("out.jpg", out) 68 | cv2.imshow("result", out) 69 | cv2.waitKey(0) 70 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92_k10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_92_k10.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_92_m.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_92_m.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_93.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # get IoU overlap ratio 4 | def iou(a, b): 5 | # get area of a 6 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 7 | # get area of b 8 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 9 | 10 | # get left top x of IoU 11 | iou_x1 = np.maximum(a[0], b[0]) 12 | # get left top y of IoU 13 | iou_y1 = np.maximum(a[1], b[1]) 14 | # get right bottom of IoU 15 | iou_x2 = np.minimum(a[2], b[2]) 16 | # get right bottom of IoU 17 | iou_y2 = np.minimum(a[3], b[3]) 18 | 19 | # get width of IoU 20 | iou_w = iou_x2 - iou_x1 21 | # get height of IoU 22 | iou_h = iou_y2 - iou_y1 23 | 24 | # get area of IoU 25 | area_iou = iou_w * iou_h 26 | # get overlap ratio between IoU and all area 27 | iou = area_iou / (area_a + area_b - area_iou) 28 | 29 | return iou 30 | 31 | # [x1, y1, x2, y2] 32 | a = np.array((50, 50, 150, 150), dtype=np.float32) 33 | 34 | b = np.array((60, 60, 170, 160), dtype=np.float32) 35 | 36 | print(iou(a, b)) 37 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_94.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_94.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_94.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | np.random.seed(0) 5 | 6 | # get IoU overlap ratio 7 | def iou(a, b): 8 | # get area of a 9 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 10 | # get area of b 11 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 12 | 13 | # get left top x of IoU 14 | iou_x1 = np.maximum(a[0], b[0]) 15 | # get left top y of IoU 16 | iou_y1 = np.maximum(a[1], b[1]) 17 | # get right bottom of IoU 18 | iou_x2 = np.minimum(a[2], b[2]) 19 | # get right bottom of IoU 20 | iou_y2 = np.minimum(a[3], b[3]) 21 | 22 | # get width of IoU 23 | iou_w = iou_x2 - iou_x1 24 | # get height of IoU 25 | iou_h = iou_y2 - iou_y1 26 | 27 | # get area of IoU 28 | area_iou = iou_w * iou_h 29 | # get overlap ratio between IoU and all area 30 | iou = area_iou / (area_a + area_b - area_iou) 31 | 32 | return iou 33 | 34 | 35 | # crop and create database 36 | def crop_bbox(img, gt, Crop_N=200, L=60, th=0.5): 37 | # get shape 38 | H, W, C = img.shape 39 | 40 | # each crop 41 | for i in range(Crop_N): 42 | # get left top x of crop bounding box 43 | x1 = np.random.randint(W - L) 44 | # get left top y of crop bounding box 45 | y1 = np.random.randint(H - L) 46 | # get right bottom x of crop bounding box 47 | x2 = x1 + L 48 | # get right bottom y of crop bounding box 49 | y2 = y1 + L 50 | 51 | # crop bounding box 52 | crop = np.array((x1, y1, x2, y2)) 53 | 54 | # get IoU between crop box and gt 55 | _iou = iou(gt, crop) 56 | 57 | # assign label 58 | if _iou >= th: 59 | cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 1) 60 | label = 1 61 | else: 62 | cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 1) 63 | label = 0 64 | 65 | return img 66 | 67 | # read image 68 | img = cv2.imread("imori_1.jpg") 69 | 70 | # gt bounding box 71 | gt = np.array((47, 41, 129, 103), dtype=np.float32) 72 | 73 | # get crop bounding box 74 | img = crop_bbox(img, gt) 75 | 76 | # draw gt 77 | cv2.rectangle(img, (gt[0], gt[1]), (gt[2], gt[3]), (0,255,0), 1) 78 | 79 | cv2.imwrite("out.jpg", img) 80 | cv2.imshow("result", img) 81 | cv2.waitKey(0) 82 | 83 | -------------------------------------------------------------------------------- /Question_91_100/answers/answer_98.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_98.jpg -------------------------------------------------------------------------------- /Question_91_100/answers/answer_99.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/answers/answer_99.jpg -------------------------------------------------------------------------------- /Question_91_100/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/imori.jpg -------------------------------------------------------------------------------- /Question_91_100/imori_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/imori_1.jpg -------------------------------------------------------------------------------- /Question_91_100/imori_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/imori_2.jpg -------------------------------------------------------------------------------- /Question_91_100/imori_many.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/imori_many.jpg -------------------------------------------------------------------------------- /Question_91_100/madara.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Question_91_100/madara.jpg -------------------------------------------------------------------------------- /Tutorial/answer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | cv::Mat out = img.clone(); 12 | 13 | for(int i=0; i(j, i)[0]; 16 | out.at(j, i)[0] = out.at(j, i)[2]; 17 | out.at(j, i)[2] = tmp; 18 | } 19 | } 20 | 21 | cv::imwrite("out.jpg", out); 22 | cv::imshow("sample", out); 23 | cv::waitKey(0); 24 | cv::destroyAllWindows(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Tutorial/answer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Processing 6 | 7 | 8 | 9 | 10 |

Image Processing!

11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Tutorial/assets/out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/out.jpg -------------------------------------------------------------------------------- /Tutorial/assets/out_cpp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/out_cpp.png -------------------------------------------------------------------------------- /Tutorial/assets/out_practice.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/out_practice.jpg -------------------------------------------------------------------------------- /Tutorial/assets/sampe_html1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sampe_html1.jpg -------------------------------------------------------------------------------- /Tutorial/assets/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample.jpg -------------------------------------------------------------------------------- /Tutorial/assets/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample.png -------------------------------------------------------------------------------- /Tutorial/assets/sample1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample1.png -------------------------------------------------------------------------------- /Tutorial/assets/sample10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample10.png -------------------------------------------------------------------------------- /Tutorial/assets/sample2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample2.png -------------------------------------------------------------------------------- /Tutorial/assets/sample3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample3.png -------------------------------------------------------------------------------- /Tutorial/assets/sample4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample4.png -------------------------------------------------------------------------------- /Tutorial/assets/sample5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample5.png -------------------------------------------------------------------------------- /Tutorial/assets/sample6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample6.png -------------------------------------------------------------------------------- /Tutorial/assets/sample7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample7.png -------------------------------------------------------------------------------- /Tutorial/assets/sample8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample8.png -------------------------------------------------------------------------------- /Tutorial/assets/sample9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample9.png -------------------------------------------------------------------------------- /Tutorial/assets/sample_html2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/assets/sample_html2.jpg -------------------------------------------------------------------------------- /Tutorial/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/Tutorial/imori.jpg -------------------------------------------------------------------------------- /Tutorial/sample.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, const char* argv[]){ 6 | cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR); 7 | 8 | int width = img.rows; 9 | int height = img.cols; 10 | 11 | cv::Mat img2 = img.clone(); 12 | 13 | //cv::Mat disp(cv::Size(height, width*2+10), CV_8UC3, cv::Scalar(0,0,0)); 14 | 15 | int i = 0, j = 0; 16 | 17 | for(i=0; i(j, i)[0] = 0; 20 | img.at(j, i)[1] = 400; 21 | img.at(j, i)[2] = -200; 22 | } 23 | } 24 | 25 | cv::Mat disp; 26 | cv::Mat tmp[3]; 27 | tmp[0] = img; 28 | tmp[1] = cv::Mat (cv::Size(10, height), CV_8UC3, cv::Scalar(0,0,0)); 29 | tmp[2] = img2; 30 | cv::hconcat(tmp, 3, disp); 31 | 32 | cv::imshow("sample", disp); 33 | cv::waitKey(0); 34 | cv::destroyAllWindows(); 35 | 36 | cv::imwrite("out.jpg", disp); 37 | 38 | return 0; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Tutorial/sample.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Processing 6 | 7 | 8 | 9 | 10 |

Image Processing!

11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Tutorial/sample_ex.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Processing 6 | 7 | 8 | 9 | 10 |

Image Processing!

11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /assets/imori.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/assets/imori.jpg -------------------------------------------------------------------------------- /assets/imori_512x512.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/assets/imori_512x512.jpg -------------------------------------------------------------------------------- /assets/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gzr2017/ImageProcessing100Wen/9e0d361f6eb61c9f23ac3e14e36a384b994c5511/assets/sample.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | matplotlib 3 | opencv-python 4 | -------------------------------------------------------------------------------- /yobi/crop.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # read image 5 | img = cv2.imread("imori_1.jpg") 6 | H, W, C = img.shape 7 | 8 | gt = np.array((47, 41, 129, 103), dtype=np.float32) 9 | cv2.rectangle(img, (gt[0], gt[1]), (gt[2], gt[3]), (0,255,255), 1) 10 | 11 | def iou(a, b): 12 | area_a = (a[2] - a[0]) * (a[3] - a[1]) 13 | area_b = (b[2] - b[0]) * (b[3] - b[1]) 14 | iou_x1 = np.maximum(a[0], b[0]) 15 | iou_y1 = np.maximum(a[1], b[1]) 16 | iou_x2 = np.minimum(a[2], b[2]) 17 | iou_y2 = np.minimum(a[3], b[3]) 18 | iou_w = max(iou_x2 - iou_x1, 0) 19 | iou_h = max(iou_y2 - iou_y1, 0) 20 | area_iou = iou_w * iou_h 21 | iou = area_iou / (area_a + area_b - area_iou) 22 | return iou 23 | 24 | np.random.seed(0) 25 | 26 | Crop_num = 100 27 | L = 56 28 | 29 | for _ in range(Crop_num): 30 | 31 | x1 = np.random.randint(W-L) 32 | y1 = np.random.randint(H-L) 33 | x2 = x1 + L 34 | y2 = y1 + L 35 | 36 | crop = np.array((x1, y1, x2, y2)) 37 | 38 | _iou = iou(gt, crop) 39 | 40 | if _iou >= 0.5: 41 | cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 1) 42 | else: 43 | cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 1) 44 | 45 | cv2.imshow("", img) 46 | cv2.waitKey(0) 47 | -------------------------------------------------------------------------------- /yobi/hog.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from glob import glob 5 | 6 | img = cv2.imread("imori.jpg").astype(np.float32) 7 | H, W, C = img.shape 8 | 9 | # Grayscale 10 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 11 | 12 | def hog(gray): 13 | # Magnitude and gradient 14 | gray = np.pad(gray, (1, 1), 'edge') 15 | 16 | gx = gray[1:H+1, 2:] - gray[1:H+1, :W] 17 | gy = gray[2:, 1:W+1] - gray[:H, 1:W+1] 18 | gx[gx == 0] = 0.000001 19 | 20 | mag = np.sqrt(gx ** 2 + gy ** 2) 21 | gra = np.arctan(gy / gx) 22 | gra[gra<0] = np.pi / 2 + gra[gra < 0] + np.pi / 2 23 | 24 | # Gradient histogram 25 | gra_n = np.zeros_like(gra, dtype=np.int) 26 | 27 | d = np.pi / 9 28 | for i in range(9): 29 | gra_n[np.where((gra >= d * i) & (gra <= d * (i+1)))] = i 30 | 31 | 32 | N = 8 33 | HH = H // N 34 | HW = W // N 35 | Hist = np.zeros((HH, HW, 9), dtype=np.float32) 36 | for y in range(HH): 37 | for x in range(HW): 38 | for j in range(N): 39 | for i in range(N): 40 | Hist[y, x, gra_n[y*4+j, x*4+i]] += mag[y*4+j, x*4+i] 41 | 42 | ## Normalization 43 | C = 3 44 | eps = 1 45 | for y in range(HH): 46 | for x in range(HW): 47 | #for i in range(9): 48 | Hist[y, x] /= np.sqrt(np.sum(Hist[max(y-1,0):min(y+2, HH), max(x-1,0):min(x+2, HW)] ** 2) + eps) 49 | 50 | return Hist 51 | 52 | 53 | train = glob("../Question_81_90/train_*") 54 | train.sort() 55 | 56 | fs = np.zeros((len(train), 16*16*9)) 57 | ls = np.zeros((len(train))) 58 | 59 | for i, path in enumerate(train): 60 | img = cv2.imread(path).astype(np.float32) 61 | # Grayscale 62 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 63 | fs[i] = hog(gray).ravel() 64 | ls[i] = 0 if 'akahara' in path else 1 65 | 66 | from sklearn import svm 67 | clf = svm.SVC(kernel='linear') 68 | clf.fit(X=fs, y=ls) 69 | 70 | 71 | img = cv2.imread("imori.jpg").astype(np.float32) 72 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 73 | 74 | f = hog(gray).ravel() 75 | f = f[None, ...] 76 | print(clf.predict(f)) 77 | 78 | 79 | img = cv2.imread("madara.jpg").astype(np.float32) 80 | gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0] 81 | 82 | f = hog(gray).ravel() 83 | f = f[None, ...] 84 | print(clf.predict(f)) 85 | -------------------------------------------------------------------------------- /yobi/neuralnet2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def sigmoid(x): 4 | return 1.0/(1+ np.exp(-x)) 5 | 6 | def sigmoid_derivative(x): 7 | return x * (1.0 - x) 8 | 9 | class NeuralNetwork: 10 | def __init__(self, x, t): 11 | self.z1 = x 12 | self.w2 = np.random.rand(self.z1.shape[1],3) 13 | self.w3 = np.random.rand(3,1) 14 | self.t = t 15 | self.out = np.zeros(self.t.shape) 16 | 17 | def feedforward(self): 18 | self.z2 = sigmoid(np.dot(self.z1, self.w2)) 19 | self.out = sigmoid(np.dot(self.z2, self.w3)) 20 | 21 | def backprop(self): 22 | # application of the chain rule to find derivative of the loss function with respect to w3 and w2 23 | d_w3 = np.dot(self.z2.T, (2*(self.t - self.out) * sigmoid_derivative(self.out))) 24 | d_w2 = np.dot(self.z1.T, (np.dot(2*(self.t - self.out) * sigmoid_derivative(self.out), self.w3.T) * sigmoid_derivative(self.z2))) 25 | 26 | # update the weights with the derivative (slope) of the loss function 27 | self.w2 += d_w2 28 | self.w3 += d_w3 29 | 30 | 31 | if __name__ == "__main__": 32 | X = np.array([[0, 0], 33 | [0, 1], 34 | [1, 0], 35 | [1, 1]]) 36 | y = np.array([[0],[1],[1],[0]]) 37 | nn = NeuralNetwork(X,y) 38 | print(X.shape, y.shape) 39 | for i in range(15000): 40 | nn.feedforward() 41 | nn.backprop() 42 | 43 | print(nn.out) 44 | -------------------------------------------------------------------------------- /yobi/nn.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | np.random.seed(0) 4 | 5 | 6 | class NN: 7 | def __init__(self, ind=2, w=64, outd=1, lr=0.1): 8 | self.w2 = np.random.randn(ind, w) 9 | self.b2 = np.random.randn(w) 10 | self.wout = np.random.randn(w, outd) 11 | self.bout = np.random.randn(outd) 12 | self.lr = lr 13 | 14 | def forward(self, x): 15 | self.z1 = x 16 | self.z2 = self.sigmoid(np.dot(self.z1, self.w2) + self.b2) 17 | self.out = self.sigmoid(np.dot(self.z2, self.wout) + self.bout) 18 | return self.out 19 | 20 | def train(self, x, t): 21 | # backpropagation output layer 22 | out_d = 2*(self.out - t) * self.out * (1 - self.out) 23 | out_dW = np.dot(self.z2.T, out_d) 24 | out_dB = np.dot(np.ones([1, out_d.shape[0]]), out_d) 25 | self.wout -= self.lr * out_dW 26 | self.bout -= self.lr * out_dB[0] 27 | 28 | # backpropagation inter layer 29 | w2_d = np.dot(out_d, self.wout.T) * self.z2 * (1 - self.z2) 30 | w2_dW = np.dot(self.z1.T, w2_d) 31 | w2_dB = np.dot(np.ones([1, w2_d.shape[0]]), w2_d) 32 | self.w2 -= self.lr * w2_dW 33 | self.b2 -= self.lr * w2_dB[0] 34 | 35 | def sigmoid(self, x): 36 | return 1. / (1. + np.exp(-x)) 37 | 38 | train_x = np.array([[0,0], [0,1], [1,0], [1,1]], dtype=np.float32) 39 | train_t = np.array([[0], [1], [1], [0]], dtype=np.float32) 40 | 41 | nn = NN(ind=train_x.shape[1]) 42 | 43 | # training 44 | for i in range(1000): 45 | nn.forward(train_x) 46 | nn.train(train_x, train_t) 47 | 48 | # test 49 | for j in range(4): 50 | x = train_x[j] 51 | t = train_t[j] 52 | print("in:", x, "pred:", nn.forward(x)) 53 | --------------------------------------------------------------------------------