Câu 1. Xây dựng và cài đặt thuật toán làm âm ảnh.

void Negatives(IplImage *imgin, IplImage *imgout) { cvNot(imgin, imgout); } void HistogramEqualization(IplImage *imgin, IplImage *imgout) { cvEqualizeHist(imgin,imgout); }

void HistogramOpenCV(IplImage *imgin, double p[L]) { int histSize[] = {L}; float hranges[] = {0,L-1}; float* ranges[] = {hranges}; CvHistogram* hist; hist = cvCreateHist(1,histSize,CV_HIST_ARRAY,ranges); cvCalcHist(&imgin,hist); cvNormalizeHist(hist,1.0); int r; for (r=0; r<L; r++) p[r] = cvQueryHistValue_1D(hist,r); cvReleaseHist(&hist); return; }

void LocalHistogramCV(IplImage *imgin, IplImage *imgout) { int m = 3, n = 3; CvMat *w1 = cvCreateMat(m,n,CV_8UC1); CvMat *w2 = cvCreateMat(m,n,CV_8UC1); int M = cvGetSize(imgin).height; int N = cvGetSize(imgin).width; int x, y, a, b, s, t; a = m/2; b = n/2; CvScalar value; for (x=a; x<M-a; x++) for (y=b; y<N-b; y++) { for (s=-a; s<=a; s++) for (t=-b; t<=b; t++) { value = cvGet2D(imgin,x+s,y+t); cvSet2D(w1,s+a,t+b,value); } cvEqualizeHist(w1,w2); value = cvGet2D(w2,a,b); cvSet2D(imgout,x,y,value);

} cvReleaseMat(&w1); cvReleaseMat(&w2); return; }

void HistogramStatistics(IplImage *imgin, IplImage *imgout) { int m = 3, n = 3; CvMat *w = cvCreateMat(m,n,CV_8UC1); int a = m/2, b = n/2; int N = cvGetSize(imgin).width; int M = cvGetSize(imgin).height; CvScalar mG,sigmaG; CvScalar msxy, sigmasxy; cvAvgSdv(imgin,&mG,&sigmaG); int x,y,s,t; double E = 4.0, k0 = 0.4, k1 = 0.02, k2 = 0.4; CvScalar value; for (x=a; x<M-a; x++) for (y=b; y<N-b; y++) { for (s=-a; s<=a; s++) for (t=-b; t<=b; t++) { value = cvGet2D(imgin,x+s,y+t); cvSet2D(w,s+a,t+b,value); } cvAvgSdv(w,&msxy,&sigmasxy); value = cvGet2D(imgin,x,y); if ((msxy.val[0] < k0*mG.val[0]) && (k1*sigmaG.val[0] <= sigmasxy.val[0] && sigmasxy.val[0] <= k2*sigmaG.val[0])) value.val[0] *= E; cvSet2D(imgout,x,y,value); } cvReleaseMat(&w); return; } Câu 9: void SmoothLinearFilterOld(IplImage *imgin, IplImage *imgout) { int m = 35, n = 35; CvMat *w = cvCreateMat(m,n,CV_32FC1); int x, y; CvScalar value; for (x=0; x<m; x++) for (y=0; y<n; y++) { value.val[0] = 1.0/(m*n); cvSet2D(w,x,y,value); } cvFilter2D(imgin,imgout,w); cvReleaseMat(&w); return;

} Hoặc: cvSmooth(imgin,imgout,CV_BLUR,15,15); Câu 10: cvSmooth(imgin,imgout,CV_MEDIAN,3);

void Gradient(IplImage *imgin, IplImage *imgout) { IplImage *gx = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); IplImage *gy = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); IplImage *Mag = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); int M = cvGetSize(imgin).height; int N = cvGetSize(imgin).width; int x, y; for (x=0; x<M; x++) for (y=0; y<N; y++) { cvSet2D(gx,x,y,cvGet2D(imgin,x,y)); cvSet2D(gy,x,y,cvGet2D(imgin,x,y)); } int m = 3, n = 3; CvMat *Sobelx CvMat *Sobely = cvCreateMat(m,n,CV_32FC1); = cvCreateMat(m,n,CV_32FC1);

CvScalar value; CvScalar *valueArr = new CvScalar[9]; int arr1[3][3] = {{-1,-2,-1},{0,0,0},{1,2,1}}; int arr2[3][3] = {{-1,0,1}, {-2,0,2},{-1,0,1}}; value.val[0] = 1; value.val[0] = 1; cvSet2D(Sobelx,0,0,value); for (x=0; x<m; x++) for (y=0; y<n; y++) { value.val[0] = arr1[x][y]; cvSet2D(Sobelx,x,y,value); value.val[0] = arr2[x][y]; cvSet2D(Sobely,x,y,value); } cvFilter2D(gx,gx,Sobelx); cvFilter2D(gy,gy,Sobely);

cvAdd(gx,gy,Mag); double max,r; double mintemp; cvMinMaxLoc(Mag,&mintemp,&max); for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(Mag,x,y); value.val[0] = (1.0*value.val[0]/max*255); cvSet2D(imgout,x,y,value); } cvReleaseImage(&gx); cvReleaseImage(&gy); cvReleaseImage(&Mag); cvReleaseMat(&Sobelx); cvReleaseMat(&Sobely); return; }

void Laplace(IplImage *imgin, IplImage *imgout) { IplImage *temp = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); int M = cvGetSize(imgin).height; int N = cvGetSize(imgin).width; int x, y; for (x=0; x<M; x++) for (y=0; y<N; y++) cvSet2D(temp,x,y,cvGet2D(imgin,x,y)); int m=3, n=3; CvMat *w = cvCreateMat(3,3,CV_32FC1); CvScalar value; value.val[0] = 1; for (x=0; x<m; x++) for (y=0; y<n; y++) cvSet2D(w,x,y,value); value.val[0] = -8; cvSet2D(w,1,1,value); cvFilter2D(temp,temp,w); double min,max; cvMinMaxLoc(temp,&min,&max); for (x=0; x<M; x++) for (y=0; y<N; y++) {

value = cvGet2D(temp,x,y); value.val[0] = (value.val[0] - min)/(max-min)*(L-1); cvSet2D(imgout,x,y,value); } cvReleaseImage(&temp); cvReleaseMat(&w); return; }

void UnsharpMask(IplImage *imgin, IplImage *imgout) { double sigma=3, sum; int x, y; int m = 5, n = 5; //double **fbar, **gmask; IplImage *fbar = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); IplImage *gmask = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); IplImage *f = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); int M = cvGetSize(imgin).height; int N = cvGetSize(imgin).width; for (x=0; x<M; x++) for (y=0; y<N; y++) { cvSet2D(fbar,x,y,cvGet2D(imgin,x,y)); cvSet2D(gmask,x,y,cvGet2D(imgin,x,y)); cvSet2D(f,x,y,cvGet2D(imgin,x,y)); }

// Tao mat na Gauss CvMat *Gauss = cvCreateMat(m,n,CV_32FC1); CvScalar value,value2; value.val[0] = 1; //Gauss = (double **)Alloc2D(m,n,sizeof(double)); for (x=-m/2; x<=m/2; x++) for (y=-n/2; y<=n/2; y++) { value.val[0] = exp(-(1.0*x*x+1.0*y*y)/(2*sigma*sigma)); cvSet2D(Gauss,x+2,y+2,value); } //value = cvSum(Gauss); //sum = value.val[0]; sum = 0;

for (x=0; x<m; x++) for (y=0; y<n; y++) { value = cvGet2D(Gauss,x,y); sum += value.val[0]; } for (x=0; x<m; x++) for (y=0; y<n; y++){ value = cvGet2D(Gauss,x,y); value.val[0]=value.val[0]/sum; cvSet2D(Gauss,x,y,value); //Gauss[x][y] = Gauss[x][y]/sum; } cvFilter2D(fbar,fbar,Gauss); //ConvolutionDouble(f, M, N, fbar, Gauss, m, n, TRUE); //gmask = (double **)Alloc2D(M,N,sizeof(double)); for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(f,x,y); value2 = cvGet2D(fbar,x,y); value.val[0] = value.val[0] - value2.val[0]; cvSet2D(gmask,x,y,value); //gmask[x][y] = (f[x][y] - fbar[x][y]); } int s; double k = 4.5; for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(f,x,y); value2 = cvGet2D(gmask,x,y); //s = int(f[x][y] + k*gmask[x][y]); value.val[0] = int(value.val[0] + k*value2.val[0] ); if (s > 255) s = 255; if (s < 0) s = 0; //g[x][y] = s; cvSet2D(imgout,x,y,value); } cvReleaseImage(&fbar); cvReleaseImage(&gmask); cvReleaseImage(&f); cvReleaseMat(&Gauss); return; }

Chương 9:

void Erosion(IplImage *imgin, IplImage *imgout) { int m=3, n=3; IplConvKernel *element = cvCreateStructuringElementEx(m,n,m/2,n/2,CV_SHAPE_RECT); cvErode(imgin,imgout,element); cvReleaseStructuringElement(&element); return; } void Dilation(IplImage *imgin, IplImage *imgout) { int m=3, n=3; IplConvKernel *element = cvCreateStructuringElementEx(m,n,m/2,n/2,CV_SHAPE_CROSS); cvDilate(imgin,imgout,element); cvReleaseStructuringElement(&element); return; } void Opening(IplImage *imgin, IplImage *imgout) { int m=3, n=3; IplConvKernel *element = cvCreateStructuringElementEx(m,n,m/2,n/2,CV_SHAPE_RECT); IplImage *temp = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_8U,1); cvMorphologyEx(imgin,imgout,temp,element,CV_MOP_OPEN); cvReleaseStructuringElement(&element); cvReleaseImage(&temp); return; } void MyClosing(IplImage *imgin, IplImage *imgout) { int m=5, n=5; IplConvKernel *element = cvCreateStructuringElementEx(m,n,m/2,n/2,CV_SHAPE_RECT); IplImage *temp = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_8U,1); cvMorphologyEx(imgin,imgout,temp,element,CV_MOP_CLOSE,2); cvReleaseStructuringElement(&element); cvReleaseImage(&temp); return; }

void Boundary(IplImage *imgin, IplImage *imgout) { int m=3, n=3;

IplConvKernel *element = cvCreateStructuringElementEx(m,n,m/2,n/2,CV_SHAPE_RECT); IplImage *temp = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_8U,1); cvErode(imgin,temp,element); cvAddWeighted(imgin,1,temp,-1,0,imgout); cvReleaseStructuringElement(&element); cvReleaseImage(&temp); return; }

IplImage *HoleFill(IplImage *imgin) { IplImage *imgout = cvCloneImage(imgin); CvScalar value = {L-1}; cvFloodFill(imgout,cvPoint(258,147),value); return imgout; }

Chương 10:
void LineDetectionLaplace (IplImage *imgin, IplImage *imgout) { // Dung dao ham cap hai de phat hien duong thang int m = 3, n = 3; CvMat *w = cvCreateMat(m,n,CV_32FC1); int x, y; for (x=0; x<m; x++) for (y=0; y<n; y++) { if (x==m/2 && y==n/2) cvSet2D(w,x,y,cvScalar(-8)); else cvSet2D(w,x,y,cvScalar(1)); } IplImage *temp = cvCreateImage(cvGetSize(imgin),IPL_DEPTH_32F,1); int M = cvGetSize(imgin).height; int N = cvGetSize(imgin).width; CvScalar value; for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(imgin,x,y); cvSet2D(temp,x,y,value); } cvFilter2D(temp,temp,w); double min, max; cvMinMaxLoc(temp,&min,&max); for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(temp,x,y); value.val[0] = (value.val[0]-min)/(max-min)*(L-1); cvSet2D(imgout,x,y,value); }

cvReleaseImage(&temp); return; }

Chương 4: void SmoothingFrequencyFilter(IplImage *imgin, IplImage *imgout) { int M = imgin->height; int N = imgin->width; int x, y; int P = 2*M, Q = 2*N; // Buoc 1,2 va 3: Mo rong anh va them zero // va nhan voi (-1)^(x+y) de chuyen F(0,0) vao tam anh IplImage *F = cvCreateImage(cvSize(Q,P),IPL_DEPTH_32F,2); cvSetZero(F); CvScalar value; for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(imgin,x,y); if ((x+y)%2 == 1) value.val[0] = -value.val[0]; value.val[1] = 0; cvSet2D(F,x,y,value); } // Buoc 4: Bien doi Fourier thuan cvDFT(F,F,CV_DXT_FORWARD); // Buoc 5: Tao ham loc H va thuc hien phep nhan G = HF IplImage *H = cvCreateImage(cvSize(Q,P),IPL_DEPTH_32F,2); double D0 = 30; double Duv; int u, v; int n = 2; for (u=0; u<P; u++) for (v=0; v<Q; v++) { Duv = sqrt(1.0*(u-P/2)*(u-P/2) + 1.0*(v-Q/2)*(v-Q/2)); /* // Loc thong thap ly tuong if (Duv <= D0) { value.val[0] = 1; value.val[1] = 1; cvSet2D(H,u,v,value); } else { value.val[0] = 0; value.val[1] = 0; cvSet2D(H,u,v,value); } // Loc thong thap Butterworth

value.val[0] = 1.0/(1.0 + pow(Duv/D0,2.0*n)); value.val[1] = value.val[0]; cvSet2D(H,u,v,value); */ // Loc thong cao Gauss value.val[0] = 1.0 - exp(-Duv*Duv/(2*D0*D0)); value.val[1] = value.val[0]; cvSet2D(H,u,v,value); } IplImage *G = cvCreateImage(cvSize(Q,P),IPL_DEPTH_32F,2); cvMul(H,F,G); // Buoc 6: Bien doi Fourier nguoc, lay phan thuc va nhan voi (-1)^(x+y) cvDFT(G,G,CV_DXT_INVERSE_SCALE); for (x=0; x<P; x++) for (y=0; y<Q; y++) { value = cvGet2D(G,x,y); if ((x+y)%2 == 1) value.val[0] = -value.val[0]; value.val[1] = 0; cvSet2D(G,x,y,value); } // Buoc 7: Bo phan them vao for (x=0; x<M; x++) for (y=0; y<N; y++) { value = cvGet2D(G,x,y); cvSet2D(imgout,x,y,value); } cvReleaseImage(&F); cvReleaseImage(&H); cvReleaseImage(&G); return; }

Sign up to vote on this title
UsefulNot useful