From 6553b3f0e3e55fc30a99c7d4b5798aa86d18a114 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Mon, 30 Mar 2015 02:31:47 +0000
Subject: [PATCH] no comment
---
src/image.c | 287 +++++++++++++++++++++++++++++++++++---------------------
1 files changed, 179 insertions(+), 108 deletions(-)
diff --git a/src/image.c b/src/image.c
index e2c451b..2cfce63 100644
--- a/src/image.c
+++ b/src/image.c
@@ -4,6 +4,86 @@
int windows = 0;
+float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
+
+float get_color(int c, int x, int max)
+{
+ float ratio = ((float)x/max)*5;
+ int i = floor(ratio);
+ int j = ceil(ratio);
+ ratio -= i;
+ float r = (1-ratio) * colors[i][c] + ratio*colors[j][c];
+ //printf("%f\n", r);
+ return r;
+}
+
+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 < 0) x1 = 0;
+ if(x1 >= a.w) x1 = a.w-1;
+ if(x2 < 0) x2 = 0;
+ if(x2 >= a.w) x2 = a.w-1;
+
+ if(y1 < 0) y1 = 0;
+ if(y1 >= a.h) y1 = a.h-1;
+ if(y2 < 0) y2 = 0;
+ if(y2 >= a.h) y2 = a.h-1;
+
+ for(i = x1; i < x2; ++i){
+ a.data[i + y1*a.w + 0*a.w*a.h] = b;
+ a.data[i + y2*a.w + 0*a.w*a.h] = b;
+
+ a.data[i + y1*a.w + 1*a.w*a.h] = g;
+ a.data[i + y2*a.w + 1*a.w*a.h] = g;
+
+ a.data[i + y1*a.w + 2*a.w*a.h] = r;
+ a.data[i + y2*a.w + 2*a.w*a.h] = r;
+ }
+ for(i = y1; i < y2; ++i){
+ a.data[x1 + i*a.w + 0*a.w*a.h] = b;
+ a.data[x2 + i*a.w + 0*a.w*a.h] = b;
+
+ a.data[x1 + i*a.w + 1*a.w*a.h] = g;
+ a.data[x2 + i*a.w + 1*a.w*a.h] = g;
+
+ a.data[x1 + i*a.w + 2*a.w*a.h] = r;
+ a.data[x2 + i*a.w + 2*a.w*a.h] = r;
+ }
+}
+
+void jitter_image(image a, int h, int w, int dh, int dw)
+{
+ int i,j,k;
+ for(k = 0; k < a.c; ++k){
+ for(i = 0; i < h; ++i){
+ for(j = 0; j < w; ++j){
+ int src = j + dw + (i+dh)*a.w + k*a.w*a.h;
+ int dst = j + i*w + k*w*h;
+ //printf("%d %d\n", src, dst);
+ a.data[dst] = a.data[src];
+ }
+ }
+ }
+}
+
+void flip_image(image a)
+{
+ int i,j,k;
+ for(k = 0; k < a.c; ++k){
+ for(i = 0; i < a.h; ++i){
+ for(j = 0; j < a.w/2; ++j){
+ int index = j + a.w*(i + a.h*(k));
+ int flip = (a.w - j - 1) + 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;
@@ -138,7 +218,7 @@
}
free_image(copy);
if(disp->height < 500 || disp->width < 500 || disp->height > 1000){
- int w = 1500;
+ int w = 500;
int h = w*p.h/p.w;
if(h > 1000){
h = 1000;
@@ -295,106 +375,6 @@
return out;
}
-// Returns a new image that is a cropped version (rectangular cut-out)
-// of the original image.
-IplImage* cropImage(const IplImage *img, const CvRect region)
-{
- IplImage *imageCropped;
- CvSize size;
-
- if (img->width <= 0 || img->height <= 0
- || region.width <= 0 || region.height <= 0) {
- //cerr << "ERROR in cropImage(): invalid dimensions." << endl;
- exit(1);
- }
-
- if (img->depth != IPL_DEPTH_8U) {
- //cerr << "ERROR in cropImage(): image depth is not 8." << endl;
- exit(1);
- }
-
- // Set the desired region of interest.
- cvSetImageROI((IplImage*)img, region);
- // Copy region of interest into a new iplImage and return it.
- size.width = region.width;
- size.height = region.height;
- imageCropped = cvCreateImage(size, IPL_DEPTH_8U, img->nChannels);
- cvCopy(img, imageCropped,NULL); // Copy just the region.
-
- return imageCropped;
-}
-
-// Creates a new image copy that is of a desired size. The aspect ratio will
-// be kept constant if 'keepAspectRatio' is true, by cropping undesired parts
-// so that only pixels of the original image are shown, instead of adding
-// extra blank space.
-// Remember to free the new image later.
-IplImage* resizeImage(const IplImage *origImg, int newHeight, int newWidth,
- int keepAspectRatio)
-{
- IplImage *outImg = 0;
- int origWidth = 0;
- int origHeight = 0;
- if (origImg) {
- origWidth = origImg->width;
- origHeight = origImg->height;
- }
- if (newWidth <= 0 || newHeight <= 0 || origImg == 0
- || origWidth <= 0 || origHeight <= 0) {
- //cerr << "ERROR: Bad desired image size of " << newWidth
- // << "x" << newHeight << " in resizeImage().\n";
- exit(1);
- }
-
- if (keepAspectRatio) {
- // Resize the image without changing its aspect ratio,
- // by cropping off the edges and enlarging the middle section.
- CvRect r;
- // input aspect ratio
- float origAspect = (origWidth / (float)origHeight);
- // output aspect ratio
- float newAspect = (newWidth / (float)newHeight);
- // crop width to be origHeight * newAspect
- if (origAspect > newAspect) {
- int tw = (origHeight * newWidth) / newHeight;
- r = cvRect((origWidth - tw)/2, 0, tw, origHeight);
- }
- else { // crop height to be origWidth / newAspect
- int th = (origWidth * newHeight) / newWidth;
- r = cvRect(0, (origHeight - th)/2, origWidth, th);
- }
- IplImage *croppedImg = cropImage(origImg, r);
-
- // Call this function again, with the new aspect ratio image.
- // Will do a scaled image resize with the correct aspect ratio.
- outImg = resizeImage(croppedImg, newHeight, newWidth, 0);
- cvReleaseImage( &croppedImg );
-
- }
- else {
-
- // Scale the image to the new dimensions,
- // even if the aspect ratio will be changed.
- outImg = cvCreateImage(cvSize(newWidth, newHeight),
- origImg->depth, origImg->nChannels);
- if (newWidth > origImg->width && newHeight > origImg->height) {
- // Make the image larger
- cvResetImageROI((IplImage*)origImg);
- // CV_INTER_LINEAR: good at enlarging.
- // CV_INTER_CUBIC: good at enlarging.
- cvResize(origImg, outImg, CV_INTER_LINEAR);
- }
- else {
- // Make the image smaller
- cvResetImageROI((IplImage*)origImg);
- // CV_INTER_AREA: good at shrinking (decimation) only.
- cvResize(origImg, outImg, CV_INTER_AREA);
- }
-
- }
- return outImg;
-}
-
image ipl_to_image(IplImage* src)
{
unsigned char *data = (unsigned char *)src->imageData;
@@ -415,6 +395,86 @@
return out;
}
+image crop_image(image im, int dr, int dc, int h, int w)
+{
+ image cropped = make_image(h, w, im.c);
+ int i, j, k;
+ for(k = 0; k < im.c; ++k){
+ for(j = 0; j < h; ++j){
+ for(i = 0; i < w; ++i){
+ int r = j + dr;
+ int c = i + dc;
+ float val = 128;
+ if (r >= 0 && r < im.h && c >= 0 && c < im.w) {
+ val = get_pixel(im, r, c, k);
+ }
+ set_pixel(cropped, j, i, k, val);
+ }
+ }
+ }
+ return cropped;
+}
+
+// #wikipedia
+image resize_image(image im, int h, int w)
+{
+ image resized = make_image(h, w, im.c);
+ int r, c, k;
+ float h_scale = (float)(im.h - 1) / (h - 1) - .00001;
+ float w_scale = (float)(im.w - 1) / (w - 1) - .00001;
+ for(k = 0; k < im.c; ++k){
+ for(r = 0; r < h; ++r){
+ for(c = 0; c < w; ++c){
+ float sr = r*h_scale;
+ float sc = c*w_scale;
+ int ir = (int)sr;
+ int ic = (int)sc;
+ float x = sr-ir;
+ float y = sc-ic;
+ float val = (1-x) * (1-y) * get_pixel(im, ir, ic, k) +
+ x * (1-y) * get_pixel(im, ir+1, ic, k) +
+ (1-x) * y * get_pixel(im, ir, ic+1, k) +
+ x * y * get_pixel(im, ir+1, ic+1, k);
+ set_pixel(resized, r, c, k, val);
+ }
+ }
+ }
+ return resized;
+}
+
+void test_resize(char *filename)
+{
+ image im = load_image(filename, 0,0);
+ image small = resize_image(im, 63, 65);
+ image big = resize_image(im, 512, 513);
+ image crop = crop_image(im, 10, 50, 100, 100);
+ image crop2 = crop_image(im, -50, -30, 400, 291);
+ show_image(im, "original");
+ show_image(small, "smaller");
+ show_image(big, "bigger");
+ show_image(crop, "crop");
+ show_image(crop2, "crop2");
+ cvWaitKey(0);
+}
+
+image load_image_color(char *filename, int h, int w)
+{
+ IplImage* src = 0;
+ if( (src = cvLoadImage(filename, 1)) == 0 )
+ {
+ printf("Cannot load file image %s\n", filename);
+ exit(0);
+ }
+ image out = ipl_to_image(src);
+ if((h && w) && (h != out.h || w != out.w)){
+ image resized = resize_image(out, h, w);
+ free_image(out);
+ out = resized;
+ }
+ cvReleaseImage(&src);
+ return out;
+}
+
image load_image(char *filename, int h, int w)
{
IplImage* src = 0;
@@ -423,12 +483,12 @@
printf("Cannot load file image %s\n", filename);
exit(0);
}
- if(h && w ){
- IplImage *resized = resizeImage(src, h, w, 1);
- cvReleaseImage(&src);
- src = resized;
- }
image out = ipl_to_image(src);
+ if((h && w) && (h != out.h || w != out.w)){
+ image resized = resize_image(out, h, w);
+ free_image(out);
+ out = resized;
+ }
cvReleaseImage(&src);
return out;
}
@@ -642,10 +702,21 @@
void print_image(image m)
{
- int i;
- for(i =0 ; i < m.h*m.w*m.c; ++i) printf("%lf, ", m.data[i]);
+ int i, j, k;
+ for(i =0 ; i < m.c; ++i){
+ for(j =0 ; j < m.h; ++j){
+ for(k = 0; k < m.w; ++k){
+ printf("%.2lf, ", m.data[i*m.h*m.w + j*m.w + k]);
+ if(k > 30) break;
+ }
+ printf("\n");
+ if(j > 30) break;
+ }
+ printf("\n");
+ }
printf("\n");
}
+
image collapse_images_vert(image *ims, int n)
{
int color = 1;
--
Gitblit v1.10.0