Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyoyo-yo committed Jul 15, 2019
1 parent 75c1587 commit 658c74b
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Question_21_30/answers/answer_21.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
# Trans [0, 255]
a, b = 0., 255.

vmin = img.min()
vmax = img.max()
c = img.min()
d = img.max()

out = img.copy()
out[out<a] = a
out[out>b] = b
out = (b-a) / (vmax - vmin) * (out - vmin) + a

out = (b-a) / (d - c) * (out - c) + a
out[out < a] = a
out[out > b] = b
out = out.astype(np.uint8)

# Display histogram
Expand Down
41 changes: 41 additions & 0 deletions Question_21_30/answers_cpp/answer_25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>

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

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

double rx = 1.5, ry = 1.5;

int resized_width = (int)(width * rx);
int resized_height = (int)(height * ry);
int x_before, y_before;

cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3);

// nearest neighbor interpolation
for (int y = 0; y < resized_height; y++){
y_before = (int)round(y / ry);
y_before = fmin(y_before, height - 1);

for (int x = 0; x < resized_width; x++){
x_before = (int)round(x / rx);
x_before = fmin(x_before, width - 1);

for (int k = 0; k < 3; k++){
out.at<cv::Vec3b>(y, x)[k] = img.at<cv::Vec3b>(y_before, x_before)[k];
}
}
}

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

return 0;
}
49 changes: 49 additions & 0 deletions Question_21_30/answers_cpp/answer_26.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>

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

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

double rx = 1.5, ry = 1.5;

int resized_width = (int)(width * rx);
int resized_height = (int)(height * ry);
int x_before, y_before;
double dx, dy;
double val;

cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3);

// bi-linear interpolation
for (int y = 0; y < resized_height; y++){
y_before = (int)floor(y / ry);
y_before = fmin(y_before, height - 1);
dy = y / ry - y_before;

for (int x = 0; x < resized_width; x++){
x_before = (int)floor(x / rx);
x_before = fmin(x_before, width - 1);
dx = x / rx - x_before;

for (int k = 0; k < 3; k++){
val = (1. - dx) * (1. - dy) * img.at<cv::Vec3b>(y_before, x_before)[k] +
dx * (1. - dy) * img.at<cv::Vec3b>(y_before, x_before + 1)[k] +
(1. - dx) * dy * img.at<cv::Vec3b>(y_before + 1, x_before)[k] +
dx * dy * img.at<cv::Vec3b>(y_before + 1, x_before)[k];
out.at<cv::Vec3b>(y, x)[k] = (uchar)val;
}
}
}

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

return 0;
}
79 changes: 79 additions & 0 deletions Question_21_30/answers_cpp/answer_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>

double h(double t){
double a = -1;
if (fabs(t) <= 1){
return (a + 2) * pow(fabs(t), 3) - (a + 3) * pow(fabs(t), 2) + 1;
} else if(fabs(t) <= 2){
return a * pow(fabs(t), 3) - 5 * a * pow(fabs(t), 2) + 8 * a * fabs(t) - 4 * a;
}
return 0;
}

int val_clip(int x, int min, int max){
return fmin(fmax(x, min), max);
}


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

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

double rx = 1.5, ry = 1.5;

int resized_width = (int)(width * rx);
int resized_height = (int)(height * ry);
int x_before, y_before;
double dx, dy, wx, wy, w_sum;
double val;
int _x, _y;

cv::Mat out = cv::Mat::zeros(resized_height, resized_width, CV_8UC3);

// bi-cubic interpolation
for (int y = 0; y < resized_height; y++){
dy = y / ry;
y_before = (int)floor(dy);

for (int x = 0; x < resized_width; x++){
dx = x / rx;
x_before = (int)floor(dx);


for (int k = 0; k < 3; k++){
w_sum = 0;
val = 0;

for (int j = -1; j < 3; j++){
_y = val_clip(y_before + j, 0, height - 1);
wy = h(fabs(dy - _y));

for (int i = -1; i < 3; i++){
_x = val_clip(x_before + i, 0, width - 1);
wx = h(fabs(dx - _x));
w_sum += wy * wx;
val += (double)img.at<cv::Vec3b>(_y, _x)[k] * wx * wy;
}
}

val /= w_sum;

val = val_clip(val, 0, 255);
out.at<cv::Vec3b>(y, x)[k] = (uchar)val;
}
}
}

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

return 0;
}

63 changes: 63 additions & 0 deletions Tutorial/sample_ex.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Image Processing</title>

</head>

<body>
<h1>Image Processing!</h1>

<canvas id="canvas" style="border: solid thin black;"></canvas>
<canvas id="canvas_result" style="border: solid thin black;"></canvas>

<br>
<button onclick="process();" style="margin-left: 90px; font-size: 20px;">process!</button>

<script type="text/javascript">
let image = new Image();
var src = "imori.jpg";
image.src = src;
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const canvas_result = document.getElementById("canvas_result");
const ctx_result = canvas_result.getContext("2d");
image.onload = () => {
canvas.width = image.width;
canvas.height = image.height;

ctx.drawImage(image, 0, 0, canvas.width, canvas.height);

canvas_result.width = image.width;
canvas_result.height = image.height;
}

function process(){

let src = ctx.getImageData(0, 0, image.width, image.height);
let dst = ctx_result.createImageData(image.width, image.height);

//for ( var i = 0; i < 128; i++){ src.data[i * 4] = 255;}

/*
src.data[29 * 4 + 19 * 128 * 4] = 255;
src.data[29 * 4 + 19 * 128 * 4 + 1] = 0;
src.data[29 * 4 + 19 * 128 * 4 + 2] = 0;
//src.data[29 * 4 + 19 * 128 + 3] = 0;
*/

for (let i = 0; i < src.data.length; i+= 4){
dst.data[i] = src.data[i];
dst.data[i+1] = src.data[i+1];
dst.data[i+2] = src.data[i+2];
dst.data[i+3] = src.data[i+3];
}

ctx_result.putImageData(dst, 0, 0);
}

</script>

</body>
</html>

0 comments on commit 658c74b

Please sign in to comment.