forked from QSCTech/zju-icicles
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
104,786 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include"func.h" | ||
|
||
// 卷积 | ||
void conv(NEWDATA *y,NEWDATA **input,float k,int type,int r){ | ||
int i,j,m,n; | ||
int temp; | ||
// for input | ||
switch(type){ | ||
// 此处针对不同的卷积进行优化 | ||
case mean_type: | ||
i = r;{ | ||
for(j=0;j<(COL);j++){ | ||
// convolution: f*g*k | ||
temp = input[i][j] + input[i][j+1] + input[i][j+2] | ||
+ input[i+1][j] + input[i+1][j+1] + input[i+1][j+2] | ||
+ input[i+2][j] + input[i+2][j+1] + input[i+2][j+2]; | ||
temp = temp * k / 9.0; | ||
y[j] += temp; | ||
} | ||
} | ||
break; | ||
|
||
case guassian_type: | ||
i = r;{ | ||
for(j=0;j<(COL);j++){ | ||
// convolution: f*g*k | ||
temp = input[i][j] + input[i][j+1]*2 + input[i][j+2] | ||
+ input[i+1][j]*2 + input[i+1][j+1]*4 + input[i+1][j+2]*2 | ||
+ input[i+2][j] + input[i+2][j+1]*2 + input[i+2][j+2]; | ||
temp = (temp >> 4) * k; | ||
y[j] += temp; | ||
} | ||
} | ||
break; | ||
|
||
case highpass_type: | ||
i = r;{ | ||
for(j=0;j<(COL);j++){ | ||
// convolution: f*g*k | ||
temp = input[i][j] + input[i][j+1] + input[i][j+2] | ||
+ input[i+1][j] + input[i+1][j+2] | ||
+ input[i+2][j] + input[i+2][j+1] + input[i+2][j+2]; | ||
// temp = input[i+1][j+1] * 9 - temp; | ||
temp = (input[i+1][j+1] << 3) + input[i+1][j+1] - temp; | ||
temp = temp * k ; | ||
y[j] += temp; | ||
} | ||
} | ||
break; | ||
|
||
case laplacian_type: | ||
i = r;{ | ||
for(j=0;j<(COL);j++){ | ||
// convolution: f*g*k | ||
temp = input[i][j] + input[i][j+1] + input[i][j+2] | ||
+ input[i+1][j] + input[i+1][j+2] | ||
+ input[i+2][j] + input[i+2][j+1] + input[i+2][j+2]; | ||
// temp = temp - input[i+1][j+1]*8; | ||
temp = temp - (input[i+1][j+1] << 3); | ||
temp = temp * k ; | ||
y[j] += temp; | ||
} | ||
} | ||
break; | ||
|
||
default: | ||
// for(i=0;i<(ROW);i++){ | ||
// for(j=0;j<(COL);j++){ | ||
// // convolution: f*g*k | ||
// for(m=0;m<3;m++){ | ||
// for(n=0;n<3;n++){ | ||
// y[i][j] += input[i+m][j+n] * kernel[2-m][2-n] * k; | ||
// } | ||
// } | ||
// } | ||
// } | ||
break; | ||
} | ||
} | ||
|
||
// 计算 y = k1 f_i * g_r + k2 f_i * g_g + k3 f_i * g_b | ||
void calcul_y(NEWDATA *y, int type,NEWDATA ***input,float k1,float k2,float k3,FILE *out_y,int r){ | ||
clear_y(y); | ||
|
||
// k1,R | ||
conv(y,input[0],k1,type,r); | ||
// k2,G | ||
conv(y,input[1],k2,type,r); | ||
// k3,B | ||
conv(y,input[2],k3,type,r); | ||
|
||
// check (< 0 & > 255) | ||
check_y(y); | ||
|
||
// write 直接将计算结果写入文件中 | ||
fwrite(y,sizeof(NEWDATA),COL,out_y); | ||
} | ||
|
||
// 从cmd读取小数 | ||
float read_float_number(char *a){ | ||
int i; | ||
float k = 0.0; | ||
float v = 1.0; | ||
|
||
for(i = 2;i < strlen(a);i++){ | ||
v = v * 0.1; | ||
k = k + v * (int)(a[i] - '0'); | ||
} | ||
return k; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 图像大小 | ||
#define ROW 1024// h | ||
#define COL 2048// w | ||
|
||
// 四种卷积核 | ||
#define mean_type 0 | ||
#define guassian_type 1 | ||
#define highpass_type 2 | ||
#define laplacian_type 3 | ||
|
||
// 16-bit | ||
#define NEWDATA short | ||
|
||
// 宏定义函数 | ||
#define clear_y(y) {int j;for(j=0;j<COL;j++) y[j] = 0;} | ||
#define check_y(y) {int j;for(j=0;j<COL;j++) {if(y[j]<0) y[j] = 0; if(y[j]>255) y[j] = 255;y[j] = (NEWDATA)(y[j]);} } | ||
|
||
// 读取k1,k2,k3 | ||
float read_float_number(char *a); | ||
|
||
// 卷积有关的函数 | ||
void conv(NEWDATA *y,NEWDATA **input,float k,int type,int r); | ||
void calcul_y(NEWDATA *y, int type,NEWDATA ***input,float k1,float k2,float k3,FILE *out_y,int r); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<string.h> | ||
#include"func.h" | ||
#include"func.c" | ||
|
||
int main(int argc, char *argv[]){ | ||
if(argc != 6) | ||
{ | ||
fprintf(stderr, "Usage: ./conv in_bin_file out_bin_file k1 k2 k3\n"); | ||
return 1; | ||
} | ||
|
||
|
||
// @Part.1 weight | ||
// 读入k1, k2, k3的值 | ||
fprintf(stderr,"------------------Part.1 Weight------------------\n"); | ||
float k1 = read_float_number(argv[3]); | ||
float k2 = read_float_number(argv[4]); | ||
float k3 = read_float_number(argv[5]); | ||
fprintf(stderr,"k1 = %.5f, k2 = %.5f, k3 = %.5f\n",k1,k2,k3); | ||
while( (k1 + k2 + k3 - 1 > 0.001) || (1 - k1 - k2 - k3 > 0.001) ){ // test if k1+k2+k3=1 | ||
fprintf(stderr,"\nSorry, k1+k2+k3==%.5f != 1\nPlease input the weight k1, k2 and k3 again:\n",k1+k2+k3); | ||
scanf("%f %f %f",&k1,&k2,&k3); | ||
fprintf(stderr,"k1 = %.5f, k2 = %.5f, k3 = %.5f\n",k1,k2,k3); | ||
} | ||
|
||
|
||
// @Part.2 input | ||
// 读入图像的rgb灰度值 | ||
fprintf(stderr,"\n------------------Part.2 Input-------------------\n"); | ||
fprintf(stderr, "Reading RGB data from image binary file...\n"); | ||
|
||
FILE *infile; | ||
infile = fopen(argv[1],"rb"); | ||
int w,h; | ||
fread(&w,sizeof(int),1,infile); | ||
fread(&h,sizeof(int),1,infile); | ||
fprintf(stderr,"The image's size is %d * %d\n", w,h); | ||
|
||
NEWDATA ***input; | ||
input = (NEWDATA***)malloc(3 * sizeof(NEWDATA**)); | ||
|
||
int i,j; | ||
for(i=0;i<3;i++){ | ||
input[i] = (NEWDATA**)malloc((ROW+2) * sizeof(NEWDATA*)); | ||
for(j=0;j<(ROW+2);j++){ | ||
input[i][j] = (NEWDATA*)malloc((COL+2) * sizeof(NEWDATA)); | ||
} | ||
} | ||
|
||
// 初始化为0 | ||
for(i=0;i<3;i++){ | ||
for(j=0;j<(COL+2);j++){ | ||
input[i][0][j] = 0; | ||
input[i][ROW+1][j] = 0; | ||
} | ||
for(j=0;j<(ROW+2);j++){ | ||
input[i][j][0] = 0; | ||
input[i][j][COL+1] = 0; | ||
} | ||
} | ||
|
||
// 读取数据 | ||
for(j=0;j<3;j++){ | ||
for(i=1;i<(ROW+1);i++){ | ||
fread(input[j][i]+sizeof(NEWDATA),sizeof(NEWDATA),COL,infile); | ||
} | ||
} | ||
fclose(infile); | ||
|
||
|
||
// @Part.3 output | ||
// 进行卷积的计算和结果的写入 | ||
fprintf(stderr,"\n------------------Part.3 Output------------------\n"); | ||
fprintf(stderr, "Calculating the convolution and saving the value to binary file...\n"); | ||
|
||
NEWDATA *y; //ROW y | ||
y = (NEWDATA*)malloc((COL) * sizeof(NEWDATA)); | ||
|
||
FILE *out_y; | ||
out_y = fopen(argv[2],"wb"); | ||
|
||
w = COL; | ||
h = ROW; | ||
// printf("%d\n",w); | ||
// printf("%d\n",h); | ||
fwrite(&w,sizeof(int),1,out_y); | ||
fwrite(&h,sizeof(int),1,out_y); | ||
|
||
fprintf(stderr,"-----------------------------------Calculating y1\n"); | ||
// mean_filter | ||
for(i=0;i<ROW;i++){ | ||
calcul_y(y,mean_type,input,k1,k2,k3,out_y,i); | ||
} | ||
|
||
fprintf(stderr,"-----------------------------------Calculating y2\n"); | ||
// guassian_filter | ||
for(i=0;i<ROW;i++){ | ||
calcul_y(y,guassian_type,input,k1,k2,k3,out_y,i); | ||
} | ||
|
||
fprintf(stderr,"-----------------------------------Calculating y3\n"); | ||
// highpass_filter | ||
for(i=0;i<ROW;i++){ | ||
calcul_y(y,highpass_type,input,k1,k2,k3,out_y,i); | ||
} | ||
|
||
fprintf(stderr,"-----------------------------------Calculating y4\n"); | ||
// laplacian_filter | ||
for(i=0;i<ROW;i++){ | ||
calcul_y(y,laplacian_type,input,k1,k2,k3,out_y,i); | ||
} | ||
|
||
fclose(out_y); | ||
fprintf(stderr,"\nThe run time of Section.2 is:"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import sys | ||
from PIL import Image | ||
import struct | ||
|
||
if len(sys.argv)!=2: | ||
print("Usage: python read_bin.py in_bin_file") | ||
|
||
infile = open(sys.argv[1],'rb') | ||
# infile = open('y.bin','rb') | ||
w = int(struct.unpack("i",infile.read(4))[0]) | ||
h = int(struct.unpack("i",infile.read(4))[0]) | ||
print("The picture\'s size is:") | ||
print(w,h) | ||
|
||
# short | ||
str = 'h' | ||
num = 2 | ||
|
||
pixel_r = (struct.unpack(str*h*w,infile.read(num*h*w))) | ||
pixel_g = (struct.unpack(str*h*w,infile.read(num*h*w))) | ||
pixel_b = (struct.unpack(str*h*w,infile.read(num*h*w))) | ||
pixel_a = (struct.unpack(str*h*w,infile.read(num*h*w))) | ||
|
||
infile.close() | ||
|
||
print("Putting pixels to new images...") | ||
cnt = 0 | ||
new_img_r = Image.new('L',(w,h)) | ||
new_img_g = Image.new('L',(w,h)) | ||
new_img_b = Image.new('L',(w,h)) | ||
new_img_a = Image.new('L',(w,h)) | ||
new_img_rgb = Image.new('RGB',(w,h)) | ||
new_img_0 = Image.new('RGBA',(w,h)) | ||
|
||
img_r = new_img_r.load() | ||
img_g = new_img_g.load() | ||
img_b = new_img_b.load() | ||
img_a = new_img_a.load() | ||
img_0 = new_img_0.load() | ||
img_rgb = new_img_rgb.load() | ||
|
||
for i in range(h): | ||
for j in range(w): | ||
temp_r = int(pixel_r[cnt]) | ||
temp_g = int(pixel_g[cnt]) | ||
temp_b = int(pixel_b[cnt]) | ||
temp_a = int(pixel_a[cnt]) | ||
|
||
img_r[j,i] = (temp_r) | ||
img_g[j,i] = (temp_g) | ||
img_b[j,i] = (temp_b) | ||
img_a[j,i] = (temp_a) | ||
img_rgb[j,i] = (temp_r,temp_g,temp_b) | ||
img_0[j,i] = (temp_r,temp_g,temp_b,temp_a) | ||
cnt+=1 | ||
|
||
|
||
print("Creating png files...") | ||
new_img_r.save("output/out_image_r.png","png") | ||
new_img_g.save("output/out_image_g.png","png") | ||
new_img_b.save("output/out_image_b.png","png") | ||
new_img_a.save("output/out_image_a.png","png") | ||
new_img_rgb.save("output/out_image_rgb.png","png") | ||
new_img_0.save("output/out_im_rgba.png","png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#coding=utf-8 | ||
''' | ||
author: Zewen-Ye | ||
time: 2019-04-29 | ||
function: this code is used to read an image and save RGB data to outfile | ||
''' | ||
|
||
import sys | ||
import struct | ||
from PIL import Image | ||
|
||
if len(sys.argv) != 3: | ||
print("Usage: python read_image.py image_file outfile") | ||
exit() | ||
|
||
img = Image.open(sys.argv[1]) | ||
print("The input image mode is: ") # RGBA | ||
print(img.mode) | ||
print("The input image size is: ") # 2048,1024 | ||
w,h = img.size | ||
print(w,h) | ||
|
||
# out to file | ||
outfile = open(sys.argv[2],'wb') | ||
outfile.write(struct.pack('i',w)) | ||
outfile.write(struct.pack('i',h)) | ||
|
||
# new_img = Image.new(mode="RGB", size=(w, h)) | ||
temp = img.load() | ||
|
||
|
||
str = 'h' | ||
for i in range(h):# 1024 | ||
for j in range(w):# 2048 | ||
r,_,_,_ = temp[j,i] | ||
outfile.write(struct.pack(str,r)) | ||
|
||
for i in range(h):# 1024 | ||
for j in range(w):# 2048 | ||
_,g,_,_ = temp[j,i] | ||
outfile.write(struct.pack(str,g)) | ||
|
||
for i in range(h):# 1024 | ||
for j in range(w):# 2048 | ||
_,_,b,_ = temp[j,i] | ||
outfile.write(struct.pack(str,b)) | ||
|
||
img.close() | ||
outfile.close() |
Oops, something went wrong.