这是代码(部分用葡萄牙语)。我的目标是计算其中有多少个物体。
基本上我正在读P6,保存背景颜色,然后动态分配并读取它,最后将所有2d数组与背景颜色进行比较,如果像素不同,它会改变背景颜色和检查他的邻居,如果一些邻居处于相同状态,那么重复该过程,事情是: 由于某种原因,结果是说有更多的像素与背景颜色不同,有人可以帮助我吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct{
unsigned char r;
unsigned char g;
unsigned char b;
} PIXEL;
void defmtx(FILE *im, PIXEL **matrix, int *dim1, int *dim2, PIXEL *cdf);
void examinar(FILE *im, PIXEL **matrix, int *dim1, int *dim2, PIXEL *cdf, int *cont);
void examinarnv2(FILE *im, PIXEL **matrix, int *i, int *j, PIXEL *cdf, int *dim1, int *dim2);
int main(){
int dim1, dim2, max;
long int aux;
FILE *im;
unsigned char img[100], p6[3], qbrlin;
PIXEL **matrix, cdf;
printf("Caminho da imagem:\n");
gets(img);
//Mudar gets dps
im = fopen(img,"r");
if( im == NULL ){
printf("Erro 1\n"); //arquivo nao existe ou endereço errado
printf("Nao foi possivel abrir o arquivo\n");
exit(1);
}
else{
fscanf(im,"%s",&p6);
if ( strcmp(p6,"P6") != 0 ){
printf("\nArquivo nao e uma imagem\n");
exit(1);
}
fscanf(im,"%i %i %i", &dim1, &dim2, &max);
//dimensões da matriz
fscanf(im,"%c%c%c%c",&qbrlin, &cdf.r, &cdf.g, &cdf.b);
defmtx(im, matrix, &dim1, &dim2, &cdf);
//printf("\n%i %i %i\n", dim1, dim2, max);
//printf("\n%hhu %hhu %hhu\n",cdf.r, cdf.g, cdf.b);
}
fclose(im);
return 0;
}
void defmtx(FILE *im, PIXEL **matrix, int *dim1, int *dim2, PIXEL *cdf){
int n = 0,j = 0, cont = 0;
long int aux;
matrix = (PIXEL **)malloc( (*dim1) * sizeof(PIXEL *) );
//Alocando a matriz
while ( n < (*dim1) ){
j = 0;
matrix[n] = (PIXEL *) malloc ((*dim2) * sizeof(PIXEL));
//Alocando a matriz
if(n == 0)
j++;
while( j < (*dim2) ){
//aux = ftell(im);
//printf(" %i ",aux);
fread( &matrix[n][j].r,sizeof(unsigned char),1,im );
fread( &matrix[n][j].g,sizeof(unsigned char),1,im );
fread( &matrix[n][j].b,sizeof(unsigned char),1,im );
j++;
}
n++;
}
matrix[0][0].r = cdf->r;
matrix[0][0].g = cdf->g;
matrix[0][0].b = cdf->b;
examinar(im, matrix, dim1, dim2, cdf, &cont );
}
void examinar(FILE *im, PIXEL **matrix, int *dim1, int *dim2, PIXEL *cdf, int *cont){
int i = 0,j = 0,flag = 0;
long int aux;
while( i < (*dim1) ){
j = 0;
while( j < (*dim2) ){
if ( (matrix[i][j].r != cdf->r) || (matrix[i][j].g != cdf->g) || (matrix[i][j].b != cdf->b)){
(*cont)++;
flag++;
aux = ftell(im);
printf("\n%i\n", aux);
//printf("%i %i , %i %i %i\n",i,j,matrix[i][j].r,matrix[i][j].g,matrix[i][j].b);
matrix[i][j].r = cdf->r;
matrix[i][j].g = cdf->g;
matrix[i][j].b = cdf->b;
examinarnv2(im, matrix, &i, &j, cdf, dim1, dim2);
break;
}
j++;
}
if ( flag == 1 ){
flag--;
break;
}
i++;
}
if (flag == 0) {
if ( ( i == (*dim1) ) && ( j == (*dim2) ) ){
printf("\nnum obj: %i\n", *cont);
}
else {
examinar(im, matrix, dim1, dim2, cdf, cont);
}
}
}
void examinarnv2(FILE *im, PIXEL **matrix, int *i, int *j, PIXEL *cdf, int *dim1, int *dim2){
// Arrumar isso depois //
int k=0;
if( ( (*i) > 0 ) && ( (*i) <= ( (*dim1) - 1 ) )){
if ( ( matrix[(*i)-1][(*j)].r != cdf->r ) || ( matrix[(*i)-1][(*j)].g != cdf->g ) || ( matrix[(*i)-1][(*j)].b != cdf->b ) ){
k = (*i) - 1;
matrix[(*i)-1][(*j)].r = cdf->r;
matrix[(*i)-1][(*j)].g = cdf->g;
matrix[(*i)-1][(*j)].b = cdf->b;
examinarnv2(im, matrix, &k , j, cdf, dim1, dim2);
}
}
if( ( (*j) > 0) && ( (*j) <= ( (*dim2) - 1 ) )){
if ( ( matrix[(*i)][(*j)-1].r != cdf->r ) || ( matrix[(*i)][(*j)-1].g != cdf->g ) || ( matrix[(*i)][(*j)-1].b != cdf->b ) ){
k = (*j)-1;
matrix[(*i)][(*j)-1].r = cdf->r;
matrix[(*i)][(*j)-1].g = cdf->g;
matrix[(*i)][(*j)-1].b = cdf->b;
examinarnv2(im, matrix, i , &k, cdf, dim1, dim2);
}
}
if( ( (*i) < ( (*dim1) - 1 ) ) && ( (*i) >= 0) ){
if ( ( matrix[(*i)+1][(*j)].r != cdf->r ) || ( matrix[(*i)+1][(*j)].g != cdf->b ) || ( matrix[(*i)+1][(*j)].b != cdf->b ) ){
k = (*i) + 1;
matrix[(*i)+1][(*j)].r = cdf->r;
matrix[(*i)+1][(*j)].g = cdf->g;
matrix[(*i)+1][(*j)].b = cdf->b;
examinarnv2(im, matrix, &k , j, cdf, dim1, dim2);
}
}
if( ( (*j) < ( (*dim2) - 1 ) ) && ( (*j) >= 0 ) ){
if ( ( matrix[(*i)][(*j)+1].r != cdf->r ) || ( matrix[(*i)][(*j)+1].g != cdf->g ) || ( matrix[(*i)][(*j)+1].b != cdf->b ) ){
k = (*j) + 1;
matrix[(*i)][(*j)+1].r = cdf->r;
matrix[(*i)][(*j)+1].g = cdf->g;
matrix[(*i)][(*j)+1].b = cdf->b;
examinarnv2(im, matrix, i, &k, cdf, dim1, dim2);
}
}
}