image processing - Why the "imrotate" function clone writen by myself in Matlab is very slow -


sorry bad english.

i'm learning matlab image processing tried clone default "imrotate" function.

the first version wrote involved using for-loop traverse whole matrix it's very slow.

then read thread: image rotation matlab without using imrotate , try vectorize program , became "much" faster.

but still slow compared default imrotate implementation consuming more 1 second rotate image(resolution 1920x1080), while default implementation job in less 50 millisecond.

so wonder there still wrong code or "normal" in matlab? here code:

(p.s. there ugly code (value11=...;value12=...value21=...) because not familiar matlab , unable figure out shorter code not using loop.

function result=my_imrotate(image,angel,method)     function result=my_imrotatesinglechannel(image,angel,method)         angel=-angel/180*pi; %transform angel deg rad         [height,width]=size(image); %get image size         trmatrix=[cos(angel),-sin(angel);sin(angel),cos(angel)]; %the transformation matrix         imgsizevec=[width,height;width,-height]; %the "size vector" transformed caluclate new size         newimgsizevec=imgsizevec*trmatrix;         newwidth=ceil(max(newimgsizevec(:,1)));         newheight=ceil(max(newimgsizevec(:,2))); %caluculate new size         [oldx,oldy]=meshgrid(1:newwidth,1:newheight);         oldx=oldx-newwidth/2;         oldy=oldy-newheight/2;         temp=[oldx(:) oldy(:)]*trmatrix;         oldx=temp(:,1);         oldy=temp(:,2);         oldx=oldx+width/2;         oldy=oldy+height/2;         switch(method)         case 'nearest'             oldx=round(oldx);             oldy=round(oldy);             condition=( oldx>=1 & oldx<=width & oldy>=1 & oldy<=height );             result(condition)=image((oldx(condition)-1)*height+oldy(condition));             result(~condition)=0;             result=reshape(result,newheight,newwidth);         case 'bilinear'             x1=floor(oldx);             x2=x1+1;             y1=floor(oldy);             y2=y1+1;             condition11=(x1>=1&x1<=width&y1>=1&y1<=height);             condition12=(x1>=1&x1<=width&y2>=1&y2<=height);             condition21=(x2>=1&x2<=width&y1>=1&y1<=height);             condition22=(x2>=1&x2<=width&y2>=1&y2<=height);             value11(condition11)=double(image((x1(condition11)-1)*height+y1(condition11)));             value12(condition12)=double(image((x1(condition12)-1)*height+y2(condition12)));             value21(condition21)=double(image((x2(condition21)-1)*height+y1(condition21)));             value22(condition22)=double(image((x2(condition22)-1)*height+y2(condition22)));             value11(~condition11)=0;             value12(~condition12)=0;             value21(~condition21)=0;             value22(~condition22)=0;             result=uint8(value22.'.*(oldx-x1).*(oldy-y1)+value11.'.*(x2-oldx).*(y2-oldy)+value21.'.*(oldx-x1).*(y2-oldy)+value12.'.*(x2-oldx).*(oldy-y1));             result=reshape(result,newheight,newwidth);         otherwise             disp('sorry, unsupported algorithm. nearest/bilinear supported.');         end     end     imageinfo=size(image);     imagetype=size(imageinfo);     if(imagetype(2)==2)         result=my_imrotatesinglechannel(image,angel,method);     elseif(imagetype(2)==3&&imageinfo(3)==3)         temp=my_imrotatesinglechannel(image(:,:,1),angel,method);         [newheight,newwidth]=size(temp);         result=temp(:);         temp=my_imrotatesinglechannel(image(:,:,2),angel,method);         result=[result temp(:)];         temp=my_imrotatesinglechannel(image(:,:,2),angel,method);         result=[result temp(:)];         result=reshape(result,newheight,newwidth,3);     else         disp('sorry, unsupported input matrix. grey/rgb image supported.');     end end 

the bulk of work in imrotate done internal imrotatemex command, implemented in compiled c code.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -