MATLAB Answers

I want to convert the following code to verilog using hdl coder, please help

4 views (last 30 days)
Akshatha V Murthy
Akshatha V Murthy on 29 Oct 2018
Answered: Kiran Kintali on 1 Nov 2020
clc;
close all;
clear all;
% tic;
% Applying SIFT on First Image
I = imread('rose.jpg'); disp(size(I));
I_read = imresize(I,[256 256]); figure, imshow(I_read);title('original');
I_enlarge = imresize(I_read,[512 512]);
I = rgb2gray(I_enlarge);
%I=I_enlarge;
figure,imshow(I);
I = im2double(I);
I_temp = I;
original = I;
octave1 = [];
octave2 = [];
octave3 = [];
% FORMING OCTAVES tic; k2 = 0; sigma_1 = 1.6; [m,n] = size(I); I_temp(m:m+4,n:n+4) = 0; % zero padding clear c; %octave1 tic; for k1 = 0:3 k = sqrt(2); sigma = (k^(k1+(2*k2)))*sigma_1; for x = -2:2 for y = -2:2 h(x+3,y+3) = (1/((2*pi)*((k*sigma)*(k*sigma))))*exp(-((x*x)+(y*y))/(2*(k*k)*(sigma*sigma))); end end for i=1:m for j=1:n temp1=I_temp(i:i+4,j:j+4)'.*h; conv_1(i,j)=sum(sum(temp1)); end end figure, imshow(conv_1(1:m-4,1:n-4));title('octave1'); octave1 = [octave1 conv_1]; end % octave 2 clear I_temp; I_temp2 = imresize(original,1/((k2+1)*2)); k2 = 1; y = size(I_temp2); [m,n] = size(I_temp2); I_temp2(m:m+4,n:n+4) = 0; clear c; for k1 = 0:3 k = sqrt(2); sigma = (k^(k1+(2*k2)))*sigma_1; for x = -2:2 for y = -2:2 h1(x+3,y+3) = (1/((2*pi)*((k*sigma)*(k*sigma))))*exp(-((x*x)+(y*y))/(2*(k*k)*(sigma*sigma))); end end for i = 1:m for j = 1:n temp2 = I_temp2(i:i+4,j:j+4)'.*h1; conv_2(i,j) = sum(sum(temp2)); end end figure, imshow(conv_2(1:m-4,1:n-4));title('octave2'); octave2 = [octave2 conv_2]; end % octave 3 clear I_temp; I_temp3 = imresize(original,1/((k2+1)*2)); z = size(I_temp3); k2 = 2; [m,n] = size(I_temp3); I_temp3(m:m+4,n:n+4) = 0; clear c; for k1 = 0:3 k = sqrt(2); sigma = (k^(k1+(2*k2)))*sigma_1; for x = -2:2 for y = -2:2 h2(x+3,y+3) = (1/((2*pi)*((k*sigma)*(k*sigma))))*exp(-((x*x)+(y*y))/(2*(k*k)*(sigma*sigma))); end end for i = 1:m for j = 1:n temp3 = I_temp3(i:i+4,j:j+4)'.*h2; conv_3(i,j) = sum(sum(temp3)); end end figure, imshow(conv_3(1:m-4,1:n-4));title('octave3'); octave3 = [octave3 conv_3]; end clear a; a = imresize(original,1/((k2+1)*2)); fprintf('\n Time for Gaussian scale space construction: %.3f s\n',toc) ;
% DOG---difference of gaussian tic; diff_11 = octave1(1:512,1:512)-octave1(1:512,513:1024); diff_12 = octave1(1:512,513:1024)-octave1(1:512,1025:1536); diff_13 = octave1(1:512,1025:1536)-octave1(1:512,1537:2048); diff_21 = octave2(1:256,1:256)-octave2(1:256,257:512); diff_22 = octave2(1:256,257:512)-octave2(1:256,513:768); diff_23 = octave2(1:256,513:768)-octave2(1:256,769:1024); diff_31 = octave3(1:128,1:128)-octave3(1:128,129:256); diff_32 = octave3(1:128,129:256)-octave3(1:128,257:384); diff_33 = octave3(1:128,257:384)-octave3(1:128,385:512); fprintf('\n Time for Differential scale space construction: %.3f s\n',toc) ;
% find exterma from DOG tic; key = []; x1 = 0; y1 = 0; z1 = 0; f = 0; for i = 2:511 for j = 2:511 % x1=0; % y1=0; % z1=0; if (((diff_12(i,j)>diff_12(i-1,j))&&(diff_12(i,j)>diff_12(i+1,j)).... &&(diff_12(i,j)>diff_12(i,j-1))&&(diff_12(i,j)>diff_12(i+1,j+1)).... &&(diff_12(i,j)>diff_12(i-1,j-1))&&(diff_12(i,j)>diff_12(i-1,j+1))..... &&(diff_12(i,j)>diff_12(i+1,j-1))&&(diff_12(i,j)>diff_12(i,j+1)))) x1=x1+1; else continue; end if x1>0 if((diff_12(i,j)>diff_13(i,j))&&(diff_12(i,j)>diff_13(i-1,j))..... &&(diff_12(i,j)>diff_13(i+1,j))&&(diff_12(i,j)>diff_13(i,j-1))...... &&(diff_12(i,j)>diff_13(i+1,j+1))&&(diff_12(i,j)>diff_13(i-1,j-1))... &&(diff_12(i,j)>diff_13(i-1,j+1))&&(diff_12(i,j)>diff_13(i+1,j-1))&&(diff_12(i,j)>diff_13(i,j+1))) y1 = y1+1; else continue;
end
end
% if y1>0
% if ((diff_12(i,j)>diff_11(i,j))&&(diff_12(i,j)>diff_11(i-1,j))&&(diff_12(i,j)>diff_11(i+1,j))&&(diff_12(i,j)>diff_11(i,j-1))&&(diff_12(i,j)>diff_11(i+1,j+1))&&(diff_12(i,j)>diff_11(i-1,j-1))&&(diff_12(i,j)>diff_11(i-1,j+1))&&(diff_12(i,j)>diff_11(i+1,j-1))&&(diff_12(i,j)>diff_11(i,j+1)))
% z1=z1+1;
% else
% continue;
% end
% end
key(i,j) = diff_12(i,j);
f = 1;
end
end
fprintf('\n Time for finding key points: %.3f s\n',toc) ;
if f == 0
x = 0;
y = 0;
z = 0;
for i = 2:511
for j = 2:511
if (((diff_12(i,j)<diff_12(i-1,j))&&(diff_12(i,j)<diff_12(i+1,j))&&(diff_12(i,j)<diff_12(i,j-1))&&(diff_12(i,j)<diff_12(i+1,j+1))&&(diff_12(i,j)<diff_12(i-1,j-1))&&(diff_12(i,j)<diff_12(i-1,j+1))&&(diff_12(i,j)<diff_12(i+1,j-1))&&(diff_12(i,j)<diff_12(i,j+1))))
x = x+1;
else
continue;
end
if x>0
if ((diff_12(i,j)<diff_13(i,j))&&(diff_12(i,j)<diff_13(i-1,j))&&(diff_12(i,j)<diff_13(i+1,j))&&(diff_12(i,j)<diff_13(i,j-1))&&(diff_12(i,j)<diff_13(i+1,j+1))&&(diff_12(i,j)<diff_13(i-1,j-1))&&(diff_12(i,j)<diff_13(i-1,j+1))&&(diff_12(i,j)<diff_13(i+1,j-1))&&(diff_12(i,j)<diff_13(i,j+1)))
y = y+1;
else
continue;
end
end
if y>0
if ((diff_12(i,j)<diff_11(i,j))&&(diff_12(i,j)<diff_11(i-1,j))&&(diff_12(i,j)<diff_11(i+1,j))&&(diff_12(i,j)<diff_11(i,j-1))&&(diff_12(i,j)<diff_11(i+1,j+1))&&(diff_12(i,j)<diff_11(i-1,j-1))&&(diff_12(i,j)<diff_11(i-1,j+1))&&(diff_12(i,j)<diff_11(i+1,j-1))&&(diff_12(i,j)<diff_11(i,j+1)))
z = z+1;
else
continue;
end
end
key(i,j) = diff_12(i,j);
end
end
end
key1 = key*255;
figure,imshow(key1);
% finding key point location [key_m,key_n] = size(key); r = 1; key_p = []; for i = 1:key_m for j = 1:key_n if key(i,j)>0 % key_p=[key_p,i,j]; key_p(r,1) = i; key_p(r,2) = j; r = r+1; end end end fprintf('\n total number of key points of image 1 are: \n'); disp(length(key_p));
% magnitude and phase calculation for i = 2:511 for j = 2:511 mag_1(i,j) = ((diff_12(i+1,j)-diff_12(i-1,j))^2)+((diff_12(i,j+1)-diff_12(i,j-1))^2); phase(i,j) = atan2((diff_12(i,j+1)-diff_11(i,j-1)),(diff_12(i+1,j)-diff_11(i-1,j))); end end mag = sqrt(mag_1);
% orintation..... for k = 1:length(key_p) m = key_p(k,1); n = key_p(k,2); if (m<=2)||(n<=2)||(m>=509)||(n>=509) continue; end temp_mag = mag(m-2:m+2,n-2:n+2); temp_phase = phase(m-2:m+2,n-2:n+2); clear bin_p; clear bin_m; s = 1; for i = 1:5 for j = 1:5 k1 = 1; for x = 1:36 if temp_phase(i,j)>-pi+(k1-1)*0.1745 && temp_phase(i,j)<-pi+0.1745*k1 bin_p(k1,s) = temp_phase(i,j); bin_m(k1,s) = temp_mag(i,j); s = s+1; end bin_p(k1,s) = 0; bin_m(k1,s) = 0; %s=s+1; k1 = k1+1; end end end for i = 1:36 d1 = sum(bin_m(i,:)); magv(i,k) = d1; end end max_mag = max(magv); for i = 1:length(key_p) if max_mag(1,i) == 0 continue; end j = find(magv(:,i)==(max_mag(1,i))); max_mag(2,i) = j; end
% discriptor for k = 1:length(key_p) magv_2 = []; m = key_p(k,1); n = key_p(k,2); if (m<=8)||(n<=8)||(m>=503)||(n>=503) continue; end temp_mag_d = mag(m-7:m+8,n-7:n+8); temp_phase_d = phase(m-7:m+8,n-7:n+8); store_phase = []; for i = 1:4:13 for j = 1:4:13 vijj = temp_mag_d(i:i+3,j:j+3); vijj_phase = temp_phase_d(i:i+3,j:j+3); %end store_phase = [store_phase,vijj_phase]; s = 1; for i1 = 1:4 for j1 = 1:4 k1 = 1; for x = 1:8 if vijj_phase(i1,j1)>-pi+(k1-1)*(pi/4) && vijj_phase(i1,j1)<-pi+(pi/4)*k1 bin_p1(k1,s) = vijj_phase(i1,j1); bin_m1(k1,s) = vijj(i1,j1); s = s+1; end bin_p1(k1,s) = 0; bin_m1(k1,s) = 0; %s=s+1; k1 = k1+1; end end end for g = 1:8 d2(:,1) = sum(bin_m1(g,:)); magv_2 = [magv_2,d2]; end end end discriptor(k,:)=magv_2(:,:)'; end

  0 Comments

Sign in to comment.

Answers (1)

Kiran Kintali
Kiran Kintali on 1 Nov 2020
The above MATLAB example is poorly written to be taken to HDL code generation.
First divide your code into design (DUT) and testbench.
Write your loops such that a pixel or workingSet is passed to DUT along with control signals instead of whole image. Buffer enough lines in the DUT and perform compute.
See example of histogram equalization in the attached example.
See attached guidance material and MATLAB to HDL samples.

  0 Comments

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by