June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,162 @@
|
|||
/*Abhishek Ghosh, 24th September 2017*/
|
||||
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
char** imageMatrix;
|
||||
|
||||
char blankPixel,imagePixel;
|
||||
|
||||
typedef struct{
|
||||
int row,col;
|
||||
}pixel;
|
||||
|
||||
int getBlackNeighbours(int row,int col){
|
||||
|
||||
int i,j,sum = 0;
|
||||
|
||||
for(i=-1;i<=1;i++){
|
||||
for(j=-1;j<=1;j++){
|
||||
if(i!=0 && j!=0)
|
||||
sum+= (imageMatrix[row+i][col+i]==imagePixel);
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int getBWTransitions(int row,int col){
|
||||
return ((imageMatrix[row-1][col]==blankPixel && imageMatrix[row-1][col+1]==imagePixel)
|
||||
+(imageMatrix[row-1][col+1]==blankPixel && imageMatrix[row][col+1]==imagePixel)
|
||||
+(imageMatrix[row][col+1]==blankPixel && imageMatrix[row+1][col+1]==imagePixel)
|
||||
+(imageMatrix[row+1][col+1]==blankPixel && imageMatrix[row+1][col]==imagePixel)
|
||||
+(imageMatrix[row+1][col]==blankPixel && imageMatrix[row+1][col-1]==imagePixel)
|
||||
+(imageMatrix[row+1][col-1]==blankPixel && imageMatrix[row][col-1]==imagePixel)
|
||||
+(imageMatrix[row][col-1]==blankPixel && imageMatrix[row-1][col-1]==imagePixel)
|
||||
+(imageMatrix[row-1][col-1]==blankPixel && imageMatrix[row-1][col]==imagePixel));
|
||||
}
|
||||
|
||||
int zhangSuenTest1(int row,int col){
|
||||
int neighbours = getBlackNeighbours(row,col);
|
||||
|
||||
return ((neighbours>=2 && neighbours<=6)
|
||||
&& (getBWTransitions(row,col)==1)
|
||||
&& (imageMatrix[row-1][col]==blankPixel||imageMatrix[row][col+1]==blankPixel||imageMatrix[row+1][col]==blankPixel)
|
||||
&& (imageMatrix[row][col+1]==blankPixel||imageMatrix[row+1][col]==blankPixel||imageMatrix[row][col-1]==blankPixel));
|
||||
}
|
||||
|
||||
int zhangSuenTest2(int row,int col){
|
||||
int neighbours = getBlackNeighbours(row,col);
|
||||
|
||||
return ((neighbours>=2 && neighbours<=6)
|
||||
&& (getBWTransitions(row,col)==1)
|
||||
&& (imageMatrix[row-1][col]==blankPixel||imageMatrix[row][col+1]==blankPixel||imageMatrix[row][col-1]==blankPixel)
|
||||
&& (imageMatrix[row-1][col]==blankPixel||imageMatrix[row+1][col]==blankPixel||imageMatrix[row][col+1]==blankPixel));
|
||||
}
|
||||
|
||||
void zhangSuen(char* inputFile, char* outputFile){
|
||||
|
||||
int startRow = 1,startCol = 1,endRow,endCol,i,j,count,rows,cols,processed;
|
||||
|
||||
pixel* markers;
|
||||
|
||||
FILE* inputP = fopen(inputFile,"r");
|
||||
|
||||
fscanf(inputP,"%d%d",&rows,&cols);
|
||||
|
||||
fscanf(inputP,"%d%d",&blankPixel,&imagePixel);
|
||||
|
||||
blankPixel<=9?blankPixel+='0':blankPixel;
|
||||
imagePixel<=9?imagePixel+='0':imagePixel;
|
||||
|
||||
printf("\nPrinting original image :\n");
|
||||
|
||||
imageMatrix = (char**)malloc(rows*sizeof(char*));
|
||||
|
||||
for(i=0;i<rows;i++){
|
||||
imageMatrix[i] = (char*)malloc((cols+1)*sizeof(char));
|
||||
fscanf(inputP,"%s\n",imageMatrix[i]);
|
||||
printf("\n%s",imageMatrix[i]);
|
||||
|
||||
}
|
||||
|
||||
fclose(inputP);
|
||||
|
||||
endRow = rows-2;
|
||||
endCol = cols-2;
|
||||
do{
|
||||
markers = (pixel*)malloc((endRow-startRow+1)*(endCol-startCol+1)*sizeof(pixel));
|
||||
count = 0;
|
||||
|
||||
for(i=startRow;i<=endRow;i++){
|
||||
for(j=startCol;j<=endCol;j++){
|
||||
if(imageMatrix[i][j]==imagePixel && zhangSuenTest1(i,j)==1){
|
||||
markers[count].row = i;
|
||||
markers[count].col = j;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
processed = (count>0);
|
||||
|
||||
for(i=0;i<count;i++){
|
||||
imageMatrix[markers[i].row][markers[i].col] = blankPixel;
|
||||
}
|
||||
|
||||
free(markers);
|
||||
markers = (pixel*)malloc((endRow-startRow+1)*(endCol-startCol+1)*sizeof(pixel));
|
||||
count = 0;
|
||||
|
||||
for(i=startRow;i<=endRow;i++){
|
||||
for(j=startCol;j<=endCol;j++){
|
||||
if(imageMatrix[i][j]==imagePixel && zhangSuenTest2(i,j)==1){
|
||||
markers[count].row = i;
|
||||
markers[count].col = j;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(processed==0)
|
||||
processed = (count>0);
|
||||
|
||||
for(i=0;i<count;i++){
|
||||
imageMatrix[markers[i].row][markers[i].col] = blankPixel;
|
||||
}
|
||||
|
||||
free(markers);
|
||||
}while(processed==1);
|
||||
|
||||
FILE* outputP = fopen(outputFile,"w");
|
||||
|
||||
printf("\n\n\nPrinting image after applying Zhang Suen Thinning Algorithm : \n\n\n");
|
||||
|
||||
for(i=0;i<rows;i++){
|
||||
for(j=0;j<cols;j++){
|
||||
printf("%c",imageMatrix[i][j]);
|
||||
fprintf(outputP,"%c",imageMatrix[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
fprintf(outputP,"\n");
|
||||
}
|
||||
|
||||
fclose(outputP);
|
||||
|
||||
printf("\nImage also written to : %s",outputFile);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char inputFile[100],outputFile[100];
|
||||
|
||||
printf("Enter full path of input image file : ");
|
||||
scanf("%s",inputFile);
|
||||
|
||||
printf("Enter full path of output image file : ");
|
||||
scanf("%s",outputFile);
|
||||
|
||||
zhangSuen(inputFile,outputFile);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
const pixelstring =
|
||||
"00000000000000000000000000000000" *
|
||||
"01111111110000000111111110000000" *
|
||||
"01110001111000001111001111000000" *
|
||||
"01110000111000001110000111000000" *
|
||||
"01110001111000001110000000000000" *
|
||||
"01111111110000001110000000000000" *
|
||||
"01110111100000001110000111000000" *
|
||||
"01110011110011101111001111011100" *
|
||||
"01110001111011100111111110011100" *
|
||||
"00000000000000000000000000000000"
|
||||
const pixels = reshape([UInt8(c- 48) for c in pixelstring], (32,10))'
|
||||
|
||||
|
||||
function surroundtesting(px, i, j, step)
|
||||
if px[i,j] == 0
|
||||
return false
|
||||
end
|
||||
isize, jsize = size(px)
|
||||
if i < 1 || j < 1 || i == isize || j == jsize # criteria 0.both
|
||||
return false
|
||||
end
|
||||
s = Array{Int,1}(9)
|
||||
s[1] = s[9] = px[i-1,j]; s[2] = px[i-1,j+1]; s[3] = px[i,j+1]; s[4] = px[i+1,j+1]
|
||||
s[5] = px[i+1,j]; s[6] = px[i+1,j-1]; s[7] = px[i,j-1]; s[8] = px[i-1,j-1]
|
||||
b = sum(s[1:8])
|
||||
if b < 2 || b > 6 # criteria 1.both
|
||||
return false
|
||||
end
|
||||
if sum([(s[i] == 0 && s[i+1] == 1) for i in 1:length(s)-1]) != 1 # criteria 2.both
|
||||
return false
|
||||
end
|
||||
if step == 1
|
||||
rightwhite = s[1] == 0 || s[3] == 0 || s[5] == 0 # 1.3
|
||||
downwhite = s[3] == 0 || s[5] == 0 || s[7] == 0 # 1.4
|
||||
return rightwhite && downwhite
|
||||
end
|
||||
upwhite = s[1] == 0 || s[3] == 0 || s[7] == 0 # 2.3
|
||||
leftwhite = s[1] == 0 || s[5] == 0 || s[7] == 0 # 2.4
|
||||
return upwhite && leftwhite
|
||||
end
|
||||
|
||||
|
||||
function zsthinning(mat)
|
||||
retmat = copy(mat)
|
||||
testmat = zeros(Int, size(mat))
|
||||
isize, jsize = size(testmat)
|
||||
needredo = true
|
||||
loops = 0
|
||||
while(needredo)
|
||||
loops += 1
|
||||
println("loop number $loops")
|
||||
needredo = false
|
||||
for n in 1:2
|
||||
for i in 1:isize, j in 1:jsize
|
||||
testmat[i,j] = surroundtesting(retmat, i, j, n) ? 1 : 0
|
||||
end
|
||||
for i in 1:isize, j in 1:jsize
|
||||
if testmat[i,j] == 1
|
||||
retmat[i,j] = 0
|
||||
needredo = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
retmat
|
||||
end
|
||||
|
||||
|
||||
function asciiprint(mat)
|
||||
for i in 1:size(mat)[1]
|
||||
println(join(map(i -> i == 1 ? '#' : ' ', mat[i,:])))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
asciiprint(zsthinning(pixels))
|
||||
Loading…
Add table
Add a link
Reference in a new issue