From f98bf6bbdb5ed81f2ea2071ad8e705130f7ba596 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Sat, 28 Mar 2015 23:11:37 +0000
Subject: [PATCH] We do our OWN resizing!
---
src/image.c | 158 ++++++++++++++++------------------------------------
src/darknet.c | 1
src/image.h | 1
3 files changed, 50 insertions(+), 110 deletions(-)
diff --git a/src/darknet.c b/src/darknet.c
index 037b8de..0cd6153 100644
--- a/src/darknet.c
+++ b/src/darknet.c
@@ -93,6 +93,7 @@
int main(int argc, char **argv)
{
+ //test_resize(argv[1]);
//test_convolutional_layer();
if(argc < 2){
fprintf(stderr, "usage: %s <function>\n", argv[0]);
diff --git a/src/image.c b/src/image.c
index ee7a823..174b24c 100644
--- a/src/image.c
+++ b/src/image.c
@@ -375,105 +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;
@@ -494,6 +395,44 @@
return out;
}
+// #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);
+ show_image(im, "original");
+ show_image(small, "smaller");
+ show_image(big, "bigger");
+ cvWaitKey(0);
+}
+
image load_image_color(char *filename, int h, int w)
{
IplImage* src = 0;
@@ -502,13 +441,12 @@
printf("Cannot load file image %s\n", filename);
exit(0);
}
- if(h && w && (src->height != h || src->width != w)){
- //printf("Resized!\n");
- IplImage *resized = resizeImage(src, h, w, 0);
- 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;
}
@@ -521,12 +459,12 @@
printf("Cannot load file image %s\n", filename);
exit(0);
}
- if(h && w ){
- IplImage *resized = resizeImage(src, h, w, 0);
- 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;
}
diff --git a/src/image.h b/src/image.h
index 93b9e7e..f010f05 100644
--- a/src/image.h
+++ b/src/image.h
@@ -66,5 +66,6 @@
void kernel_update(image m, image update, int stride, int channel, image out, int edge);
void free_image(image m);
+void test_resize(char *filename);
#endif
--
Gitblit v1.10.0