OpenCV texture synthesis

Why my code doesn't work...

#include<cv.h>
#include<highgui.h>
#include<iostream>
#include<cxcore.h>
using namespace std;

#define MaskSize 3
int Figure(IplImage *img,IplImage *sampimg,IplImage *grayimg,int i,int j,int w,int h);
int step;
int channel;
int height;
int width;
int sampstep;
int graystep;

void main()
{
int i,j;
IplImage* img=0;
img=cvLoadImage("H:\\rock_wall.jpg",-1);
if(!img)
{
printf("Could not load image file!");
exit(0);
}
cout<<"channels:"<<img->nChannels<<endl;
cout<<"depth:"<<img->depth<<endl;
cout<<"width:"<<img->width<<endl;
cout<<"height:"<<img->height<<endl;
cout<<"widthstep:"<<img->widthStep<<endl;

int step=img->widthStep;
int channel=img->nChannels;
int height=img->height;
int width=img->width;

IplImage* sampimg=cvCreateImage(cvSize(30,30),IPL_DEPTH_8U,3);
IplImage* grayimg=cvCreateImage(cvSize(30,30),IPL_DEPTH_8U,1);
int sampstep=sampimg->widthStep;
for (j=0;j<MaskSize;j++)
{
for (i=0;i<MaskSize;i++)
{
sampimg->imageData[j*sampstep+i*channel]=img->imageData[j*step+i*channel];
sampimg->imageData[j*sampstep+i*channel+1]=img->imageData[j*step+i*channel+1];
sampimg->imageData[j*sampstep+i*channel+2]=img->imageData[j*step+i*channel+2];
grayimg->imageData[j*graystep+i*channel]=1;
//grayimg->imageData[j*graystep+i*channel+1]=1;
//grayimg->imageData[j*graystep+i*channel+2]=1;
}
}

int w=MaskSize-1,h=MaskSize/2;//中心点坐标(w,h)

while (w<sampimg->width-1||h<sampimg->height-1)
{
for (j=1;j<height-2;j++)
{
for (i=1;i<width-2;i++)
{
if(Figure(img,sampimg,grayimg,i,j,w,h)==1)
{
if(w<sampimg->width)
w++;
else if(h<sampimg->height)
{
w=1;
h++;
}
break;
}
}
}
}


// cvSaveImage("H:\\texture synthesis.bmp",img);
cvvNamedWindow("open wl");
cvShowImage("open wl",img);

cvvNamedWindow("open samp");
cvShowImage("open samp",sampimg);
cvReleaseImage(&sampimg);
cvReleaseImage(&grayimg);
cvWaitKey();
cvDestroyWindow("open wl");
cvDestroyWindow("open samp");
}



int Figure(IplImage *img,IplImage *sampimg,IplImage *grayimg,int i,int j,int w,int h)
{
step=img->widthStep;
channel=img->nChannels;
height=img->height;
width=img->width;
sampstep=sampimg->widthStep;
graystep=grayimg->widthStep;
int sum=0,num=0;
for (int m=-MaskSize/2;m<=1;m++)
{
for (int n=-MaskSize/2;n<=1;n++)
{
sum+=abs(img->imageData[(j+m)*step+(i+n)*channel]-sampimg->imageData[(h+m)*sampstep+(w+n)*channel])*grayimg->imageData[(h+m)*graystep+(w+n)*channel];
sum+=abs(img->imageData[(j+m)*step+(i+n)*channel+1]-sampimg->imageData[(h+m)*sampstep+(w+n)*channel+1])*grayimg->imageData[(h+m)*graystep+(w+n)*channel];//+1];
sum+=abs(img->imageData[(j+m)*step+(i+n)*channel+2]-sampimg->imageData[(h+m)*sampstep+(w+n)*channel+2])*grayimg->imageData[(h+m)*graystep+(w+n)*channel];//+2];

num+=grayimg->imageData[(h+m)*graystep+(w+n)*channel];
}
}
if (sum/num<100)
{
for (int m=-MaskSize/2;m<=1;m++)
{
for (int n=-MaskSize/2;n<=1;n++)
{
if (grayimg->imageData[(h+m)*graystep+(w+n)*channel]==0)
{
sampimg->imageData[(h+m)*sampstep+(w+n)*channel]=img->imageData[(j+m)*step+(i+n)*channel];
sampimg->imageData[(h+m)*sampstep+(w+n)*channel+1]=img->imageData[(j+m)*step+(i+n)*channel+1];
sampimg->imageData[(h+m)*sampstep+(w+n)*channel+2]=img->imageData[(j+m)*step+(i+n)*channel+2];
grayimg->imageData[(h+m)*graystep+(w+n)*channel]=1;
//grayimg->imageData[(h+m)*graystep+(w+n)*channel+1]=1;
//grayimg->imageData[(h+m)*graystep+(w+n)*channel+2]=1;
}

}
}
return 1;
}
else return 0;
}
Topic archived. No new replies allowed.