Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Oct 14, 2019
1 parent 9d7d235 commit b669096
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 175 deletions.
57 changes: 32 additions & 25 deletions Question_21_30/answers_cpp/answer_21.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,65 @@
#include <iostream>
#include <math.h>

int main(int argc, const char* argv[]){

// read original image
cv::Mat img = cv::imread("imori_dark.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// histogram normalization
cv::Mat histogram_normalization(cv::Mat img, int a, int b){
// get height and width
int width = img.cols;
int height = img.rows;
int channel = img.channels();

// histogram transformation hyper-parameters
double a = 0.;
double b = 255.;
double c = 256., d = 0.;
double val;
int c, d;
int val;

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


// get [c, d]
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
for (int k = 0; k < 3; k++){
val = (float)img.at<cv::Vec3b>(j, i)[k];
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
for (int _c = 0; _c < channel; _c++){
val = (float)img.at<cv::Vec3b>(y, x)[_c];
c = fmin(c, val);
d = fmax(d, val);
}
}
}

// histogram transformation
for (int i = 0; i < height; i++){
for ( int j = 0; j < width; j++){
for ( int k = 0; k < 3; k++){
val = img.at<cv::Vec3b>(j, i)[k];
for (int y = 0; y < height; y++){
for ( int x = 0; x < width; x++){
for ( int _c = 0; _c < 3; _c++){
val = img.at<cv::Vec3b>(y, x)[_c];

if (val < a){
out.at<cv::Vec3b>(j, i)[k] = (uchar)a;
out.at<cv::Vec3b>(y, x)[_c] = (uchar)a;
}
else if (val <= b){
out.at<cv::Vec3b>(j, i)[k] = (uchar)((b - a) / (d - c) * (val - c) + a);
out.at<cv::Vec3b>(y, x)[_c] = (uchar)((b - a) / (d - c) * (val - c) + a);
}
else {
out.at<cv::Vec3b>(j, i)[k] = (uchar)b;
out.at<cv::Vec3b>(y, x)[_c] = (uchar)b;
}
}
}
}

return out;
}


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

// histogram normalization
cv::Mat out = histogram_normalization(img, 0, 255);

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

return 0;
}
48 changes: 29 additions & 19 deletions Question_21_30/answers_cpp/answer_22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
#include <iostream>
#include <math.h>

int main(int argc, const char* argv[]){

// read original image
cv::Mat img = cv::imread("imori_dark.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// histogram transform
cv::Mat histogram_transform(cv::Mat img, int m0, int s0){
// get height and width
int width = img.cols;
int height = img.rows;
int channel = img.channels();

// histogram transformation hyper-parameters
double m0 = 128, s0 = 52;
double m, s;
double sum = 0., squared_sum = 0.;
double val;
Expand All @@ -21,36 +20,47 @@ int main(int argc, const char* argv[]){
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);

// get sum
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
for (int k = 0; k < 3; k++){
val = (float)img.at<cv::Vec3b>(j, i)[k];
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
for (int c = 0; c < 3; c++){
val = (float)img.at<cv::Vec3b>(y, x)[c];
sum += val;
squared_sum += (val * val);
}
}
}

// get standard deviation
m = sum / (height * width * 3);
s = sqrt(squared_sum / (height * width * 3) - m * m);
m = sum / (height * width * channel);
s = sqrt(squared_sum / (height * width * channel) - m * m);


// histogram transformation
for (int i = 0; i < height; i++){
for ( int j = 0; j < width; j++){
for ( int k = 0; k < 3; k++){
val = img.at<cv::Vec3b>(j, i)[k];
for (int y = 0; y < height; y++){
for ( int x = 0; x < width; x++){
for ( int c = 0; c < 3; c++){
val = img.at<cv::Vec3b>(y, x)[c];

out.at<cv::Vec3b>(j, i)[k] = (uchar)(s0 / s * (val - m) + m0);
out.at<cv::Vec3b>(y, x)[c] = (uchar)(s0 / s * (val - m) + m0);
}
}
}

return out;
}


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

// histogram transform
cv::Mat out = histogram_transform(img, 128, 52);

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

return 0;
}
53 changes: 35 additions & 18 deletions Question_21_30/answers_cpp/answer_23.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,74 @@
#include <iostream>
#include <math.h>

int main(int argc, const char* argv[]){

// read original image
cv::Mat img = cv::imread("imori_dark.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// histogram equalization
cv::Mat histogram_equalization(cv::Mat img){
// get height and width
int width = img.cols;
int height = img.rows;
int channel = img.channels();

// output image
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);

// histogram equalization hyper-parameters
double Zmax = 255;
double hist[255];
double S = height * width * 3;
double S = height * width * channel;
int val;
double hist_sum = 0;

// output image
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);

// histogram initialization
for (int i = 0; i < 255; i++){
hist[i] = 0;
}

// get histogram sum
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
for (int k = 0; k < 3; k++){
val = (int)img.at<cv::Vec3b>(j, i)[k];
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
for (int c = 0; c < channel; c++){
val = (int)img.at<cv::Vec3b>(y, x)[c];
hist[val] ++;
}
}
}

// histogram equalization
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
for (int k = 0; k < 3; k++){
val = (int)img.at<cv::Vec3b>(j, i)[k];
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
for (int c = 0; c < channel; c++){
val = (int)img.at<cv::Vec3b>(y, x)[c];

// get histogram sum <= current pixel value
hist_sum = 0;
for (int l = 0; l < val; l++){
hist_sum += hist[l];
}
out.at<cv::Vec3b>(j, i)[k] = (uchar)(Zmax / S * hist_sum);
// assign equalized value
out.at<cv::Vec3b>(y, x)[c] = (uchar)(Zmax / S * hist_sum);
}
}
}

return out;
}


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

// histogram equalization
cv::Mat out = histogram_equalization(img);

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

return 0;
}

42 changes: 25 additions & 17 deletions Question_21_30/answers_cpp/answer_24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,45 @@
#include <iostream>
#include <math.h>

int main(int argc, const char* argv[]){

// read original image
cv::Mat img = cv::imread("imori_gamma.jpg", cv::IMREAD_COLOR);

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

// gamma correction hyper-parameters
double c = 1;
double g = 2.2;
double val;
// gamma correction
cv::Mat gamma_correction(cv::Mat img, double gamma_c, double gamma_g){
// get height and width
int width = img.cols;
int height = img.rows;
int channel = img.channels();

// output image
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);

double val;

// gamma correction
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++){
for (int k = 0; k < 3; k++){
val = (double)img.at<cv::Vec3b>(j, i)[k] / 255;
for (int y = 0; y< height; y++){
for (int x = 0; x < width; x++){
for (int c = 0; c < channel; c++){
val = (double)img.at<cv::Vec3b>(y, x)[c] / 255;

out.at<cv::Vec3b>(j, i)[k] = (uchar)(pow(val / c, 1 / g) * 255);
out.at<cv::Vec3b>(y, x)[c] = (uchar)(pow(val / gamma_c, 1 / gamma_g) * 255);
}
}
}

return out;
}


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

// gamma correction
cv::Mat out = gamma_correction(img, 1, 2.2);

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

return 0;
}
32 changes: 21 additions & 11 deletions Question_21_30/answers_cpp/answer_25.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
#include <iostream>
#include <math.h>

int main(int argc, const char* argv[]){

// read original image
cv::Mat img = cv::imread("imori.jpg", cv::IMREAD_COLOR);

int width = img.rows;
int height = img.cols;
// nearest nieghbor
cv::Mat nearest_neighbor(cv::Mat img, double rx, double ry){
// get height and width
int width = img.cols;
int height = img.rows;
int channel = img.channels();

// resized ratio
double rx = 1.5, ry = 1.5;

// get resized shape
int resized_width = (int)(width * rx);
int resized_height = (int)(height * ry);
int x_before, y_before;
Expand All @@ -31,16 +30,27 @@ int main(int argc, const char* argv[]){
x_before = fmin(x_before, width - 1);

// assign pixel to new position
for (int k = 0; k < 3; k++){
out.at<cv::Vec3b>(y, x)[k] = img.at<cv::Vec3b>(y_before, x_before)[k];
for (int c = 0; c < channel; c++){
out.at<cv::Vec3b>(y, x)[c] = img.at<cv::Vec3b>(y_before, x_before)[c];
}
}
}

return out;
}


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

// nearest neighbor
cv::Mat out = nearest_neighbor(img, 1.5, 1.5);

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

return 0;
}
Loading

0 comments on commit b669096

Please sign in to comment.