yolov3输出检测图片位置信息
前言
我们在进行图片识别后需要进行进一步的处理,该文章会介绍:1.怎样取消lables;2.输出并保存(.txt)标记框的位置信息
一.去掉label
在darknet/src/image.c 收索draw_detections_v3 .在该函数对应目录下进行修改。
二.目标定位(Object localization)框的数据信息
以图片左上角为(0,0),以右下角为(1,1),这些数字均为位置或长度所在图片的比例大小。
下面的代码是实现提取上述数据并且保存到本地文件夹下:
#include "image.h"
#include "utils.h"
#include "blas.h"
#include "cuda.h"
#include <stdio.h>
#include <math.h> #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h" #ifdef OPENCV
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/types_c.h"
#include "opencv2/core/version.hpp"
#ifndef CV_VERSION_EPOCH
#include "opencv2/videoio/videoio_c.h"
#include "opencv2/imgcodecs/imgcodecs_c.h"
#include "http_stream.h"
#endif
#include "http_stream.h"
#endif int windows = ; float colors[][] = { {,,}, {,,},{,,},{,,},{,,},{,,} }; float get_color(int c, int x, int max)
{
float ratio = ((float)x/max)*;
int i = floor(ratio);
int j = ceil(ratio);
ratio -= i;
float r = (-ratio) * colors[i][c] + ratio*colors[j][c];
//printf("%f\n", r);
return r;
} static float get_pixel(image m, int x, int y, int c)
{
assert(x < m.w && y < m.h && c < m.c);
return m.data[c*m.h*m.w + y*m.w + x];
}
static float get_pixel_extend(image m, int x, int y, int c)
{
if (x < || x >= m.w || y < || y >= m.h) return ;
/*
if(x < 0) x = 0;
if(x >= m.w) x = m.w-1;
if(y < 0) y = 0;
if(y >= m.h) y = m.h-1;
*/
if (c < || c >= m.c) return ;
return get_pixel(m, x, y, c);
}
static void set_pixel(image m, int x, int y, int c, float val)
{
if (x < || y < || c < || x >= m.w || y >= m.h || c >= m.c) return;
assert(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] = val;
}
static void add_pixel(image m, int x, int y, int c, float val)
{
assert(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] += val;
} void composite_image(image source, image dest, int dx, int dy)
{
int x,y,k;
for(k = ; k < source.c; ++k){
for(y = ; y < source.h; ++y){
for(x = ; x < source.w; ++x){
float val = get_pixel(source, x, y, k);
float val2 = get_pixel_extend(dest, dx+x, dy+y, k);
set_pixel(dest, dx+x, dy+y, k, val * val2);
}
}
}
} image border_image(image a, int border)
{
image b = make_image(a.w + *border, a.h + *border, a.c);
int x,y,k;
for(k = ; k < b.c; ++k){
for(y = ; y < b.h; ++y){
for(x = ; x < b.w; ++x){
float val = get_pixel_extend(a, x - border, y - border, k);
if(x - border < || x - border >= a.w || y - border < || y - border >= a.h) val = ;
set_pixel(b, x, y, k, val);
}
}
}
return b;
} image tile_images(image a, image b, int dx)
{
if(a.w == ) return copy_image(b);
image c = make_image(a.w + b.w + dx, (a.h > b.h) ? a.h : b.h, (a.c > b.c) ? a.c : b.c);
fill_cpu(c.w*c.h*c.c, , c.data, );
embed_image(a, c, , );
composite_image(b, c, a.w + dx, );
return c;
} image get_label(image **characters, char *string, int size)
{
if(size > ) size = ;
image label = make_empty_image(,,);
while(*string){
image l = characters[size][(int)*string];
image n = tile_images(label, l, -size - + (size+)/);
free_image(label);
label = n;
++string;
}
image b = border_image(label, label.h*.);
free_image(label);
return b;
} image get_label_v3(image **characters, char *string, int size)
{
size = size / ;
if (size > ) size = ;
image label = make_empty_image(, , );
while (*string) {
image l = characters[size][(int)*string];
image n = tile_images(label, l, -size - + (size + ) / );
free_image(label);
label = n;
++string;
}
image b = border_image(label, label.h*.);
free_image(label);
return b;
} void draw_label(image a, int r, int c, image label, const float *rgb)
{
int w = label.w;
int h = label.h;
if (r - h >= ) r = r - h; int i, j, k;
for(j = ; j < h && j + r < a.h; ++j){
for(i = ; i < w && i + c < a.w; ++i){
for(k = ; k < label.c; ++k){
float val = get_pixel(label, i, j, k);
set_pixel(a, i+c, j+r, k, rgb[k] * val);
}
}
}
} void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
{
//normalize_image(a);
int i;
if(x1 < ) x1 = ;
if(x1 >= a.w) x1 = a.w-;
if(x2 < ) x2 = ;
if(x2 >= a.w) x2 = a.w-; if(y1 < ) y1 = ;
if(y1 >= a.h) y1 = a.h-;
if(y2 < ) y2 = ;
if(y2 >= a.h) y2 = a.h-; for(i = x1; i <= x2; ++i){
a.data[i + y1*a.w + *a.w*a.h] = r;
a.data[i + y2*a.w + *a.w*a.h] = r; a.data[i + y1*a.w + *a.w*a.h] = g;
a.data[i + y2*a.w + *a.w*a.h] = g; a.data[i + y1*a.w + *a.w*a.h] = b;
a.data[i + y2*a.w + *a.w*a.h] = b;
}
for(i = y1; i <= y2; ++i){
a.data[x1 + i*a.w + *a.w*a.h] = r;
a.data[x2 + i*a.w + *a.w*a.h] = r; a.data[x1 + i*a.w + *a.w*a.h] = g;
a.data[x2 + i*a.w + *a.w*a.h] = g; a.data[x1 + i*a.w + *a.w*a.h] = b;
a.data[x2 + i*a.w + *a.w*a.h] = b;
}
} void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b)
{
int i;
for(i = ; i < w; ++i){
draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
}
} void draw_bbox(image a, box bbox, int w, float r, float g, float b)
{
int left = (bbox.x-bbox.w/)*a.w;
int right = (bbox.x+bbox.w/)*a.w;
int top = (bbox.y-bbox.h/)*a.h;
int bot = (bbox.y+bbox.h/)*a.h; int i;
for(i = ; i < w; ++i){
draw_box(a, left+i, top+i, right-i, bot-i, r, g, b);
}
} image **load_alphabet()
{
int i, j;
const int nsize = ;
image **alphabets = calloc(nsize, sizeof(image));
for(j = ; j < nsize; ++j){
alphabets[j] = calloc(, sizeof(image));
for(i = ; i < ; ++i){
char buff[];
sprintf(buff, "data/labels/%d_%d.png", i, j);
alphabets[j][i] = load_image_color(buff, , );
}
}
return alphabets;
} void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
int i, j;
FILE * fp;
fp = fopen("BoxValue.txt", "w");
if(NULL == fp)
{
printf("error");
return;
} for (i = ; i < num; ++i) {
char labelstr[] = { };
int class_id = -;
for (j = ; j < classes; ++j) {
if (dets[i].prob[j] > thresh) {
if (class_id < ) {
strcat(labelstr, names[j]);
class_id = j;
}
else {
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
}
printf("%s: %.0f%%\n", names[j], dets[i].prob[j] * );
}
}
if (class_id >= ) {
int width = im.h * .;
if (width < )
width = ; /*
if(0){
width = pow(prob, 1./2.)*10+1;
alphabet = 0;
}
*/ //printf("%d %s: %.0f%%\n", i, names[class_id], prob*100);
int offset = class_id * % classes;
float red = get_color(, offset, classes);
float green = get_color(, offset, classes);
float blue = get_color(, offset, classes);
float rgb[]; //width = prob*20+2; rgb[] = red;
rgb[] = green;
rgb[] = blue;
box b = dets[i].bbox;
//printf("%f %f %f %f\n", b.x, b.y, b.w, b.h); int left = (b.x - b.w / .)*im.w;
int right = (b.x + b.w / .)*im.w;
int top = (b.y - b.h / .)*im.h;
int bot = (b.y + b.h / .)*im.h; if (left < ) left = ;
if (right > im.w - ) right = im.w - ;
if (top < ) top = ;
if (bot > im.h - ) bot = im.h - ; float b_x_center = (left + right) / ;
float b_y_center = (top + bot) / ;
float b_width = right - left;
float b_height = bot - top;
//sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height); draw_box_width(im, left, top, right, bot, width, red, green, blue);
//fprintf(fp,"%.1f %.1f %.1f %.1f %d\n",left, top, right, bot, width, height, class_id);
fprintf(fp,"%.2f %.2f %.2f %.2f %d\n",b_x_center, b_y_center, b_width, b_height, class_id);
if (alphabet) {
image label = get_label_v3(alphabet, labelstr, (im.h*.));
//draw_label(im, top + width, left, label, rgb);
free_image(label);
}
if (dets[i].mask) {
image mask = float_to_image(, , , dets[i].mask);
image resized_mask = resize_image(mask, b.w*im.w, b.h*im.h);
image tmask = threshold_image(resized_mask, .);
embed_image(tmask, im, left, top);
free_image(mask);
free_image(resized_mask);
free_image(tmask);
}
}
}
fclose(fp);
} void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
int i; for(i = ; i < num; ++i){
int class_id = max_index(probs[i], classes);
float prob = probs[i][class_id];
if(prob > thresh){ //// for comparison with OpenCV version of DNN Darknet Yolo v2
//printf("\n %f, %f, %f, %f, ", boxes[i].x, boxes[i].y, boxes[i].w, boxes[i].h);
// int k;
//for (k = 0; k < classes; ++k) {
// printf("%f, ", probs[i][k]);
//}
//printf("\n"); int width = im.h * .; if(){
width = pow(prob, ./.)*+;
alphabet = ;
} int offset = class_id* % classes;
float red = get_color(,offset,classes);
float green = get_color(,offset,classes);
float blue = get_color(,offset,classes);
float rgb[]; //width = prob*20+2; rgb[] = red;
rgb[] = green;
rgb[] = blue;
box b = boxes[i]; int left = (b.x-b.w/.)*im.w;
int right = (b.x+b.w/.)*im.w;
int top = (b.y-b.h/.)*im.h;
int bot = (b.y+b.h/.)*im.h; if(left < ) left = ;
if(right > im.w-) right = im.w-;
if(top < ) top = ;
if(bot > im.h-) bot = im.h-;
printf("%s: %.0f%%", names[class_id], prob * ); //printf(" - id: %d, x_center: %d, y_center: %d, width: %d, height: %d",
// class_id, (right + left) / 2, (bot - top) / 2, right - left, bot - top); printf("\n");
draw_box_width(im, left, top, right, bot, width, red, green, blue);
if (alphabet) {
image label = get_label(alphabet, names[class_id], (im.h*.)/);
draw_label(im, top + width, left, label, rgb);
}
}
}
} #ifdef OPENCV void draw_detections_cv_v3(IplImage* show_img, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
{
int i, j;
if (!show_img) return; for (i = ; i < num; ++i) {
char labelstr[] = { };
int class_id = -;
for (j = ; j < classes; ++j) {
if (dets[i].prob[j] > thresh) {
if (class_id < ) {
strcat(labelstr, names[j]);
class_id = j;
}
else {
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
}
printf("%s: %.0f%%\n", names[j], dets[i].prob[j] * );
}
}
if (class_id >= ) {
int width = show_img->height * .; /*
if(0){
width = pow(prob, 1./2.)*10+1;
alphabet = 0;
}
*/ //printf("%d %s: %.0f%%\n", i, names[class_id], prob*100);
int offset = class_id * % classes;
float red = get_color(, offset, classes);
float green = get_color(, offset, classes);
float blue = get_color(, offset, classes);
float rgb[]; //width = prob*20+2; rgb[] = red;
rgb[] = green;
rgb[] = blue;
box b = dets[i].bbox;
//printf("%f %f %f %f\n", b.x, b.y, b.w, b.h); int left = (b.x - b.w / .)*show_img->width;
int right = (b.x + b.w / .)*show_img->width;
int top = (b.y - b.h / .)*show_img->height;
int bot = (b.y + b.h / .)*show_img->height; if (left < ) left = ;
if (right > show_img->width - ) right = show_img->width - ;
if (top < ) top = ;
if (bot > show_img->height - ) bot = show_img->height - ; //int b_x_center = (left + right) / 2;
//int b_y_center = (top + bot) / 2;
//int b_width = right - left;
//int b_height = bot - top;
//sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height); float const font_size = show_img->height / .F;
CvPoint pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
pt1.x = left;
pt1.y = top;
pt2.x = right;
pt2.y = bot;
pt_text.x = left;
pt_text.y = top - ;
pt_text_bg1.x = left;
pt_text_bg1.y = top - ( + * font_size);
pt_text_bg2.x = right;
pt_text_bg2.y = top;
CvScalar color;
color.val[] = red * ;
color.val[] = green * ;
color.val[] = blue * ; cvRectangle(show_img, pt1, pt2, color, width, , );
//printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class_id, names[class_id]);
cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, , );
cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, , ); // filled
CvScalar black_color;
black_color.val[] = ;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, , font_size * , );
cvPutText(show_img, labelstr, pt_text, &font, black_color);
}
}
} void draw_detections_cv(IplImage* show_img, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
int i; for (i = ; i < num; ++i) {
int class_id = max_index(probs[i], classes);
float prob = probs[i][class_id];
if (prob > thresh) { int width = show_img->height * .; if () {
width = pow(prob, . / .) * + ;
alphabet = ;
} printf("%s: %.0f%%\n", names[class_id], prob * );
int offset = class_id * % classes;
float red = get_color(, offset, classes);
float green = get_color(, offset, classes);
float blue = get_color(, offset, classes);
float rgb[]; //width = prob*20+2; rgb[] = red;
rgb[] = green;
rgb[] = blue;
box b = boxes[i]; int left = (b.x - b.w / .)*show_img->width;
int right = (b.x + b.w / .)*show_img->width;
int top = (b.y - b.h / .)*show_img->height;
int bot = (b.y + b.h / .)*show_img->height; if (left < ) left = ;
if (right > show_img->width - ) right = show_img->width - ;
if (top < ) top = ;
if (bot > show_img->height - ) bot = show_img->height - ; float const font_size = show_img->height / .F;
CvPoint pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
pt1.x = left;
pt1.y = top;
pt2.x = right;
pt2.y = bot;
pt_text.x = left;
pt_text.y = top - ;
pt_text_bg1.x = left;
pt_text_bg1.y = top - (+*font_size);
pt_text_bg2.x = right;
pt_text_bg2.y = top;
CvScalar color;
color.val[] = red * ;
color.val[] = green * ;
color.val[] = blue * ; cvRectangle(show_img, pt1, pt2, color, width, , );
//printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class_id, names[class_id]);
cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, , );
cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, , ); // filled
CvScalar black_color;
black_color.val[] = ;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, , font_size * , );
cvPutText(show_img, names[class_id], pt_text, &font, black_color);
}
}
} IplImage* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size)
{
int img_offset = ;
int draw_size = img_size - img_offset;
IplImage* img = cvCreateImage(cvSize(img_size, img_size), , );
cvSet(img, CV_RGB(, , ), );
CvPoint pt1, pt2, pt_text;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, , , CV_AA);
char char_buff[];
int i;
// vertical lines
pt1.x = img_offset; pt2.x = img_size, pt_text.x = ;
for (i = ; i <= number_of_lines; ++i) {
pt1.y = pt2.y = (float)i * draw_size / number_of_lines;
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
if (i % == ) {
sprintf(char_buff, "%2.1f", max_img_loss*(number_of_lines - i) / number_of_lines);
pt_text.y = pt1.y + ;
cvPutText(img, char_buff, pt_text, &font, CV_RGB(, , ));
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
}
}
// horizontal lines
pt1.y = draw_size; pt2.y = , pt_text.y = draw_size + ;
for (i = ; i <= number_of_lines; ++i) {
pt1.x = pt2.x = img_offset + (float)i * draw_size / number_of_lines;
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
if (i % == ) {
sprintf(char_buff, "%d", max_batches * i / number_of_lines);
pt_text.x = pt1.x - ;
cvPutText(img, char_buff, pt_text, &font, CV_RGB(, , ));
cvLine(img, pt1, pt2, CV_RGB(, , ), , , );
}
}
cvPutText(img, "Iteration number", cvPoint(draw_size / , img_size - ), &font, CV_RGB(, , ));
cvPutText(img, "Press 's' to save: chart.jpg", cvPoint(, img_size - ), &font, CV_RGB(, , ));
printf(" If error occurs - run training with flag: -dont_show \n");
cvNamedWindow("average loss", CV_WINDOW_NORMAL);
cvMoveWindow("average loss", , );
cvResizeWindow("average loss", img_size, img_size);
cvShowImage("average loss", img);
cvWaitKey();
return img;
} void draw_train_loss(IplImage* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches)
{
int img_offset = ;
int draw_size = img_size - img_offset;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, , , CV_AA);
char char_buff[];
CvPoint pt1, pt2;
pt1.x = img_offset + draw_size * (float)current_batch / max_batches;
pt1.y = draw_size * ( - avg_loss / max_img_loss);
if (pt1.y < ) pt1.y = ;
cvCircle(img, pt1, , CV_RGB(, , ), CV_FILLED, , ); sprintf(char_buff, "current avg loss = %2.4f", avg_loss);
pt1.x = img_size / , pt1.y = ;
pt2.x = pt1.x + , pt2.y = pt1.y + ;
cvRectangle(img, pt1, pt2, CV_RGB(, , ), CV_FILLED, , );
pt1.y += ;
cvPutText(img, char_buff, pt1, &font, CV_RGB(, , ));
cvShowImage("average loss", img);
int k = cvWaitKey();
if (k == 's' || current_batch == (max_batches-)) cvSaveImage("chart.jpg", img, );
}
#endif // OPENCV void transpose_image(image im)
{
assert(im.w == im.h);
int n, m;
int c;
for(c = ; c < im.c; ++c){
for(n = ; n < im.w-; ++n){
for(m = n + ; m < im.w; ++m){
float swap = im.data[m + im.w*(n + im.h*c)];
im.data[m + im.w*(n + im.h*c)] = im.data[n + im.w*(m + im.h*c)];
im.data[n + im.w*(m + im.h*c)] = swap;
}
}
}
} void rotate_image_cw(image im, int times)
{
assert(im.w == im.h);
times = (times + ) % ;
int i, x, y, c;
int n = im.w;
for(i = ; i < times; ++i){
for(c = ; c < im.c; ++c){
for(x = ; x < n/; ++x){
for(y = ; y < (n-)/ + ; ++y){
float temp = im.data[y + im.w*(x + im.h*c)];
im.data[y + im.w*(x + im.h*c)] = im.data[n--x + im.w*(y + im.h*c)];
im.data[n--x + im.w*(y + im.h*c)] = im.data[n--y + im.w*(n--x + im.h*c)];
im.data[n--y + im.w*(n--x + im.h*c)] = im.data[x + im.w*(n--y + im.h*c)];
im.data[x + im.w*(n--y + im.h*c)] = temp;
}
}
}
}
} void flip_image(image a)
{
int i,j,k;
for(k = ; k < a.c; ++k){
for(i = ; i < a.h; ++i){
for(j = ; j < a.w/; ++j){
int index = j + a.w*(i + a.h*(k));
int flip = (a.w - j - ) + a.w*(i + a.h*(k));
float swap = a.data[flip];
a.data[flip] = a.data[index];
a.data[index] = swap;
}
}
}
} image image_distance(image a, image b)
{
int i,j;
image dist = make_image(a.w, a.h, );
for(i = ; i < a.c; ++i){
for(j = ; j < a.h*a.w; ++j){
dist.data[j] += pow(a.data[i*a.h*a.w+j]-b.data[i*a.h*a.w+j],);
}
}
for(j = ; j < a.h*a.w; ++j){
dist.data[j] = sqrt(dist.data[j]);
}
return dist;
} void embed_image(image source, image dest, int dx, int dy)
{
int x,y,k;
for(k = ; k < source.c; ++k){
for(y = ; y < source.h; ++y){
for(x = ; x < source.w; ++x){
float val = get_pixel(source, x,y,k);
set_pixel(dest, dx+x, dy+y, k, val);
}
}
}
} image collapse_image_layers(image source, int border)
{
int h = source.h;
h = (h+border)*source.c - border;
image dest = make_image(source.w, h, );
int i;
for(i = ; i < source.c; ++i){
image layer = get_image_layer(source, i);
int h_offset = i*(source.h+border);
embed_image(layer, dest, , h_offset);
free_image(layer);
}
return dest;
} void constrain_image(image im)
{
int i;
for(i = ; i < im.w*im.h*im.c; ++i){
if(im.data[i] < ) im.data[i] = ;
if(im.data[i] > ) im.data[i] = ;
}
} void normalize_image(image p)
{
int i;
float min = ;
float max = -; for(i = ; i < p.h*p.w*p.c; ++i){
float v = p.data[i];
if(v < min) min = v;
if(v > max) max = v;
}
if(max - min < .){
min = ;
max = ;
}
for(i = ; i < p.c*p.w*p.h; ++i){
p.data[i] = (p.data[i] - min)/(max-min);
}
} void normalize_image2(image p)
{
float *min = calloc(p.c, sizeof(float));
float *max = calloc(p.c, sizeof(float));
int i,j;
for(i = ; i < p.c; ++i) min[i] = max[i] = p.data[i*p.h*p.w]; for(j = ; j < p.c; ++j){
for(i = ; i < p.h*p.w; ++i){
float v = p.data[i+j*p.h*p.w];
if(v < min[j]) min[j] = v;
if(v > max[j]) max[j] = v;
}
}
for(i = ; i < p.c; ++i){
if(max[i] - min[i] < .){
min[i] = ;
max[i] = ;
}
}
for(j = ; j < p.c; ++j){
for(i = ; i < p.w*p.h; ++i){
p.data[i+j*p.h*p.w] = (p.data[i+j*p.h*p.w] - min[j])/(max[j]-min[j]);
}
}
free(min);
free(max);
} image copy_image(image p)
{
image copy = p;
copy.data = calloc(p.h*p.w*p.c, sizeof(float));
memcpy(copy.data, p.data, p.h*p.w*p.c*sizeof(float));
return copy;
} void rgbgr_image(image im)
{
int i;
for(i = ; i < im.w*im.h; ++i){
float swap = im.data[i];
im.data[i] = im.data[i+im.w*im.h*];
im.data[i+im.w*im.h*] = swap;
}
} #ifdef OPENCV
void show_image_cv(image p, const char *name)
{
int x,y,k;
image copy = copy_image(p);
constrain_image(copy);
if(p.c == ) rgbgr_image(copy);
//normalize_image(copy); char buff[];
//sprintf(buff, "%s (%d)", name, windows);
sprintf(buff, "%s", name); IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
int step = disp->widthStep;
cvNamedWindow(buff, CV_WINDOW_NORMAL);
//cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
++windows;
for(y = ; y < p.h; ++y){
for(x = ; x < p.w; ++x){
for(k= ; k < p.c; ++k){
disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy,x,y,k)*);
}
}
}
free_image(copy);
if(){
int w = ;
int h = w*p.h/p.w;
if(h > ){
h = ;
w = h*p.w/p.h;
}
IplImage *buffer = disp;
disp = cvCreateImage(cvSize(w, h), buffer->depth, buffer->nChannels);
cvResize(buffer, disp, CV_INTER_LINEAR);
cvReleaseImage(&buffer);
}
cvShowImage(buff, disp); cvReleaseImage(&disp);
} void show_image_cv_ipl(IplImage *disp, const char *name)
{
if (disp == NULL) return;
char buff[];
//sprintf(buff, "%s (%d)", name, windows);
sprintf(buff, "%s", name);
cvNamedWindow(buff, CV_WINDOW_NORMAL);
//cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
++windows;
cvShowImage(buff, disp);
//cvReleaseImage(&disp);
}
#endif void show_image(image p, const char *name)
{
#ifdef OPENCV
show_image_cv(p, name);
#else
fprintf(stderr, "Not compiled with OpenCV, saving to %s.png instead\n", name);
save_image(p, name);
#endif
} #ifdef OPENCV image ipl_to_image(IplImage* src)
{
unsigned char *data = (unsigned char *)src->imageData;
int h = src->height;
int w = src->width;
int c = src->nChannels;
int step = src->widthStep;
image out = make_image(w, h, c);
int i, j, k, count=;; for(k= ; k < c; ++k){
for(i = ; i < h; ++i){
for(j = ; j < w; ++j){
out.data[count++] = data[i*step + j*c + k]/.;
}
}
}
return out;
} image load_image_cv(char *filename, int channels)
{
IplImage* src = ;
int flag = -;
if (channels == ) flag = -;
else if (channels == ) flag = ;
else if (channels == ) flag = ;
else {
fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
} if( (src = cvLoadImage(filename, flag)) == )
{
fprintf(stderr, "Cannot load image \"%s\"\n", filename);
char buff[];
sprintf(buff, "echo %s >> bad.list", filename);
system(buff);
return make_image(,,);
//exit(0);
}
image out = ipl_to_image(src);
cvReleaseImage(&src);
rgbgr_image(out);
return out;
} image get_image_from_stream(CvCapture *cap)
{
IplImage* src = cvQueryFrame(cap);
if (!src) return make_empty_image(,,);
image im = ipl_to_image(src);
rgbgr_image(im);
return im;
} image get_image_from_stream_resize(CvCapture *cap, int w, int h, IplImage** in_img, int cpp_video_capture)
{
printf("start to get_image_from_stream_resize.................\n");
IplImage* src;
if (cpp_video_capture) {
static int once = ;
if (once) {
once = ;
do {
src = get_webcam_frame(cap);
if (!src) return make_empty_image(, , );
} while (src->width < || src->height < || src->nChannels < );
} else
src = get_webcam_frame(cap);
}
else src = cvQueryFrame(cap); if (!src) return make_empty_image(, , );
if (src->width < || src->height < || src->nChannels < ) return make_empty_image(, , );
IplImage* new_img = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, );
*in_img = cvCreateImage(cvSize(src->width, src->height), IPL_DEPTH_8U, );
cvResize(src, *in_img, CV_INTER_LINEAR);
cvResize(src, new_img, CV_INTER_LINEAR);
image im = ipl_to_image(new_img);
cvReleaseImage(&new_img);
if (cpp_video_capture) cvReleaseImage(&src);
rgbgr_image(im);
printf("im is \n",im);
return im;
} void save_image_jpg(image p, const char *name)
{
image copy = copy_image(p);
if(p.c == ) rgbgr_image(copy);
int x,y,k; char buff[];
sprintf(buff, "%s.jpg", name); IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
int step = disp->widthStep;
for(y = ; y < p.h; ++y){
for(x = ; x < p.w; ++x){
for(k= ; k < p.c; ++k){
disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy,x,y,k)*);
}
}
}
cvSaveImage(buff, disp,);
cvReleaseImage(&disp);
free_image(copy);
}
#endif void save_image_png(image im, const char *name)
{
char buff[];
//sprintf(buff, "%s (%d)", name, windows);
sprintf(buff, "%s.png", name);
unsigned char *data = calloc(im.w*im.h*im.c, sizeof(char));
int i,k;
for(k = ; k < im.c; ++k){
for(i = ; i < im.w*im.h; ++i){
data[i*im.c+k] = (unsigned char) (*im.data[i + k*im.w*im.h]);
}
}
int success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c);
free(data);
if(!success) fprintf(stderr, "Failed to write image %s\n", buff);
} void save_image(image im, const char *name)
{
#ifdef OPENCV
save_image_jpg(im, name);
#else
save_image_png(im, name);
#endif
} void show_image_layers(image p, char *name)
{
int i;
char buff[];
for(i = ; i < p.c; ++i){
sprintf(buff, "%s - Layer %d", name, i);
image layer = get_image_layer(p, i);
show_image(layer, buff);
free_image(layer);
}
} void show_image_collapsed(image p, char *name)
{
image c = collapse_image_layers(p, );
show_image(c, name);
free_image(c);
} image make_empty_image(int w, int h, int c)
{
image out;
out.data = ;
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;
} image make_random_image(int w, int h, int c)
{
image out = make_empty_image(w,h,c);
out.data = calloc(h*w*c, sizeof(float));
int i;
for(i = ; i < w*h*c; ++i){
out.data[i] = (rand_normal() * .) + .;
}
return out;
} image float_to_image(int w, int h, int c, float *data)
{
image out = make_empty_image(w,h,c);
out.data = data;
return out;
} image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect)
{
int x, y, c;
float cx = im.w/.;
float cy = im.h/.;
image rot = make_image(w, h, im.c);
for(c = ; c < im.c; ++c){
for(y = ; y < h; ++y){
for(x = ; x < w; ++x){
float rx = cos(rad)*((x - w/.)/s*aspect + dx/s*aspect) - sin(rad)*((y - h/.)/s + dy/s) + cx;
float ry = sin(rad)*((x - w/.)/s*aspect + dx/s*aspect) + cos(rad)*((y - h/.)/s + dy/s) + cy;
float val = bilinear_interpolate(im, rx, ry, c);
set_pixel(rot, x, y, c, val);
}
}
}
return rot;
} image rotate_image(image im, float rad)
{
int x, y, c;
float cx = im.w/.;
float cy = im.h/.;
image rot = make_image(im.w, im.h, im.c);
for(c = ; c < im.c; ++c){
for(y = ; y < im.h; ++y){
for(x = ; x < im.w; ++x){
float rx = cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx;
float ry = sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy;
float val = bilinear_interpolate(im, rx, ry, c);
set_pixel(rot, x, y, c, val);
}
}
}
return rot;
} void translate_image(image m, float s)
{
int i;
for(i = ; i < m.h*m.w*m.c; ++i) m.data[i] += s;
} void scale_image(image m, float s)
{
int i;
for(i = ; i < m.h*m.w*m.c; ++i) m.data[i] *= s;
} image crop_image(image im, int dx, int dy, int w, int h)
{
image cropped = make_image(w, h, im.c);
int i, j, k;
for(k = ; k < im.c; ++k){
for(j = ; j < h; ++j){
for(i = ; i < w; ++i){
int r = j + dy;
int c = i + dx;
float val = ;
r = constrain_int(r, , im.h-);
c = constrain_int(c, , im.w-);
if (r >= && r < im.h && c >= && c < im.w) {
val = get_pixel(im, c, r, k);
}
set_pixel(cropped, i, j, k, val);
}
}
}
return cropped;
} int best_3d_shift_r(image a, image b, int min, int max)
{
if(min == max) return min;
int mid = floor((min + max) / .);
image c1 = crop_image(b, , mid, b.w, b.h);
image c2 = crop_image(b, , mid+, b.w, b.h);
float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, );
float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, );
free_image(c1);
free_image(c2);
if(d1 < d2) return best_3d_shift_r(a, b, min, mid);
else return best_3d_shift_r(a, b, mid+, max);
} int best_3d_shift(image a, image b, int min, int max)
{
int i;
int best = ;
float best_distance = FLT_MAX;
for(i = min; i <= max; i += ){
image c = crop_image(b, , i, b.w, b.h);
float d = dist_array(c.data, a.data, a.w*a.h*a.c, );
if(d < best_distance){
best_distance = d;
best = i;
}
printf("%d %f\n", i, d);
free_image(c);
}
return best;
} void composite_3d(char *f1, char *f2, char *out, int delta)
{
if(!out) out = "out";
image a = load_image(f1, ,,);
image b = load_image(f2, ,,);
int shift = best_3d_shift_r(a, b, -a.h/, a.h/); image c1 = crop_image(b, , shift, b.w, b.h);
float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, );
image c2 = crop_image(b, -, shift, b.w, b.h);
float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, ); if(d2 < d1 && ){
image swap = a;
a = b;
b = swap;
shift = -shift;
printf("swapped, %d\n", shift);
}
else{
printf("%d\n", shift);
} image c = crop_image(b, delta, shift, a.w, a.h);
int i;
for(i = ; i < c.w*c.h; ++i){
c.data[i] = a.data[i];
}
#ifdef OPENCV
save_image_jpg(c, out);
#else
save_image(c, out);
#endif
} void fill_image(image m, float s)
{
int i;
for (i = ; i < m.h*m.w*m.c; ++i) m.data[i] = s;
} void letterbox_image_into(image im, int w, int h, image boxed)
{
int new_w = im.w;
int new_h = im.h;
if (((float)w / im.w) < ((float)h / im.h)) {
new_w = w;
new_h = (im.h * w) / im.w;
}
else {
new_h = h;
new_w = (im.w * h) / im.h;
}
image resized = resize_image(im, new_w, new_h);
embed_image(resized, boxed, (w - new_w) / , (h - new_h) / );
free_image(resized);
} image letterbox_image(image im, int w, int h)
{
int new_w = im.w;
int new_h = im.h;
if (((float)w / im.w) < ((float)h / im.h)) {
new_w = w;
new_h = (im.h * w) / im.w;
}
else {
new_h = h;
new_w = (im.w * h) / im.h;
}
image resized = resize_image(im, new_w, new_h);
image boxed = make_image(w, h, im.c);
fill_image(boxed, .);
//int i;
//for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0;
embed_image(resized, boxed, (w - new_w) / , (h - new_h) / );
free_image(resized);
return boxed;
} image resize_max(image im, int max)
{
int w = im.w;
int h = im.h;
if(w > h){
h = (h * max) / w;
w = max;
} else {
w = (w * max) / h;
h = max;
}
if(w == im.w && h == im.h) return im;
image resized = resize_image(im, w, h);
return resized;
} image resize_min(image im, int min)
{
int w = im.w;
int h = im.h;
if(w < h){
h = (h * min) / w;
w = min;
} else {
w = (w * min) / h;
h = min;
}
if(w == im.w && h == im.h) return im;
image resized = resize_image(im, w, h);
return resized;
} image random_crop_image(image im, int w, int h)
{
int dx = rand_int(, im.w - w);
int dy = rand_int(, im.h - h);
image crop = crop_image(im, dx, dy, w, h);
return crop;
} image random_augment_image(image im, float angle, float aspect, int low, int high, int size)
{
aspect = rand_scale(aspect);
int r = rand_int(low, high);
int min = (im.h < im.w*aspect) ? im.h : im.w*aspect;
float scale = (float)r / min; float rad = rand_uniform(-angle, angle) * TWO_PI / .; float dx = (im.w*scale/aspect - size) / .;
float dy = (im.h*scale - size) / .;
if(dx < ) dx = ;
if(dy < ) dy = ;
dx = rand_uniform(-dx, dx);
dy = rand_uniform(-dy, dy); image crop = rotate_crop_image(im, rad, scale, size, size, dx, dy, aspect); return crop;
} float three_way_max(float a, float b, float c)
{
return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
} float three_way_min(float a, float b, float c)
{
return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
} // http://www.cs.rit.edu/~ncs/color/t_convert.html
void rgb_to_hsv(image im)
{
assert(im.c == );
int i, j;
float r, g, b;
float h, s, v;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
r = get_pixel(im, i , j, );
g = get_pixel(im, i , j, );
b = get_pixel(im, i , j, );
float max = three_way_max(r,g,b);
float min = three_way_min(r,g,b);
float delta = max - min;
v = max;
if(max == ){
s = ;
h = ;
}else{
s = delta/max;
if(r == max){
h = (g - b) / delta;
} else if (g == max) {
h = + (b - r) / delta;
} else {
h = + (r - g) / delta;
}
if (h < ) h += ;
h = h/.;
}
set_pixel(im, i, j, , h);
set_pixel(im, i, j, , s);
set_pixel(im, i, j, , v);
}
}
} void hsv_to_rgb(image im)
{
assert(im.c == );
int i, j;
float r, g, b;
float h, s, v;
float f, p, q, t;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
h = * get_pixel(im, i , j, );
s = get_pixel(im, i , j, );
v = get_pixel(im, i , j, );
if (s == ) {
r = g = b = v;
} else {
int index = floor(h);
f = h - index;
p = v*(-s);
q = v*(-s*f);
t = v*(-s*(-f));
if(index == ){
r = v; g = t; b = p;
} else if(index == ){
r = q; g = v; b = p;
} else if(index == ){
r = p; g = v; b = t;
} else if(index == ){
r = p; g = q; b = v;
} else if(index == ){
r = t; g = p; b = v;
} else {
r = v; g = p; b = q;
}
}
set_pixel(im, i, j, , r);
set_pixel(im, i, j, , g);
set_pixel(im, i, j, , b);
}
}
} image grayscale_image(image im)
{
assert(im.c == );
int i, j, k;
image gray = make_image(im.w, im.h, );
float scale[] = {0.587, 0.299, 0.114};
for(k = ; k < im.c; ++k){
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
gray.data[i+im.w*j] += scale[k]*get_pixel(im, i, j, k);
}
}
}
return gray;
} image threshold_image(image im, float thresh)
{
int i;
image t = make_image(im.w, im.h, im.c);
for(i = ; i < im.w*im.h*im.c; ++i){
t.data[i] = im.data[i]>thresh ? : ;
}
return t;
} image blend_image(image fore, image back, float alpha)
{
assert(fore.w == back.w && fore.h == back.h && fore.c == back.c);
image blend = make_image(fore.w, fore.h, fore.c);
int i, j, k;
for(k = ; k < fore.c; ++k){
for(j = ; j < fore.h; ++j){
for(i = ; i < fore.w; ++i){
float val = alpha * get_pixel(fore, i, j, k) +
( - alpha)* get_pixel(back, i, j, k);
set_pixel(blend, i, j, k, val);
}
}
}
return blend;
} void scale_image_channel(image im, int c, float v)
{
int i, j;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
float pix = get_pixel(im, i, j, c);
pix = pix*v;
set_pixel(im, i, j, c, pix);
}
}
} void translate_image_channel(image im, int c, float v)
{
int i, j;
for(j = ; j < im.h; ++j){
for(i = ; i < im.w; ++i){
float pix = get_pixel(im, i, j, c);
pix = pix+v;
set_pixel(im, i, j, c, pix);
}
}
} image binarize_image(image im)
{
image c = copy_image(im);
int i;
for(i = ; i < im.w * im.h * im.c; ++i){
if(c.data[i] > .) c.data[i] = ;
else c.data[i] = ;
}
return c;
} void saturate_image(image im, float sat)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
hsv_to_rgb(im);
constrain_image(im);
} void hue_image(image im, float hue)
{
rgb_to_hsv(im);
int i;
for(i = ; i < im.w*im.h; ++i){
im.data[i] = im.data[i] + hue;
if (im.data[i] > ) im.data[i] -= ;
if (im.data[i] < ) im.data[i] += ;
}
hsv_to_rgb(im);
constrain_image(im);
} void exposure_image(image im, float sat)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
hsv_to_rgb(im);
constrain_image(im);
} void distort_image(image im, float hue, float sat, float val)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
scale_image_channel(im, , val);
int i;
for(i = ; i < im.w*im.h; ++i){
im.data[i] = im.data[i] + hue;
if (im.data[i] > ) im.data[i] -= ;
if (im.data[i] < ) im.data[i] += ;
}
hsv_to_rgb(im);
constrain_image(im);
} void random_distort_image(image im, float hue, float saturation, float exposure)
{
float dhue = rand_uniform_strong(-hue, hue);
float dsat = rand_scale(saturation);
float dexp = rand_scale(exposure);
distort_image(im, dhue, dsat, dexp);
} void saturate_exposure_image(image im, float sat, float exposure)
{
rgb_to_hsv(im);
scale_image_channel(im, , sat);
scale_image_channel(im, , exposure);
hsv_to_rgb(im);
constrain_image(im);
} float bilinear_interpolate(image im, float x, float y, int c)
{
int ix = (int) floorf(x);
int iy = (int) floorf(y); float dx = x - ix;
float dy = y - iy; float val = (-dy) * (-dx) * get_pixel_extend(im, ix, iy, c) +
dy * (-dx) * get_pixel_extend(im, ix, iy+, c) +
(-dy) * dx * get_pixel_extend(im, ix+, iy, c) +
dy * dx * get_pixel_extend(im, ix+, iy+, c);
return val;
} image resize_image(image im, int w, int h)
{
image resized = make_image(w, h, im.c);
image part = make_image(w, im.h, im.c);
int r, c, k;
float w_scale = (float)(im.w - ) / (w - );
float h_scale = (float)(im.h - ) / (h - );
for(k = ; k < im.c; ++k){
for(r = ; r < im.h; ++r){
for(c = ; c < w; ++c){
float val = ;
if(c == w- || im.w == ){
val = get_pixel(im, im.w-, r, k);
} else {
float sx = c*w_scale;
int ix = (int) sx;
float dx = sx - ix;
val = ( - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+, r, k);
}
set_pixel(part, c, r, k, val);
}
}
}
for(k = ; k < im.c; ++k){
for(r = ; r < h; ++r){
float sy = r*h_scale;
int iy = (int) sy;
float dy = sy - iy;
for(c = ; c < w; ++c){
float val = (-dy) * get_pixel(part, c, iy, k);
set_pixel(resized, c, r, k, val);
}
if(r == h- || im.h == ) continue;
for(c = ; c < w; ++c){
float val = dy * get_pixel(part, c, iy+, k);
add_pixel(resized, c, r, k, val);
}
}
} free_image(part);
return resized;
} void test_resize(char *filename)
{
image im = load_image(filename, ,, );
float mag = mag_array(im.data, im.w*im.h*im.c);
printf("L2 Norm: %f\n", mag);
image gray = grayscale_image(im); image c1 = copy_image(im);
image c2 = copy_image(im);
image c3 = copy_image(im);
image c4 = copy_image(im);
distort_image(c1, ., 1.5, 1.5);
distort_image(c2, -., ., .);
distort_image(c3, ., 1.5, .);
distort_image(c4, ., ., 1.5); show_image(im, "Original");
show_image(gray, "Gray");
show_image(c1, "C1");
show_image(c2, "C2");
show_image(c3, "C3");
show_image(c4, "C4");
#ifdef OPENCV
while(){
image aug = random_augment_image(im, , ., , , );
show_image(aug, "aug");
free_image(aug); float exposure = 1.15;
float saturation = 1.15;
float hue = .; image c = copy_image(im); float dexp = rand_scale(exposure);
float dsat = rand_scale(saturation);
float dhue = rand_uniform(-hue, hue); distort_image(c, dhue, dsat, dexp);
show_image(c, "rand");
printf("%f %f %f\n", dhue, dsat, dexp);
free_image(c);
cvWaitKey();
}
#endif
} 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();
}
if(channels) c = channels;
int i,j,k;
image im = make_image(w, h, c);
for(k = ; k < c; ++k){
for(j = ; j < h; ++j){
for(i = ; 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]/.;
}
}
}
free(data);
return im;
} image load_image(char *filename, int w, int h, int c)
{
#ifdef OPENCV #ifndef CV_VERSION_EPOCH
//image out = load_image_stb(filename, c); // OpenCV 3.x
image out = load_image_cv(filename, c);
#else
image out = load_image_cv(filename, c); // OpenCV 2.4.x
#endif #else
image out = load_image_stb(filename, c); // without OpenCV
#endif if((h && w) && (h != out.h || w != out.w)){
image resized = resize_image(out, w, h);
free_image(out);
out = resized;
}
return out;
} image load_image_color(char *filename, int w, int h)
{
return load_image(filename, w, h, );
} image get_image_layer(image m, int l)
{
image out = make_image(m.w, m.h, );
int i;
for(i = ; i < m.h*m.w; ++i){
out.data[i] = m.data[i+l*m.h*m.w];
}
return out;
} void print_image(image m)
{
int i, j, k;
for(i = ; i < m.c; ++i){
for(j = ; j < m.h; ++j){
for(k = ; k < m.w; ++k){
printf("%.2lf, ", m.data[i*m.h*m.w + j*m.w + k]);
if(k > ) break;
}
printf("\n");
if(j > ) break;
}
printf("\n");
}
printf("\n");
} image collapse_images_vert(image *ims, int n)
{
int color = ;
int border = ;
int h,w,c;
w = ims[].w;
h = (ims[].h + border) * n - border;
c = ims[].c;
if(c != || !color){
w = (w+border)*c - border;
c = ;
} image filters = make_image(w, h, c);
int i,j;
for(i = ; i < n; ++i){
int h_offset = i*(ims[].h+border);
image copy = copy_image(ims[i]);
//normalize_image(copy);
if(c == && color){
embed_image(copy, filters, , h_offset);
}
else{
for(j = ; j < copy.c; ++j){
int w_offset = j*(ims[].w+border);
image layer = get_image_layer(copy, j);
embed_image(layer, filters, w_offset, h_offset);
free_image(layer);
}
}
free_image(copy);
}
return filters;
} image collapse_images_horz(image *ims, int n)
{
int color = ;
int border = ;
int h,w,c;
int size = ims[].h;
h = size;
w = (ims[].w + border) * n - border;
c = ims[].c;
if(c != || !color){
h = (h+border)*c - border;
c = ;
} image filters = make_image(w, h, c);
int i,j;
for(i = ; i < n; ++i){
int w_offset = i*(size+border);
image copy = copy_image(ims[i]);
//normalize_image(copy);
if(c == && color){
embed_image(copy, filters, w_offset, );
}
else{
for(j = ; j < copy.c; ++j){
int h_offset = j*(size+border);
image layer = get_image_layer(copy, j);
embed_image(layer, filters, w_offset, h_offset);
free_image(layer);
}
}
free_image(copy);
}
return filters;
} void show_image_normalized(image im, const char *name)
{
image c = copy_image(im);
normalize_image(c);
show_image(c, name);
free_image(c);
} void show_images(image *ims, int n, char *window)
{
image m = collapse_images_vert(ims, n);
/*
int w = 448;
int h = ((float)m.h/m.w) * 448;
if(h > 896){
h = 896;
w = ((float)m.w/m.h) * 896;
}
image sized = resize_image(m, w, h);
*/
normalize_image(m);
save_image(m, window);
show_image(m, window);
free_image(m);
} void free_image(image m)
{
if(m.data){
free(m.data);
}
}
上边的代码是darknet/image.c的相关代码,我们主要在函数draw_detections_v3下进行修改,主要几处修改如下:
yolov3输出检测图片位置信息的更多相关文章
- 使用NPOI从Excel中提取图片及图片位置信息
问题背景: 话说,在ExcelReport的开发过程中,有一个比较棘手的问题:怎么复制图片呢? 当然,解决这个问题的第一步是:能使用NPOI提取到图片及图片的位置信息.到这里,一切想法都很顺利.但NP ...
- 机器学习进阶-人脸关键点检测 1.dlib.get_frontal_face_detector(构建人脸框位置检测器) 2.dlib.shape_predictor(绘制人脸关键点检测器) 3.cv2.convexHull(获得凸包位置信息)
1.dlib.get_frontal_face_detector() # 获得人脸框位置的检测器, detector(gray, 1) gray表示灰度图, 2.dlib.shape_predict ...
- YOLOv3模型识别车位图片的测试报告(节选)
1,YOLOv3模型简介 YOLO能实现图像或视频中物体的快速识别.在相同的识别类别范围和识别准确率条件下,YOLO识别速度最快. 官网:https://pjreddie.com/darknet/yo ...
- laravel7 百度智能云检测图片是否合规
APP 文件下建一个Libs目录建一个BaiduService文件,需要检测引入进行调用即可 BaiduService文件内容如下 <?php namespace App\Libs; use A ...
- Android模拟位置信息
Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服 ...
- 用 Python 和 OpenCV 检测图片上的条形码
用 Python 和 OpenCV 检测图片上的的条形码 这篇博文的目的是应用计算机视觉和图像处理技术,展示一个条形码检测的基本实现.我所实现的算法本质上基于StackOverflow 上的这个问 ...
- freemarker 图片输出、多张图片输出(图片重复原因及解决)
单张图片显示: 先写好word格式的文档,在其中插入一张图片,然后重命名为ftl,再用编辑器打开,把图片那一段base64信息用freemarker表达式替换掉. 之后把图片的信息输出到模板即可. 多 ...
- Android 图片Exif信息相关的获取与修改
1 Exif是什么 Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈.快门.平衡白.ISO.焦距.日期时间 ...
- android上传位置信息导致的流量大爆炸问题调查
原由:项目中有人写了个位置上传的服务,其实一直没问题,后来不知道什么时候出现了很多抱怨,是开着app流量一下子跑掉了几个G,差点就要卖房子还移动话费了,很多同事哭笑不得的找上门来,后来PM解决了,我一 ...
随机推荐
- 02-08Android学习进度报告八
今天主要学习了昨天还没有学习完的Date & Time组件的知识. 首先是DatePicker(日期选择器) android:calendarTextColor : 日历列表的文本的颜色 an ...
- matplotlib常见操作
import osimport numpy as npfrom PIL import Imageimport matplotlib.pyplot as plt img = Image.open(os. ...
- HDU 5564:Clarke and digits 收获颇多的矩阵快速幂 + 前缀和
Clarke and digits Accepts: 16 Submissions: 29 Time Limit: 5000/3000 MS (Java/Others) Memory Limi ...
- 第1节 Scala基础语法:14、15、list集合练习
package cn.itcast.collect /** *作业题 */object ListTest { def main(args: Array[String]): Unit = { //创建一 ...
- 搭建DHCP服务
DHCP服务流程 1.DHCP客户端初次接入网络时,会发送DHCP发现报文(DHCP Discover),用于查找和定位DHCP服务器. 2.DHCP服务器在接收到DHCP发现报文后,发送DHCP提供 ...
- bat批处理下如何像shell一样将命令执行的效果赋值给变量
在bat下如何实现像shell一样,把执行命令行后的结果赋值给变量呐? 刚开始,可真难为到我了.随着对bat批处理知识熟悉的加深. 学习到了!!! 举个 栗子: svnlook uuid C:\R ...
- scrapy(创建scrapy工程)报错:“ ImportError:DLL load failed:找不到指定的模块”
先要确定什么模块找不到 解决方法 windowa环境下加 ( --user) pip install -I cryptography --user
- Linux设备驱动的软件架构思想
驱动相关:硬件之上的软件层,负责底层硬件与用户程序的交互 设备相关:底层设备的硬件操作 总线:匹配设备和驱动 设备驱动分层的思想:为同一类设备设计一个框架,而框架中的核心层则实现了该设备的一些 ...
- C# 中的委托和事件 转载张子阳的
C# 中的委托和事件 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人 ...
- 数据库事务ACID特效
一.数据库事务正确执行的4个基础要素: 1.原子性 整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节.事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状 ...