Commit 71a3965f authored by Chris Coughlin's avatar Chris Coughlin

Force greyscale in ImageDatasets

parent 8dc22973
......@@ -24,6 +24,7 @@ import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
......@@ -145,7 +146,7 @@ public class ImageDataset extends IODataset {
* For multi-image file formats, reads the image at the specified index.
*
* @param idx index to read
* @return image at the specified index
* @return image at the specified index or null if the image couldn't be read
* @throws IOException if an I/O error occurs
* @throws IndexOutOfBoundsException if specified index is less than 0 or greater than the number of frames in the image
*/
......@@ -161,10 +162,18 @@ public class ImageDataset extends IODataset {
BufferedImage.TYPE_INT_RGB,
idx);
}
this.width = image.getWidth();
this.height = image.getHeight();
double[] ddata = getData(image, 0, 0, width, height);
return ddata;
if (image != null) {
this.width = image.getWidth();
this.height = image.getHeight();
BufferedImage greyImage = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_GRAY);
Graphics g = greyImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return getData(greyImage, 0, 0, width, height);
} else {
return null;
}
} else {
throw new IndexOutOfBoundsException("Invalid frame specified, must be between 0 " + " and " + getNumFrames());
}
......
......@@ -24,6 +24,7 @@ import org.junit.Test;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
......@@ -99,10 +100,14 @@ public class ImageDatasetTest {
public double[] img2double(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
BufferedImage greyImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = greyImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
double[] ddata = new double[w * h];
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
ddata[j * w + i] = image.getRGB(i, j);
ddata[j * w + i] = greyImage.getRGB(i, j);
}
}
return ddata;
......@@ -128,12 +133,11 @@ public class ImageDatasetTest {
public void readMultiFrameImages() throws Exception {
setReaders(multiframeImageFile);
assertEquals(imageFrames.size(), imgReader.getNumFrames());
for (int i = 0; i < imageFrames.size(); i++) {
BufferedImage expectedImage = imageFrames.get(i);
for (BufferedImage expectedImage : imageFrames) {
imgReader.read();
assertEquals(expectedImage.getWidth(), imgReader.getWidth());
assertEquals(expectedImage.getHeight(), imgReader.getHeight());
assertArraysEqual(img2double(imageFrames.get(i)), imgReader.getData());
assertArraysEqual(img2double(expectedImage), imgReader.getData());
}
}
......@@ -171,7 +175,7 @@ public class ImageDatasetTest {
int[] badFrames = new int[]{-1, imgReader.getNumFrames() + 1};
for (int badFrame : badFrames) {
try {
double[] d = imgReader.read(badFrame);
imgReader.read(badFrame);
fail("Expected an IllegalArgumentException");
} catch (IndexOutOfBoundsException oobe) {
assertEquals(oobe.getMessage(),
......
......@@ -94,18 +94,18 @@ public class HOGOperationTest {
@Test
public void gradientVector() throws Exception {
double[] angles = {
0.5884534693844212, 1.2941178003702536, 1.8183019658935073, -0.4714510096636749, -0.5916043645056042,
-0.0013029043338976995, -0.0019569222253300288, 0.5881597664509021, 1.8988068809553786, -0.2463479168199545,
1.1087276254225014, 0.9499203241586575, -2.8186934812310516, -2.1561049934298517, 1.8878750209865398,
0.4640408139125636, -0.929007196235364, -2.781919847786684, 1.5684678441244164, 3.14158248121897,
-0.40590123504777353, -1.5729668198600413, -1.1055813448563234, 2.0344500153589467, 3.141562256143188
0.5880026035475675, 1.5707963267948966, 2.356194490192345, -1.5707963267948966, -1.1071487177940904,
-0.7853981633974483, -1.5707963267948966, 0.7853981633974483, 2.677945044588987, -0.4636476090008061,
1.5707963267948966, 0.8960553845713439, 3.141592653589793, -1.5707963267948966, 1.1071487177940904,
0.5404195002705842, -0.7853981633974483, -2.677945044588987, 1.2490457723982544, 3.141592653589793,
-0.5880026035475675, -2.0344439357957027, -0.7853981633974483, 2.356194490192345, 3.141592653589793
};
double[] mags = {
473665, 477969, 270660, 146543, 236865,
392968, 261636, 472948, 207418, 270832,
147003, 565915, 208136, 236793, 207735,
588013, 328859, 561506, 328970, 196611,
499887, 591110, 293316, 147120, 65795
237220, 263172, 186091, 131586, 147118,
93045, 65793, 186091, 147118, 147118,
65793, 421281, 65793, 131586, 147118,
383636, 186091, 294235, 208056, 65793,
237220, 294235, 93045, 93045, 65793
};
SlidingWindowOperation slide = new SlidingWindowOperation(5, 5);
Dataset window = slide.run(orig);
......@@ -117,18 +117,18 @@ public class HOGOperationTest {
@Test
public void descriptors() throws Exception {
double[] descs = {
0.06530170491145333, 0.28322781688673104, 0.34270369704219694,
0.19948158895495757, 0.015335880187632382, 0.21679122527188513,
0.0, 0.0, 0.0,
0.08739388207712295, 0.22662030310185916, 0.18680709803694037,
0.12037934433187701, 0.19868614674912502, 0.09465492088989542,
0.0, 0.0, 0.0,
0.002472281908022271, 0.22941365275275352, 0.42233763832484666,
0.37040243631247227, 0.2426635317974436, 0.04896401706662007,
0.0, 0.0, 0.0,
0.03275669779930263, 0.06745738935443556, 0.11207084400247329,
0.17937548622712854, 0.2021475425488016, 0.18307827082857084,
0.0, 0.0, 0.0
0.03641657801918729, 0.2000313689730766, 0.26473176753900385,
0.29243714209263694, 0.18926941435644665, 0.027350178243506346,
0.0, 0.0, 0.0,
0.10924973405756186, 0.2826047814019705, 0.20729149279195228,
0.20197116076058896, 0.17792847786835236, 0.027350178243506346,
0.0, 0.0, 0.0,
0.0, 0.25106649033351897, 0.45486072857505216,
0.28885757989272354, 0.22725938776037125, 0.041425286992806336,
0.0, 0.0, 0.0,
0.03641657801918729, 0.03641657801918729, 0.14887450341005584,
0.12146222333831669, 0.2666251910527576, 0.2027955335923396,
0.0, 0.0, 0.0,
};
// Need a slightly larger slice here for descriptors
SlidingWindowOperation slide = new SlidingWindowOperation(5, 6);
......
......@@ -108,18 +108,18 @@ public class GradientVectorTest {
@Test
public void calc() throws Exception {
double[] angles = {
0.5884534693844212, 1.2941178003702536, 1.8183019658935073, -0.4714510096636749, -0.5916043645056042,
-0.0013029043338976995, -0.0019569222253300288, 0.5881597664509021, 1.8988068809553786, -0.2463479168199545,
1.1087276254225014, 0.9499203241586575, -2.8186934812310516, -2.1561049934298517, 1.8878750209865398,
0.4640408139125636, -0.929007196235364, -2.781919847786684, 1.5684678441244164, 3.14158248121897,
-0.40590123504777353, -1.5729668198600413, -1.1055813448563234, 2.0344500153589467, 3.141562256143188
0.5880026035475675, 1.5707963267948966, 2.356194490192345, -1.5707963267948966, -1.1071487177940904,
-0.7853981633974483, -1.5707963267948966, 0.7853981633974483, 2.677945044588987, -0.4636476090008061,
1.5707963267948966, 0.8960553845713439, 3.141592653589793, -1.5707963267948966, 1.1071487177940904,
0.5404195002705842, -0.7853981633974483, -2.677945044588987, 1.2490457723982544, 3.141592653589793,
-0.5880026035475675, -2.0344439357957027, -0.7853981633974483, 2.356194490192345, 3.141592653589793,
};
double[] mags = {
473665, 477969, 270660, 146543, 236865,
392968, 261636, 472948, 207418, 270832,
147003, 565915, 208136, 236793, 207735,
588013, 328859, 561506, 328970, 196611,
499887, 591110, 293316, 147120, 65795
237220, 263172, 186091, 131586, 147118,
93045, 65793, 186091, 147118, 147118,
65793, 421281, 65793, 131586, 147118,
383636, 186091, 294235, 208056, 65793,
237220, 294235, 93045, 93045, 65793
};
SlidingWindowOperation slide = new SlidingWindowOperation(5, 5);
Dataset window = slide.run(orig);
......
0.01109374549908828
0.21218821157595721
0.22482536071210232
0.2337814007872031
0.2812325671120377
0.1324407393402217
0.01992987790542395
0.20673124353181072
0.276839234991846
0.2567410922885127
0.26259630432888975
0.11334548305765604
0.0
0.0
0.0
0.06988255277556935
0.3236453659666081
0.2691017991760818
0.17337843284681576
0.19823366024764855
0.0927802312394465
0.0517229560983206
0.2722965530341354
0.3560099676503996
0.19340148498885207
0.15939754139825235
0.0656024223392636
0.0
0.0
0.0
0.03205258149082684
0.2980702608273086
0.27594643413633807
0.1901884115404805
0.21270897905717231
0.07965853841914805
0.03826374439333582
0.21528001143991887
0.27002257788147277
0.25820391002645715
0.14692935765283904
0.13988422371308934
0.0
0.0
0.0
0.026274408627658976
0.17668517607246936
0.25522043727465177
0.2039323438999347
0.2767113090745596
0.15500053604779618
0.02040937870519653
0.1350222724066874
0.25098930707100475
0.20700492163340717
0.26061342587498515
0.2021519147411096
0.0
0.0
0.0
0.05936598729561519
0.2453390915283124
0.1935356067841496
0.20356814020430325
0.4316320894918434
0.2158216042801397
0.050342361223827216
0.2363066760036822
0.21258024179420948
0.278203022393734
0.36266663260558923
0.20392884474251619
0.0
0.0
0.0
0.01672532184433442
0.20929947722322273
0.2073367278521091
0.18789514474223373
0.2312858943924482
0.08201724383143773
0.03803670666576965
0.1747508806947367
0.2217898014057855
0.17009122897142862
0.2627624834136948
0.10362398732035595
0.0
0.0
0.0
0.03001341015180605
0.264637918138099
0.28924949148188245
0.169547242547324
0.19179679354903628
0.12280548791161207
0.02040178276909927
0.2386652528844908
0.28292008330985663
0.24183556070039408
0.18447661534253307
0.11944268793055471
0.0
0.0
0.0
0.013940703125141474
0.19032400976872857
0.21180064041214341
0.23610091523459578
0.2538736747736851
0.0722407515971267
0.026356646307584268
0.21038796770571794
0.24980747378235052
0.24342742256418734
0.1964586860038821
0.0813437738879853
0.0
0.0
0.0
0.020601947272731597
0.1343329493914034
0.13894653092236386
0.08373737755481711
0.09598872166142444
0.07167451969379729
0.018956178502362336
0.09149170340334342
0.10084883689849236
0.0535332728000044
0.06834257137207422
0.03734870136143387
0.0
0.0
0.0
0.03823092983033689
0.15163882218419134
0.18750651880669286
0.5470386987731394
0.22215055879347587
0.12528606443620013
0.015020752779029273
0.15866888174224722
0.2299941967990508
0.6068402073532815
0.19907070602188756
0.0623944033131362
0.0
0.0
0.0
0.018080397188161377
0.08572453282782315
0.2209923479454946
0.16186936820426617
0.0899622913293764
0.06846831443326977
0.005807711912936952
0.05355833984811347
0.09704301593803633
0.10297623605441561
0.061419576240624336
0.036916544938681135
0.0
0.0
0.0
0.009870771022993989
0.0846968031467014
0.3292575513677322
0.47764101307385864
0.2558162262006184
0.11039564025296189
0.014074032339713457
0.07225894276796614
0.4354555418761333
0.4750145337885004
0.16911472836293803
0.06894601843339328
0.0
0.0
0.0
0.018671623144806616
0.10503119947733229
0.09350879509486912
0.09680046606981217
0.1405724610540501
0.05050871849949813
0.01783062003525222
0.104337264332101
0.12277354176752597
0.11481576506131418
0.11308860265204246
0.05033471899147091
0.0
0.0
0.0
0.01682945555789321
0.18494286222100384
0.15885676789244324
0.10229925875361794
0.08258065025799556
0.08008350433751223
0.021820663502465993
0.166956832341012
0.18763118288034458
0.09743222043958097
0.10151986887024206
0.0516285611212585
0.0
0.0
0.0
0.02385511093731293
0.12642383201297794
0.1512074755184807
0.07463512153292448
0.09367139651995471
0.04014299176998184
0.01938757351212835
0.11532562104536057
0.12524385568830027
0.07900327046995917
0.08111561189529005
0.06418065240424711
0.0
0.0
0.0
0.006660005606807787
0.25274027822490547
0.8415566843866659
0.18029972119790552
0.06506493154611448
0.02280732624446179
0.012084164062111918
0.2112574965995838
0.8625665324459432
0.14882678166284968
0.043276329904892545
0.008650030057934303
0.0
0.0
0.0
0.010122274561103822
0.3039667975737312
0.7560233494048378
0.1665268779845396
0.05551074470356332
0.04545927113206495
0.01209864953249008
0.29367306803456494
0.800187299204668
0.16965797652027215
0.03266713221903715
0.03026167171130985
0.0
0.0
0.0
0.010835801710209007
0.19699679705669945
0.2578691449406852
0.20159213594381528
0.08308754268950659
0.03558189054588704
0.015620912083007706
0.14609979977444773