I am getting an image matrix [converted from image to text file using Matlab] from a text file in an 1-D array. After applying linear Median filtering, I want to save the new array back to text file [and then back to an image using Matlab] to visualize the effects.
`define xlen 158
`define ylen 159
`define totLen `xlen * `ylen
module median1(
input clk
);
reg [7:0] imagOrig[0:`totLen-1];
reg [7:0] imagTrans[0:`totLen-1];
reg [7:0] xIndex =1;
reg [7:0] yIndex =0;
int writeTrans;
reg chk =0;
initial $readmemh("imagVecHex.txt", imagOrig);
always @ (clk) begin
if (yIndex <`ylen) begin
//Median
if (imagOrig[yIndex * `ylen + xIndex] > imagOrig[yIndex * `ylen + xIndex -1] && //if B is median
imagOrig[yIndex * `ylen + xIndex] < imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex];
else if (imagOrig[yIndex * `ylen + xIndex] < imagOrig[yIndex * `ylen + xIndex -1] && //if B is median
imagOrig[yIndex * `ylen + xIndex] > imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex];
else if (imagOrig[yIndex * `ylen + xIndex -1] > imagOrig[yIndex * `ylen + xIndex] && //if A is median
imagOrig[yIndex * `ylen + xIndex -1] < imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex -1];
else if (imagOrig[yIndex * `ylen + xIndex -1] < imagOrig[yIndex * `ylen + xIndex] && //if A is median
imagOrig[yIndex * `ylen + xIndex -1] > imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex -1];
else if (imagOrig[yIndex * `ylen + xIndex +1] > imagOrig[yIndex * `ylen + xIndex] && //if C is median
imagOrig[yIndex * `ylen + xIndex +1] < imagOrig[yIndex * `ylen + xIndex -1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex +1];
else if (imagOrig[yIndex * `ylen + xIndex +1] < imagOrig[yIndex * `ylen + xIndex] && //if C is median
imagOrig[yIndex * `ylen + xIndex +1] > imagOrig[yIndex * `ylen + xIndex -1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex +1];
//***if two or more are equall
else if (imagOrig[yIndex * `ylen + xIndex] == imagOrig[yIndex * `ylen + xIndex -1] || //if B == (A || C)
imagOrig[yIndex * `ylen + xIndex] == imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex];
else if (imagOrig[yIndex * `ylen + xIndex -1] == imagOrig[yIndex * `ylen + xIndex] || //if A == (B || C)
imagOrig[yIndex * `ylen + xIndex -1] == imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex -1];
else if (imagOrig[yIndex * `ylen + xIndex +1] == imagOrig[yIndex * `ylen + xIndex] || //if C == (A || B)
imagOrig[yIndex * `ylen + xIndex +1] == imagOrig[yIndex * `ylen + xIndex -1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex +1];
else if (imagOrig[yIndex * `ylen + xIndex] == imagOrig[yIndex * `ylen + xIndex -1] && //if A == B == C
imagOrig[yIndex * `ylen + xIndex] == imagOrig[yIndex * `ylen + xIndex +1] )
imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex];
Now, after I get the transformed array, I want to store it back to the file.
The two methods I know are $writememh and $fwrite. The problem with both is that these methods are used inside initial AND initial can not be put inside if-condition. [I need if-condition to store the array AFTER transformation is done]
This may roughly look like this:
xIndex =xIndex +1;
if (xIndex ==`xlen-1) begin
xIndex =1;
yIndex =yIndex +1;
if (yIndex ==`ylen) //When the last entry is processed the raise the flag 'chk'
chk =1;
end
end
end
if (chk ==1) begin //After 'chk' is raised write the array to memory and reset the flag
writeTrans = $fopen("imagVecHexTrans.txt","w");
$fwrite(writeTrans,"%h %h\n",imagTrans);
$fclose(writeTrans);
chk =0;
end
endmodule
My question is:
Is there any file-io method without 'initial'?
If not, then how to rewrite the code to get same functionality?
initialandalwayscan accept the same code, only one runs once at the start and the other runs on an edge trigger (@(posedge clk)) or level trigger (@*). ie anything in an initial can go inside an always.initialoralwaysbe put insideif-condition? @Morgan