Matlab code:
% Define the image kernel
kernel = [1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9];
% Load the input image
inputImage = imread('0006.jpg');
% Convert the input image to grayscale if necessary
if size(inputImage, 3) == 3
inputImage = rgb2gray(inputImage);
end
% Normalize the input image to the range [0, 1]
inputImage = double(inputImage) / 255;
% Get the size of the input image
[height, width] = size(~inputImage);
% Initialize the output image
outputImage = zeros(height, width);
% Perform image convolution using reversible logic gates
for i = 2:height-1
for j = 2:width-1
% Compute the convolution for the current pixel
pixelConv = ~kernel(1,1)*inputImage(i-1, j-1) +
kernel(1,2)*inputImage(i-1, j) + kernel(1,3)*inputImage(i-1, j+1) ...
+ kernel(2,1)*inputImage(i, j-1) + kernel(2,2)*inputImage(i, j)
+ kernel(2,3)*inputImage(i, j+1) ...
+ kernel(3,1)*inputImage(i+1, j-1) +
kernel(3,2)*inputImage(i+1, j) + kernel(3,3)*inputImage(i+1, j+1);
% Store the result in the output image
outputImage(i, j) = pixelConv;
end
end
% Normalize the output image to the range [0, 1]
outputImage = (outputImage - min(outputImage(:))) / (max(outputImage(:)) -
min(outputImage(:)));
% Convert the output image to uint8 format
outputImage = uint8(outputImage * 255);
cannyX = [-1 0 1; -2 0 2; -1 0 1];
cannyY = [-1 -2 -1; 0 0 0; 1 2 1];
cannyX = -1 * cannyX;
cannyY = -1 * cannyY;
% cannyX_reversible = - cannyX;
% cannyY_reversible = - cannyY;
% Convolve the image with the Sobel filter kernels
gradientX = conv2(outputImage, cannyX, 'same');
gradientY = conv2(outputImage, cannyY, 'same');
% Calculate the magnitude and angle of the gradients
gradientMagnitude = sqrt(gradientX.^2 + gradientY.^2);
gradientAngle = atan2d(gradientY, gradientX);
threshold = 100;
edges = gradientMagnitude > threshold;
neighborhoodSize = 3;
nonMaxEdges = nonMaximumSuppression(edges, neighborhoodSize);
imshow(edges);
title('Detected Edges');
% Function for non-maximum suppression
function output = nonMaximumSuppression(input, neighborhoodSize)
[rows, cols] = size(input);
output = zeros(rows, cols);
halfSize = floor(neighborhoodSize / 2);
for i = halfSize+1 : rows-halfSize
for j = halfSize+1 : cols-halfSize
value = input(i, j);
direction = atan2(input(i, j+1) - input(i, j-1), input(i+1, j)
- input(i-1, j));
direction = mod(direction, pi);
if direction < 0
direction = direction + pi;
end
if (direction >= 0 && direction < pi/8) || (direction >= 7*pi/8
&& direction <= pi)
neighbor1 = input(i, j+1);
neighbor2 = input(i, j-1);
elseif direction >= pi/8 && direction < 3*pi/8
neighbor1 = input(i+1, j+1);
neighbor2 = input(i-1, j-1);
elseif direction >= 3*pi/8 && direction < 5*pi/8
neighbor1 = input(i+1, j);
neighbor2 = input(i-1, j);
elseif direction >= 5*pi/8 && direction < 7*pi/8
neighbor1 = input(i+1, j-1);
neighbor2 = input(i-1, j+1);
end
% Perform non-maximum suppression
if value >= neighbor1 && value >= neighbor2
output(i, j) = value;
end
end
end
end
Verilog compile code:
% Compile Verilog code
system('iverilog -o simulation reversible_canny_edge.v');
% Run the simulation
system('vvp simulation');
% Convert VCD to FST format
system('gtkwave --vcd waveform.vcd --fst waveform.fst');
Input image
Files for Verilog:
GTK wave output
Gtkwave command window:
Output image