Here to review, although he is a C + +, in order to lazy, first use MATLAB to solve the problem ...
Here is the MATLAB Bayer Jitter algorithm, uses the 256 level other gray image to shake the same size black and white picture.
Clear
CLC
m1 = [[0 2];[ 3 1]];
U1=ones (2, 2);
M2=[[4*M1 4*m1+2*u1];[ 4*M1+3*U1 4*M1+U1]]
U2=ones (4, 4);
M3=[[4*M2 4*m2+2*u2];[ 4*M2+3*U2 4*M2+U2]]
I = Imread (' test.bmp ');
GI =. 2989*i (:,:, 1) ...
+.5870*i (:,:, 2) ...
+.1140*i (:,:, 3);
%imshow (GI);
%r = I (:,:, 1);
%g = I (:,:, 2);
%b = I (:,:, 3);
[h W] = size (GI);
BW = 0;
For I=1:h
For J=1:w
if (GI (i,j)/4> m3 (Bitand (i, 7) + 1, Bitand (j,7) + 1))
BW (I,J) = 255;
Else
BW (I,J) = 0;
End
End
End
Imshow (BW);
The specific idea of the algorithm see "jitter algorithm small 1".
Here's how to turn 24bit's true color picture into a 15bit or 16bit picture. Or use the Floyd-steinberg algorithm. First say the composition of 15Bit, Red 5bit, Green 5bit, blue 5bit,1 bit reserved. 16Bit colors, red, green, blue are 5Bit, 6Bit, 5Bit. This is because the human eye on the gray sense than the color stronger, green to gray contribution to the most, so, more than one bit of green quantization will make the gray level more rich.
Clear
CLC
I = Imread (' 0001.jpg ');
img = double (I)% convert picture
[h W] = Size (IMG (:,:, 1));% Get picture size
D = 1;% is 1 o'clock, the error is passed from left to right, when-1, the error is passed from right to left
Re = 0;
GE = 0;
be = 0;
rs = 8;%2^n, n = 3 means to reduce the red quantization level to 2^5 = 32.
GS = 8;%2^n, n = 3 means to reduce the level of green quantization to 2^5 = 32.
BS = 8;%2^n, n = 3 means to reduce the blue quantization level to 2^5 = 32.
For I=1:h
For J=1:w
if (d = = 1)
val = rs * Fix (IMG (i,j,1)/RS);
Re = img (i, J, 1)-Val;
IMG (i, j, 1) = Val;
val = gs * Fix (IMG (i,j,2)/GS);
GE = img (i, J, 2)-Val;
IMG (i, j, 2) = Val;
val = bs * Fix (IMG (i,j,3)/BS);
be = img (i, J, 3)-Val;
IMG (i, j, 3) = Val;
The If ((j + 1) <= W)% calculates the transmission of the error to the right pixel
IMG (i, j + 1, 1) = IMG (i, j + 1, 1) + re * 3/8;
IMG (i, j + 1, 2) = IMG (i, j + 1, 2) + ge * 3/8;
IMG (i, j + 1, 3) = IMG (i, j + 1, 3) + be * 3/8;
End
if ((i + 1) <= h)% calculation error on the lower side of the pixel transfer
IMG (i + 1, j, 1) = img (i + 1, J, 1) + re * 3/8;
IMG (i + 1, j, 2) = img (i + 1, J, 2) + ge * 3/8;
IMG (i + 1, j, 3) = img (i + 1, J, 3) + be * 3/8;
End
if ((i + 1) <= h && (j + 1) <= W)% compute error on right lower side of pixel transfer
IMG (i + 1, j + 1, 1) = img (i + 1, j + 1, 1) + RE/4;
IMG (i + 1, j + 1, 2) = img (i + 1, j + 1, 2) + GE/4;
IMG (i + 1, j + 1, 3) = img (i + 1, j + 1, 3) + BE/4;
End
Else
val = rs * Fix (IMG (i,w-j + 1,1)/RS);
Re = img (i, w-j + 1, 1)-Val;
IMG (i, w-j + 1, 1) = Val;
val = gs * Fix (IMG (i,w-j + 1,2)/GS);
GE = img (i, w-j + 1, 2)-Val;
IMG (i, w-j + 1, 2) = Val;
val = bs * Fix (IMG (i,w-j + 1,3)/BS);
be = img (i, W-j + 1, 3)-Val;
IMG (i, W-j + 1, 3) = Val;
if ((W-J) > 0)% calculation error transfer to left side
IMG (i, w-j, 1) = IMG (i, w-j, 1) + re * 3/8;
IMG (i, w-j, 2) = img (i, W-J, 2) + ge * 3/8;
IMG (i, w-j, 3) = img (i, W-J, 3) + be * 3/8;
End
if (i + 1 <= h)% error transfer on the lower side of the calculation error
IMG (i + 1, j, 1) = img (i + 1, J, 1) + re * 3/8;
IMG (i + 1, j, 2) = img (i + 1, J, 2) + ge * 3/8;
IMG (i + 1, j, 3) = img (i + 1, J, 3) + be * 3/8;
End
if ((i + 1) <= H && (w-j) > 0)% error transmission on left side of error
IMG (i + 1, w-j, 1) = img (i + 1, w-j, 1) + RE/4;
IMG (i + 1, w-j, 2) = img (i + 1, w-j, 2) + GE/4;
IMG (i + 1, w-j, 3) = img (i + 1, w-j, 3) + BE/4;
End
End
End
d =-D;
End
out = Uint8 (IMG);
Imshow (out)
This is the original image of the processing:
This is the image after processing: