Skip to content

Commit

Permalink
folder orgnized
Browse files Browse the repository at this point in the history
  • Loading branch information
郭子睿 authored and 郭子睿 committed Nov 25, 2019
1 parent 1dff641 commit c0579e6
Show file tree
Hide file tree
Showing 257 changed files with 11,730 additions and 2,726 deletions.
38 changes: 27 additions & 11 deletions Question_01_10/answers_cpp/answer_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,43 @@
#include <opencv2/highgui.hpp>
#include <iostream>

int main(int argc, const char* argv[]){
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// Channel swap
cv::Mat channel_swap(cv::Mat img){
// get height and width
int width = img.cols;
int height = img.rows;

cv::Mat out = img.clone();
// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);

for (int j=0; j<height; j++){
for (int i=0; i<width; i++){
unsigned char tmp = out.at<cv::Vec3b>(j, i)[0];
out.at<cv::Vec3b>(j, i)[0] = img.at<cv::Vec3b>(j,i)[2];
out.at<cv::Vec3b>(j,i)[2] = tmp;
// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// R -> B
out.at<cv::Vec3b>(y, x)[0] = img.at<cv::Vec3b>(y, x)[2];
// B -> R
out.at<cv::Vec3b>(y, x)[2] = img.at<cv::Vec3b>(y, x)[0];
// G -> G
out.at<cv::Vec3b>(y, x)[1] = img.at<cv::Vec3b>(y, x)[1];
}
}

return out;
}


int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

// channel swap
cv::Mat out = channel_swap(img);

//cv::imwrite("out.jpg", out);
cv::imshow("sample", out);
cv::waitKey(0);
cv::destroyAllWindows();

return 0;

}
71 changes: 42 additions & 29 deletions Question_01_10/answers_cpp/answer_10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,58 @@
#include <iostream>
#include <math.h>

int main(int argc, const char* argv[]){
cv::Mat img = cv::imread("imori_noise.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;

// median filter
cv::Mat median_filter(cv::Mat img, int kernel_size){
int height = img.rows;
int width = img.cols;
int channel = img.channels();

// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
int k_size = 3;
int p = floor(k_size / 2);

// prepare kernel
int pad = floor(kernel_size / 2);

// filtering
double v = 0;
int vs[k_size * k_size];
int vs[kernel_size * kernel_size];
int count = 0;

for (int j = 0; j < height; j++){
for (int i = 0; i < width; i++){
for (int c = 0; c < 3; c++){
v = 0;
count = 0;

for (int i = 0; i < k_size * k_size; i++){
vs[i] = 999;
}

for (int _j = -p; _j < p+1; _j++){
for (int _i = -p; _i < p+1; _i++){
if (((j+_j) >= 0) && ((i+_i) >= 0)){
vs[count++] = (int)img.at<cv::Vec3b>(j+_j, i+_i)[c];
}
}
}

std::sort(vs, vs+(k_size * k_size));
out.at<cv::Vec3b>(j,i)[c] = (uchar)vs[int(floor(count / 2)) + 1];
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
for (int c = 0; c < channel; c++){
v = 0;
count = 0;

for (int i = 0; i < kernel_size * kernel_size; i++){
vs[i] = 999;
}

// get neighbor pixels
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((y + dy) >= 0) && ((x + dx) >= 0)){
vs[count++] = (int)img.at<cv::Vec3b>(y + dy, x + dx)[c];
}
}
}

// get and assign median
std::sort(vs, vs + (kernel_size * kernel_size));
out.at<cv::Vec3b>(y, x)[c] = (uchar)vs[int(floor(count / 2)) + 1];
}
}
}
return out;
}

int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori_noise.jpg", cv::IMREAD_COLOR);

// median filter
cv::Mat out = median_filter(img, 3);

//cv::imwrite("out.jpg", out);
cv::imshow("answer", out);
Expand Down
40 changes: 27 additions & 13 deletions Question_01_10/answers_cpp/answer_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@
#include <opencv2/highgui.hpp>
#include <iostream>

int main(int argc, const char* argv[]){
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// BGR -> Gray
cv::Mat BGR2GRAY(cv::Mat img){
// get height and width
int width = img.cols;
int height = img.rows;

// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);

for (int j=0; j<height; j++){
for (int i=0; i<width; i++){
out.at<uchar>(j, i) = (int)((float)img.at<cv::Vec3b>(j,i)[0] * 0.0722 + \
(float)img.at<cv::Vec3b>(j,i)[1] * 0.7152 + \
(float)img.at<cv::Vec3b>(j,i)[2] * 0.2126);

// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// BGR -> Gray
out.at<uchar>(y, x) = 0.2126 * (float)img.at<cv::Vec3b>(y, x)[2] \
+ 0.7152 * (float)img.at<cv::Vec3b>(y, x)[1] \
+ 0.0722 * (float)img.at<cv::Vec3b>(y, x)[0];
}
}


return out;
}


int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

// BGR -> Gray
cv::Mat out = BGR2GRAY(img);

//cv::imwrite("out.jpg", out);
cv::imshow("answer", out);
cv::imshow("sample", out);
cv::waitKey(0);
cv::destroyAllWindows();

return 0;

}
69 changes: 51 additions & 18 deletions Question_01_10/answers_cpp/answer_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,68 @@
#include <opencv2/highgui.hpp>
#include <iostream>

int main(int argc, const char* argv[]){
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// BGR -> Gray
cv::Mat BGR2GRAY(cv::Mat img){
// get height and width
int width = img.cols;
int height = img.rows;

// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);

// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// BGR -> Gray
out.at<uchar>(y, x) = 0.2126 * (float)img.at<cv::Vec3b>(y, x)[2] \
+ 0.7152 * (float)img.at<cv::Vec3b>(y, x)[1] \
+ 0.0722 * (float)img.at<cv::Vec3b>(y, x)[0];
}
}

int th = 128;
return out;
}

// Gray -> Binary
cv::Mat Binarize(cv::Mat gray, int th){
int width = gray.cols;
int height = gray.rows;

// prepare output
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);

for (int j=0; j<height; j++){
for (int i=0; i<width; i++){
uchar val = (int)((float)img.at<cv::Vec3b>(j,i)[0] * 0.0722 + \
(float)img.at<cv::Vec3b>(j,i)[1] * 0.7152 + \
(float)img.at<cv::Vec3b>(j,i)[2] * 0.2126);
if (val < th){
val = 0;

// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// Binarize
if (gray.at<uchar>(y, x) > th){
out.at<uchar>(y, x) = 255;
} else {
val = 255;
out.at<uchar>(y, x) = 0;
}
out.at<uchar>(j,i) = val;

}
}


return out;
}


int main(int argc, const char* argv[]){
// read image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

// BGR -> Gray
cv::Mat gray = BGR2GRAY(img);

// Gray -> Binary
cv::Mat out = Binarize(gray, 128);

//cv::imwrite("out.jpg", out);
cv::imshow("answer", out);
cv::imshow("sample", out);
cv::waitKey(0);
cv::destroyAllWindows();

return 0;

}
Loading

0 comments on commit c0579e6

Please sign in to comment.