Speeding up image-related Matlab bindings

Report crashes, strange behaviour, or apparent bugs
Post Reply
renaud.detry
Posts: 16
Joined: 24 May 2013, 16:29

Speeding up image-related Matlab bindings

Post by renaud.detry »

Hello Marc,

I'd like to submit a patch for the Matlab bindings. Is there a favored channel for patch submission?

In the meantime, you will find the patch below.

The patch brings the following changes:
  • simxGetVisionSensorImage2 and simxReadVisionSensor extract datapoints from libpointer via matrix operations instead of loops. I have a 100x speedup for images of 256x256x3 pixels (.12s instead of 15s).
  • simxGetVisionSensorImage2 and simxReadVisionSensor no longer copy their image buffer to the corresponding return argument if that argument is not requested by the user.
Best wishes,
Renaud.

Code: Select all

--- remApi.m-orig	2013-07-02 15:47:30.000000000 +0200
+++ remApi.m	2013-10-07 16:30:43.000000000 +0200
@@ -1171,31 +1171,14 @@
 		 
 			[rtn resolution_ image_] = calllib(obj.libName,'simxGetVisionSensorImage',clientID,handle_,resolution_,image_,options_,operationmode_);
 
-			if (rtn==0)
+			if (rtn==0)&(nargout>2)
 				if((resolution_(1) ~= 0) && (resolution_(2) ~= 0))
 					if(options == 1) %grayscale image
 						image_.setdatatype('uint8Ptr',1,resolution_(1)*resolution_(2));
-						image = zeros(resolution_(1),resolution_(2));
-						image = cast(image,'uint8');
-						for i = resolution_(1):-1:1;
-							count = (resolution_(1)-i)*resolution_(2);
-							for j = 1:resolution_(2);
-								image(i,j) = uint8(image_.value(count+j));
-							end
-						end
+						image = flipdim(permute(reshape(image_.Value, resolution_(1), resolution_(2)), [2 1]), 1);
 					else 
 						image_.setdatatype('uint8Ptr',1,resolution_(1)*resolution_(2)*3);
-						image = zeros(resolution_(1),resolution_(2),3);
-						image = cast(image,'uint8');
-						for i = resolution_(1):-1:1;
-							count = (resolution_(1)-i)*resolution_(2)*3;
-							for j = 1:3:resolution_(2)*3;
-								for k=1:3;
-									l=int32(j/3) +1;
-									image(i,l,k) = uint8(image_.value(count+j+k-1));  
-								end
-							end
-						end
+						image = flipdim(permute(reshape(image_.Value, 3, resolution_(1), resolution_(2)), [3 2 1]), 1);
 					end
 				end
 			else
@@ -1305,20 +1288,12 @@
 
 			auxValues = [];
 			auxValuesCount = [];
-			if(rtn == 0)
+			if(rtn == 0)&(nargout>1)
 				auxValuesCount_.setdatatype('int32Ptr',1,999);
 				packets=auxValuesCount_.value(1);
-				auxValues_.setdatatype('singlePtr',single(999));
-				n=1;
-				for i=1:packets
-					cnt=auxValuesCount_.value(i+1);
-					auxValuesCount=[auxValuesCount cnt];
-					for j=1:cnt
-						v=auxValues_.value(n);
-						n=n+1;
-						auxValues=[auxValues v];
-					end
-				end
+				auxValuesCount = auxValuesCount_.Value(2:(packets+1));
+				auxValues_.setdatatype('singlePtr',sum(auxValuesCount));
+				auxValues = auxValues_.Value';
 				auxValuesCount_.setdatatype('int8Ptr',2,2);
 				auxValues_.setdatatype('int8Ptr',2,2);
 				obj.simxReleaseBuffer(auxValuesCount_);

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Speeding up image-related Matlab bindings

Post by coppelia »

Thanks again Renaud!

We will add your changes to the next release. For patch submissions, you can also write us directly.

Cheers

renaud.detry
Posts: 16
Joined: 24 May 2013, 16:29

Re: Speeding up image-related Matlab bindings

Post by renaud.detry »

Ok, noted.

Cheers,
Renaud

Post Reply