Giáo trình xử lý ảnh y tế Tập 3 P16 pot

9 252 0
Giáo trình xử lý ảnh y tế Tập 3 P16 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

375 printf("\n %d derivative=%f error=%f ", k,der,delta); if(fabs(delta)>=500.0) { printf("\n A numerical problem was encountered."); printf("\n Restart problem with a different choice."); exit(1); } r[0]-delta/der; } gotoxy(70,25); textattr(WHITE+(BLACK<<4)); cputs(" "); gotoxy(xt,yt+2); switch(ch) { case '1': case '3': printf("\n\n -%c %9.6f ",(char)236,r[0]); break; case '2': printf("\n\n %9.6f %9.6f ",d[0],r[0]); } for(i=1;i<N;i++) printf("\n %9.6f %9.6f ",d[i],r[i]); switch(ch) { case '1': case '3': printf("\n +%c",(char)236); break; case '2': printf("\n %9.6f ",-d[0]); } printf("\nEnter file name for storing quantizer >"); scanf("%s",file_name); fptr=fopen(file_name,"w"); for(i=0;i<N;i++) fprintf(fptr,"%f %f \n",d[i],r[i]); fclose(fptr); 376 } double p(double x) { double prob; switch(ch) { case '1': prob=exp(-x*x*0.5); break; case '2': prob=1.0; break; case '3': prob=exp(-1.4142136*fabs(x)); break; } return prob; } #define EPS 1.0e-6 /* Calculating the next decision level, given values for d[i-11 and r[i-1]. */ double decision_level(double di_1, double ri_1) { double delta,di,fun,dfun,ff,area1,area2; int i; if(fabs(di_1)>10.0) delta=2.0; else delta=0.1; di=ri_1+delta; i=0; /* Using Newton-Raphson's method for root finding. */ while(fabs(delta)>=EPS) { i++; if(i>=1000) { printf("\n no convergence.\n"); return di; } area1=Romberg(di-1,di,f1); area2=Romberg(di-1,di,f2); 377 ff=area1/area2; fun=(ri_1)-ff; if(fabs(area2)<=1.0e-10) { printf("\n A numerical problem was encountered."); printf("\n Restart problem with a different choice."); exit(1); } dfun=-p(di)*(di-ff)/area2; if(fabs(dfun)<=1.0e-10) { gotoxy(1,20); printf("\n derivative=%f",dfun); printf("\n A numerical problem was encountered."); printf("\n Restart problem with a different choice."); exit(1); } delta=-fun/dfun; di+=delta; } return di; } /* functions used by numerical integration routine. */ double f1(double y) { return (y*(p(y))); } double f2(double y) { return p(y); } /* Numerical integration using Romberg method. */ double Romberg(double di_1,double di,double (*f)(double)) { 378 double T[10][10],h,x,Area; int N,k,kk,i,j; N=9; k=1 ; h=di- di_1; T[0][0]=h*((*f)(di_1)+(*f)(di))/2.0; for(i=1;i<=N;i++) { k<<=1; h/=2.0; x=di_1 - h; Area=0.0; T[0][i]=(T[0][i-1])/2.0; for(j=0;j<(k-1);j+=2) { x+=2.0*h ; Area+=(*f)(x); } T[0][i]+=Area*h; kk=1; for(j=1;j<=i;j++) { kk<<=2; T[j][i]=(kk*T[j-1][i]-T[j-1][i-1])/(float)(kk- 1); } } return T[N][N]; } Bài tập 13.8 1. Chạy chương trình 13.9 dùng các lựa chọn sau: 3 bit, 4 bit, 5 bit, 6 bit. 2. Với mỗi lựa chọn số bit ta chọn: Gauss. Đồng đều. Laplace. 3. Các mức lượng tử có thể dùng cho thiết kế có trên đĩa, ví dụ dùng Q3G.DAT cho lượng tử 3 bit dùng lượng tử Gauss, Q4L.DAT cho 4 bit lượng tử Laplace. Phương pháp Lloyd Lloyd cũng đưa ra một phương pháp thứ hai cho phép xác định các mức lượng tử mà ông gọi là phương pháp Lloyd I. Phương 379 pháp này có nhiều ưu điểm hơn phương pháp II (giải thuật Lloyd-Max), vì nó dễ dàng cho tính toán và các vector lượng tử hoá có thể mở rộng. Chú ý là vấn đề mà chúng ta quan tâm ở đây là khoảng cách lượng tử hoá, lượng tử hoá của hàm một biến đã biết được phân tán. Vector lượng tử hoá là một vector của nhiều biến mà với các biến này ta đã biết được phân tán. Thuật toán Lloyd theo các bước sau: 1. Rút ra ước lượng cho phạm vi của các biến d i {i = 0, 1, 2, , N} (Một ước lượng có thể rút ra bằng cách dùng các giá trị từ lượng tử hoá đồng đều hoặc từ các mức lượng tử trước mà ta cần một kết quả tốt hơn). 2. Đặt một biến D 1 = 0. D 1 dùng để lưu lại tình trạng không chính xác lúc trước. 3. Tính      1 1 )( )( i i i i d d d d i dyyp dyyyp r i = 0, 1, , N - 1. 4. Tính d r r i i i   1 2 i = 0, 1, , N - 1 5. Tính tình trạng không chính xác       1 0 2 2 1 )()( N k d d k k k dyypryD Có thể dễ dàng mở rộng biểu thức trạng thái không chính xác theo             1 0 22 111 )()(2)( N k d d d d k d d k k k k k k k dyyprdyyyprypyD (13.58) 6. Nếu D D D 2 1 1    thì một giải pháp đã được tìm ra. Lưu lại kết quả và thoát khỏi chương trình. 7. Đặt D 1 = D 2 8. Quay lại bước 3. Một chương trình C cho giải thuật trên được đề cập đến ở dưới đây. 380 Chương trình 13.10 “LLOYDQ.C” Thuật toán Lloyd cho việc thiết kế các mức lượng tử. /*Program13.10 "LLOYDO.C".Lloyd algorithm for quantizer design.*/ /* Program for designing the Lloyd-quantizer for a Gauss, uniform or Laplace distribution.*/ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <alloc.h> #include <conio.h> double decision_level(double, double); double f1(double); double f2(double); double f3(double); double p(double); double Romberg(double, double, double (*)(double)); char ch; void main( ) { double *r,*d,step,sum,I1,I2,I3,D1,D2; int i,j,m,N,xt,yt,niter,ind; char file_name[16],ch1; FILE *fptr; clrscr( ) ; printf("Enter number of bits >"); scanf("%d",&m); printf("Enter number of iterations >"); scanf("%d",&niter); N=1<<m; r=(double *)malloc((N+1)*sizeof(double)); d=(double *)malloc((N+1)*sizeof(double)); printf("Enter choice of distribution:"); printf("\n 1. Gauss."); printf("\n 2. Uniform."); 381 printf("\n 3. Laplace. >(1,2 or 3): "); while(((ch=getch())!='1')&&(ch!='2')&&(ch!='3')); putch(ch); printf("\n Do you wish to start from a previous design? (y or n) >"); while(((ch1=getch())!='y')&&(ch1!='n')); putch(ch1); ind=0; switch(ch1) { case 'y': printf("\n Enter name Of file containing quantizer data Y"); scanf("%s",file_name); fptr=fopen(file_name,"r"); if(fptr==NULL) { printf("\n No such file."); ind=1; } if(ind!=1) { for(i=0;i<N;i++) fscanf(fptr,"%f %f ",&d[i],&r[i]); d[N]=20.0; fclose(fptr); break; } case 'n': d[N]=20.0; d[0]=-20.0; if(ch=='2') {d[N]=1.0; d[0]=-1.0;} step=(d[N]-d[0])/(float)N; for(i=1;i<N;i++) d[i]=d[i-1]+step; } D1=0.0; for(j=0;j<niter;j++) { gotoxy(1,15); 382 printf("iter=%d",j); D2=0.0; for(i=0;i<N;i++) { I1=Romberg(d[i],d[i+1],f3), I2=Romberg(d[i],d[i+1],f1); I3=Romberg(d[i],d[i+1],f2); r[i]=I2/I3; D2+=I1-2.0*r[i]*I2+r[i]*r[i]*13; } for(i=1;i<N;i++) d[i]=(r[i]+r[i-1])/2.0; printf("\nDistortion=%lf",D2); if(fabs((D2-D1)/D2)<=1.e-6) { printf("\n\nConvergence was reached.\n"); break; } D1=D2; } switch(ch) { case '1': case '3': printf("\n\n -%c %9.6f ",(char)236,r[0]); break; case '2': printf("\n\n %9.6f %9.6f ", d[0], r[0]); } for(i=1; i<N; i++) printf("\n %9.6f %9.6f ",d[i],r[i]); switch(ch) { case '1': case '3': printf("\n +%c",(char)236); break; case '2': printf("\n %9.6f ",-d[0]); } printf("\nEnter file name for storing quantizer- ->"); 383 scanf("%s",file_name); fptr=fopen(file_name,"W"); for(i=0; i<N; i++) fprintf(fptr,"%f %f \n",d[i], r[i]); fclose(fptr); } double p(double x) { double prob; switch(ch) { case '1': prob=exp(-x*x*0.5); break; case '2': prob=1.0; break; case '3': prob=exp(-1.4142136*fabs(x)); break; } return prob; } /* functions used by numerical integration routine. */ double f1(double y) { return (y*(p(y))); } double f2(double y) { return p(y); } double f3(double y) { return y*y*p(y); } Bài tập 13.10 .       1 0 2 2 1 )()( N k d d k k k dyypryD Có thể dễ dàng mở rộng biểu thức trạng thái không chính xác theo             1 0 22 111 )()(2)( N k d d d d k d d k k k k k k k dyyprdyyyprypyD ( 13. 58) 6 chương trình. 7. Đặt D 1 = D 2 8. Quay lại bước 3. Một chương trình C cho giải thuật trên được đề cập đến ở dưới đ y. 38 0 Chương trình 13. 10 “LLOYDQ.C” Thuật toán Lloyd cho việc. y) { return (y* (p (y) )); } double f2(double y) { return p (y) ; } double f3(double y) { return y* y*p (y) ; } Bài tập 13. 10

Ngày đăng: 10/07/2014, 22:20

Tài liệu cùng người dùng

Tài liệu liên quan