1

I have a frame grabber that is returning R, G, B matrices (So, X, Y, {R,G,B})

But I need to slice up the data to 2 12 bit vectors; I'm unsure how to do this quickly (converting everything to strings is terrible).

If someone could help me out, I'd appreciate it; thanks all!

[Edit] here is what I mean: I have a data object that is 4-D - X,Y,RGB,FrameNumber.

So for example, if I do: data(1,1,1,1), I get the 1st row, 1st col, Red Channel, Frame 1.

The problem is my camera is infrared and actually spitting out 2 12 bit numbers (it's entirely custom). So I need to take this: data(1,1,1,1),data(1,1,2,1),data(1,1,3,1), concatentate them, and return to the user 2x12 bit numbers.

With strings:

data(1,1,1,1) = 0x40
data(1,1,2,1) = 0x37
data(1,1,3,1) = 0x4F

Concatentation gives me:

0x40374F

I'd need to return to the user, for this pixel: [403,74F]

[An apology about my example: I tried to quickly come up with a 4D matrix in matlab, and it's been so long since I've used it that it will take me a while to figure out what it actually looks like constructing it!]

Travis
  • 11
  • 3
  • You could use `mod` and `idivide`, but I am not sure what exactly you need. Could you give a small example? – Daniel Jul 24 '15 at 18:11
  • It's unclear what you actually need and why. Why exactly do you want to convert the three uint8 values to two 12-bit values? Does one value take the 8 bits from `R` and the first (lowest) four bits from `G` and the other take the remainder? This seems quite non-standard. You say you want to do this quickly -edit your question to provide example code that does it slowly so that we can see what you actually mean? Finally, since there is no 12-bit datatype, you're going to have to use uint16 values to store your 12 bits. – horchler Jul 24 '15 at 18:14
  • If you want this to be FAST, write it in C++ as a MEX Function. It should only be a few lines of code. – siliconwafer Jul 24 '15 at 19:55
  • How would the array indexing work with an object in the Matlab workspace look? That's a really good idea, I'll check that out. – Travis Jul 24 '15 at 20:55

1 Answers1

0

Maybe this is what you're looking for. I'm assuming the underlying data is decimal, and not a string, and in turn your output is a decimal number, not a string.

data(1,1,1,1)=hex2dec('40');
data(1,1,2,1)=hex2dec('37');
data(1,1,3,1)=hex2dec('4F');

%convert three numbers to one large one
x=[256^2 256 1]*squeeze(data(1,1,:,1));
%split it in two
out(1)=floor(x/4096);
out(2)=x-out(1)*4096;

%check
fprintf('upper 12 bits in hex: %s\n',dec2hex(out(1)));
fprintf('lower 12 bits in hex: %s\n',dec2hex(out(2)));
Joe Serrano
  • 424
  • 2
  • 7
  • Hi @Travis if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Daniel Aug 29 '15 at 08:35