forked from pjreddie/vision-hw4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_image.c
180 lines (159 loc) · 4.05 KB
/
load_image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// You probably don't want to edit this file
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
image make_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = calloc(h*w*c, sizeof(float));
return out;
}
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
void save_image_stb(image im, const char *name, int png)
{
char buff[256];
unsigned char *data = calloc(im.w*im.h*im.c, sizeof(char));
int i,k;
for(k = 0; k < im.c; ++k){
for(i = 0; i < im.w*im.h; ++i){
data[i*im.c+k] = (unsigned char) roundf((255*im.data[i + k*im.w*im.h]));
}
}
int success = 0;
if(png){
sprintf(buff, "%s.png", name);
success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c);
} else {
sprintf(buff, "%s.jpg", name);
success = stbi_write_jpg(buff, im.w, im.h, im.c, data, 100);
}
free(data);
if(!success) fprintf(stderr, "Failed to write image %s\n", buff);
}
void save_png(image im, const char *name)
{
save_image_stb(im, name, 1);
}
void save_image(image im, const char *name)
{
save_image_stb(im, name, 0);
}
//
// Load an image using stb
// channels = [0..4]
// channels > 0 forces the image to have that many channels
//
image load_image_stb(char *filename, int channels)
{
int w, h, c;
unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
if (!data) {
fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n",
filename, stbi_failure_reason());
exit(0);
}
if (channels) c = channels;
int i,j,k;
image im = make_image(w, h, c);
for(k = 0; k < c; ++k){
for(j = 0; j < h; ++j){
for(i = 0; i < w; ++i){
int dst_index = i + w*j + w*h*k;
int src_index = k + c*i + c*w*j;
im.data[dst_index] = (float)data[src_index]/255.;
}
}
}
//We don't like alpha channels, #YOLO
if(im.c == 4) im.c = 3;
free(data);
return im;
}
image load_image(char *filename)
{
image out = load_image_stb(filename, 0);
return out;
}
void free_image(image im)
{
free(im.data);
}
#ifdef OPENCV
void rgbgr_image(image im)
{
int i;
for(i = 0; i < im.w*im.h; ++i){
float swap = im.data[i];
im.data[i] = im.data[i+im.w*im.h*2];
im.data[i+im.w*im.h*2] = swap;
}
}
void ipl_into_image(IplImage* src, image im)
{
unsigned char *data = (unsigned char *)src->imageData;
int h = src->height;
int w = src->width;
int c = src->nChannels;
int step = src->widthStep;
int i, j, k;
for(i = 0; i < h; ++i){
for(k= 0; k < c; ++k){
for(j = 0; j < w; ++j){
im.data[k*w*h + i*w + j] = data[i*step + j*c + k]/255.;
}
}
}
}
image ipl_to_image(IplImage* src)
{
int h = src->height;
int w = src->width;
int c = src->nChannels;
image out = make_image(w, h, c);
ipl_into_image(src, out);
return out;
}
image get_image_from_stream(CvCapture *cap)
{
IplImage* src = cvQueryFrame(cap);
if (!src) return make_empty_image(0,0,0);
image im = ipl_to_image(src);
rgbgr_image(im);
return im;
}
int show_image(image im, const char *name, int ms)
{
image p = copy_image(im);
IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
clamp_image(p);
int x,y,k;
if(p.c == 3) rgbgr_image(p);
char buff[256];
sprintf(buff, "%s", name);
int step = disp->widthStep;
cvNamedWindow(buff, CV_WINDOW_NORMAL);
for(y = 0; y < p.h; ++y){
for(x = 0; x < p.w; ++x){
for(k= 0; k < p.c; ++k){
disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(p,x,y,k)*255);
}
}
}
cvShowImage(buff, disp);
free_image(p);
cvReleaseImage(&disp);
return cvWaitKey(ms);
}
#endif