java - Algorithm to find neighboring subimages -
i have image , extracting subimage feed neural network. attempting calculate average output of subimages in same neighborhood.
so if have original image (m x n pixels) , found subimage @ (sub_x, sub_y) size (sub_width , sub_height), need extract subimages same size (sub_width , sub_height) @ (sub_x + m, sub_y + n) m , n go 1 - 3.
i have working solution:
for (int subx = (x-3); subx < (x+4); subx++) (int suby = (y-3); suby < (y+4); suby++) if ( (subx > 0) && (suby > 0) ) if ( ((subx + width) < img.getwidth()) && ((suby + height) < img.getheight()) ){ counter++; testingimage = img.getsubimage(subx, suby, width, height); }
x,y, width, , height integers of original subimage found.
img original full sized bufferedimage.
i'm not happy performance though. there faster/smarter way this?
here's 1 simple thing can do: rid of conditions inside loops. calculate ranges first, once , run loops without range checks.
int subxstart = max(x-3, 0); int subystart = max(y-3, 0); int subxend = min(x+4, img.getwidth() - width); int subyend = min(y+4, img.getheight() - height); (int subx = subxstart; subx < subxend; subx++) { (int suby = subystart; suby < subyend; suby++) { counter++; testingimage = img.getsubimage(subx, suby, width, height); // run neural net } }
you can try switching order of loops. order matches memory ordering should considerably faster.
Comments
Post a Comment