Skip to content

Instantly share code, notes, and snippets.

@agiletalk
Created April 17, 2011 17:05
Show Gist options
  • Save agiletalk/924230 to your computer and use it in GitHub Desktop.
Save agiletalk/924230 to your computer and use it in GitHub Desktop.
Co-occurrence matrix of a binary data (DIPS Homework #3.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER 128
int main(int argc, char* argv[])
{
char buf[BUFFER];
char* ptr;
int i, j, k, l, row, col, dx, dy, z, y;
int **input, **output;
// com [no. of rows] [no. of columns] [deltaX] [deltaY] [Z] [Y] <return>
// for instance, com 3 4 1 0 0 1
if(argc != 7)
{
printf("usage: %s [no. of rows] [no. of columns] [deltaX] [deltaY] [Z] [Y]\n", argv[0]);
printf("\tfor instance, %s 3 4 1 0 0 1\n", argv[0]);
return -1;
}
// initialize
row = atoi(argv[1]);
col = atoi(argv[2]);
dx = atoi(argv[3]);
dy = atoi(argv[4]);
z = atoi(argv[5]);
y = atoi(argv[6]);
// malloc
input = (int**)malloc(sizeof(int) * row);
for(i = 0; i < row; i++)
{
input[i] = (int*)malloc(sizeof(int) * col);
}
output = (int**)malloc(sizeof(int) * row);
for(i = 0; i < row; i++)
{
output[i] = (int*)malloc(sizeof(int) * col);
memset(output[i], 0, sizeof(int) * col);
}
// input array
for(i = 0; i < row; i++)
{
j = 0;
gets(buf);
ptr = strtok(buf, " ");
input[i][j++] = atoi(ptr);
while(ptr = strtok(NULL, " "))
{
input[i][j++] = atoi(ptr);
}
}
// co-occurrence matrix of a binary data
// 1, if I(i,j) = Z and I(i+dx,j+dy) = Y
// 0, otherwise
// C△x,△y (Z,Y) matrix
printf("\nC[%d,%d](%d,%d) matrix is like this.\n", dx, dy, z, y);
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
if((i+dx < row && y+dy < col) &&
(input[i][j] == z && input[i+dx][j+dy] == y))
{
output[z][y]++;
}
}
}
for(k = 0; k < row; k++)
{
for(l = 0; l < col; l++)
{
printf("%d ", output[k][l]);
}
memset(output[k], 0, sizeof(int) * col);
printf("\n");
}
// C△x,△y matrix
printf("\nCOM[%d,%d] matrix is like this.\n", dx, dy);
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
// i => Z, j => Y
for(k = 0; k < row; k++)
{
for(l = 0; l < col; l++)
{
// k => i, l => j
if((k+dx < row && l+dy < col) &&
(input[k][l] == i && input[k+dx][l+dy] == j))
{
output[i][j]++;
}
}
}
}
}
for(k = 0; k < row; k++)
{
for(l = 0; l < col; l++)
{
printf("%d ", output[k][l]);
}
printf("\n");
}
// free
for(i = 0; i < row; i++)
{
free(input[i]);
}
free(input);
for(i = 0; i < row; i++)
{
free(output[i]);
}
free(output);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment