From 118bdd6f624a81c7b43689943485f8d70cbd944e Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Fri, 14 Feb 2014 18:26:31 +0000
Subject: [PATCH] Training on VOC

---
 src/image.c |  133 +++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 118 insertions(+), 15 deletions(-)

diff --git a/src/image.c b/src/image.c
index 62ee5f7..460df3d 100644
--- a/src/image.c
+++ b/src/image.c
@@ -242,8 +242,107 @@
     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;
 
-image load_image(char *filename)
+    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 load_image(char *filename, int h, int w)
 {
     IplImage* src = 0;
     if( (src = cvLoadImage(filename,-1)) == 0 )
@@ -251,10 +350,14 @@
         printf("Cannot load file image %s\n", filename);
         exit(0);
     }
+    cvShowImage("Orig", src);
+    IplImage *resized = resizeImage(src, h, w, 1);
+    cvShowImage("Sized", resized);
+    cvWaitKey(0);
+    cvReleaseImage(&src);
+    src = resized;
     unsigned char *data = (unsigned char *)src->imageData;
     int c = src->nChannels;
-    int h = src->height;
-    int w = src->width;
     int step = src->widthStep;
     image out = make_image(h,w,c);
     int i, j, k, count=0;;
@@ -363,14 +466,14 @@
         two_d_convolve(m, i, kernel, i, stride, out, channel, edge);
     }
     /*
-    int j;
-    for(i = 0; i < m.h; i += stride){
-        for(j = 0; j < m.w; j += stride){
-            float val = single_convolve(m, kernel, i, j);
-            set_pixel(out, i/stride, j/stride, channel, val);
-        }
-    }
-    */
+       int j;
+       for(i = 0; i < m.h; i += stride){
+       for(j = 0; j < m.w; j += stride){
+       float val = single_convolve(m, kernel, i, j);
+       set_pixel(out, i/stride, j/stride, channel, val);
+       }
+       }
+     */
 }
 
 void upsample_image(image m, int stride, image out)
@@ -422,10 +525,10 @@
         }
     }
     /*
-    for(i = 0; i < update.h*update.w*update.c; ++i){
-        update.data[i] /= (m.h/stride)*(m.w/stride);
-    }
-    */
+       for(i = 0; i < update.h*update.w*update.c; ++i){
+       update.data[i] /= (m.h/stride)*(m.w/stride);
+       }
+     */
 }
 
 void single_back_convolve(image m, image kernel, int x, int y, float val)

--
Gitblit v1.10.0