Browse Source

Init quantization and PPM, Fixes for bayer2rgb

master
Apostolos Fanakis 6 years ago
parent
commit
0dbee87dd5
  1. 61
      Assignment_1/bayer2rgb.m
  2. 18
      Assignment_1/imagedequant.m
  3. 17
      Assignment_1/imagequant.m
  4. 14
      Assignment_1/mydequant.m
  5. 14
      Assignment_1/myquant.m
  6. 47
      Assignment_1/saveasppm.m
  7. 57546
      Assignment_1/try.ppm

61
Assignment_1/bayer2rgb.m

@ -209,7 +209,7 @@ function xc = bayer2rgb(xb, M, N, method)
for currentCol = 1:N for currentCol = 1:N
if ((mod(centerPointRow(currentRow), 2) == 0 && ... if ((mod(centerPointRow(currentRow), 2) == 0 && ...
mod(centerPointCol(currentCol), 2)) == 0 || ... mod(centerPointCol(currentCol), 2) == 0) || ...
(mod(centerPointRow(currentRow), 2) ~= 0 && ... (mod(centerPointRow(currentRow), 2) ~= 0 && ...
mod(centerPointCol(currentCol), 2) ~= 0)) mod(centerPointCol(currentCol), 2) ~= 0))
% This point of the original image DOES contain % This point of the original image DOES contain
@ -248,11 +248,11 @@ function xc = bayer2rgb(xb, M, N, method)
fixedCenterPointCol = centerPointCol(currentCol) + 1; fixedCenterPointCol = centerPointCol(currentCol) + 1;
end end
if (fixedCenterPointRow < 1) % if (fixedCenterPointRow < 1)
% Fix for the marginal first row case % % Fix for the marginal first row case
fixedCenterPointRow = 1; % fixedCenterPointRow = 1;
fixedCenterPointCol = fixedCenterPointCol - 1; % fixedCenterPointCol = fixedCenterPointCol - 1;
end % end
xc.green(currentRow, currentCol) = ... xc.green(currentRow, currentCol) = ...
tiltedInterp(gridPointsCoordinatesX(currentCol), ... tiltedInterp(gridPointsCoordinatesX(currentCol), ...
@ -277,6 +277,29 @@ end
function value = tiltedInterp(xw, yw, xc, yc, xbPadded) function value = tiltedInterp(xw, yw, xc, yc, xbPadded)
% Rotates the nearest points by 45 degrees
x1 = (xc - 1) * cosd(45) - yc * sind(45);
y1 = (xc - 1) * sind(45) + yc * cosd(45);
x2 = (xc + 1) * cosd(45) - yc * sind(45);
y2 = xc * sind(45) + (yc + 1) * cosd(45);
lowerLeftInterpPoint = ...
((x2 - xw) / (x2 - x1)) * ...
xbPadded(yc + 2, xc - 1 + 2) + ...
((xw - x1) / (x2 - x1)) * ...
xbPadded(yc + 1 + 2, xc + 2);
upperRightInterpPoint = ...
((x2 - xw) / (x2 - x1)) * ...
xbPadded(yc - 1 + 2, xc + 2) + ...
((xw - x1) / (x2 - x1)) * ...
xbPadded(yc + 2, xc + 1 + 2);
value2 = ...
(y2 - yw) / (y2 - y1) * lowerLeftInterpPoint + ...
(yw - y1) / (y2 - y1) * upperRightInterpPoint;
% Calculates the vertical displacement of the main diagonal that % Calculates the vertical displacement of the main diagonal that
% crosses this point % crosses this point
bw = yw - xw; bw = yw - xw;
@ -289,9 +312,9 @@ function value = tiltedInterp(xw, yw, xc, yc, xbPadded)
% the lower point and the left point % the lower point and the left point
dist = sqrt((x1 - xw) ^ 2 + (x1 + bw - yw) ^ 2); dist = sqrt((x1 - xw) ^ 2 + (x1 + bw - yw) ^ 2);
% Calculates the lengths of the upper parts of the lines connecting the % Calculates the length of the upper part of the line connecting the
% points % points
h1 = sqrt(((xc - 1 - xw) ^ 2 + (yc - yw) ^ 2) - dist ^ 2); h1 = abs(sqrt(((xc - 1 - xw) ^ 2 + (yc - yw) ^ 2) - dist ^ 2));
lowerLeftInterpPoint = ... lowerLeftInterpPoint = ...
((sqrt(2) - h1) / sqrt(2)) * ... ((sqrt(2) - h1) / sqrt(2)) * ...
@ -308,18 +331,12 @@ function value = tiltedInterp(xw, yw, xc, yc, xbPadded)
value = ... value = ...
(sqrt(2) - dist) / sqrt(2) * lowerLeftInterpPoint + ... (sqrt(2) - dist) / sqrt(2) * lowerLeftInterpPoint + ...
dist / sqrt(2) * upperRightInterpPoint; dist / sqrt(2) * upperRightInterpPoint;
end
value = (xbPadded(yc + 2, xc - 1 + 2) + ...
xbPadded(yc + 2, xc + 1 + 2) + ...
xbPadded(yc - 1 + 2, xc + 2) + ...
xbPadded(yc + 1 + 2, xc + 2)) / 4;
% if (value ~= value2)
% disp(1);
% end
end

18
Assignment_1/imagedequant.m

@ -0,0 +1,18 @@
function x = imagedequant(q, w1, w2, w3)
%imagedequant dequantizes the pixel values of a tri-chromatic image
% Usage x = imagedequant(q, w1, w2, w3), where:
% Inputs
% - q is the input image who's values are going to be dequantized
% - w1 is the quantization step size for the first channel (red)
% - w2 is the quantization step size for the second channel (green)
% - w3 is the quantization step size for the third channel (blue)
%
% Output
% - x is the image with the dequantized values
x = struct;
x.red = mydequant(q.red, w1);
x.green = mydequant(q.green, w2);
x.blue = mydequant(q.blue, w3);
end

17
Assignment_1/imagequant.m

@ -0,0 +1,17 @@
function q = imagequant(x, w1, w2, w3)
%imagequant quantizes the pixel values of a tri-chromatic image
% Usage q = imagequant(x, w1, w2, w3), where:
% Inputs
% - x is the input image who's values are going to be quantized
% - w1 is the quantization step size for the first channel (red)
% - w2 is the quantization step size for the second channel (green)
% - w3 is the quantization step size for the third channel (blue)
%
% Output
% - q is the image with the quantized values
q = struct;
q.red = myquant(x.red, w1);
q.green = myquant(x.green, w2);
q.blue = myquant(x.blue, w3);
end

14
Assignment_1/mydequant.m

@ -0,0 +1,14 @@
function x = mydequant(q, w)
%Implementation of a uniform, symmetric dequantizer without a dead zone
% Usage x = mydequant(q, w), where:
% Inputs
% - q is the input that is going to be dequantized, this can be a
% scalar, a column or row vector or a matrix
% - w is the quantization step size
%
% Output
% - x holds the quantized value(s), depending on the input this may
% be a scalar, a column or row vector or a matrix
x = (2 * q .* w + w) / 2;
end

14
Assignment_1/myquant.m

@ -0,0 +1,14 @@
function q = myquant(x, w)
%Implementation of a uniform, symmetric quantizer without a dead zone
% Usage q = myquant(x, w), where:
% Inputs
% - x is the input that is going to be quantized, this can be a
% scalar, a column or row vector or a matrix
% - w is the quantization step size
%
% Output
% - q holds the quantized value(s), depending on the input this may
% be a scalar, a column or row vector or a matrix
q = floor(x ./ w);
end

47
Assignment_1/saveasppm.m

@ -0,0 +1,47 @@
function saveasppm(x, filename, K)
%saveasppm encodes an image as PPM and saves it to the disk
% Usage saveasppm(x, filename, K), where:
% Inputs
% - x is the input image
% - filename is the path and filename of the file where the resulting
% PPM image is going to be saved
% - K is the maximum value of brightness
file = fopen(filename, 'wt+');
fwrite(file, 'P6');
fwrite(file, ' ');
fwrite(file, int2str(size(x.red, 1)));
fwrite(file, ' ');
fwrite(file, int2str(size(x.red, 2)));
fwrite(file, ' ');
fwrite(file, int2str(K));
fwrite(file, newline);
if K < 256
precision = 'integer*1';
precisionBytes = 1;
machinefmt = 'l';
else
precision = 'integer*2';
precisionBytes = 2;
machinefmt = 'b';
end
for row = 1:size(x.red, 1)
for column = 1:size(x.red, 2)
fwrite(file, x.red(row, column), precision, machinefmt);
fwrite(file, ' ');
fwrite(file, x.green(row, column), precision, machinefmt);
fwrite(file, ' ');
fwrite(file, x.blue(row, column), precision, machinefmt);
fwrite(file, ' ');
end
end
% combined = B(:,[1;1]*(1:size(B,2)));
% combined(:,1:2:end) = A;
fclose(file);
end

57546
Assignment_1/try.ppm

File diff suppressed because it is too large
Loading…
Cancel
Save