资源描述:
《基于MATLAB的图像处理方法进行面积计算》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、%EditBy:DTL%%%%图片颜色均衡x_min=58;x_max=1439;y_min=1;y_max=853;valid_pix_cnt=0;%浅色区域像素点个数计数%原始图像I=imread('ini_p2.png');figure(1)imshow(I);sum_pix=(x_max-x_min+1)*(y_max-y_min+1);mean_R=sum(sum(I(y_min:y_max,x_min:x_max,1)))/sum_pix;mean_G=sum(sum(I(y_min:y_max,x_min:x_max,2)))/sum_pix;mean_B=sum(su
2、m(I(y_min:y_max,x_min:x_max,3)))/sum_pix;I(249:370,423:595,1)=mean_R-5;I(249:370,423:595,2)=mean_G-5;I(249:370,423:595,3)=mean_B-5;%%%分块均衡Lx=150;Ly=100;blk_pix=Lx*Ly;x_inc=15;y_inc=9;x_cnt=floor((x_max-x_min-Lx+1)/x_inc)+1;y_cnt=floor((y_max-y_min-Ly+1)/y_inc)+1;I_blc=I;%均衡处理forx_blc=0:x_cntfor
3、y_blc=0:y_cntx_start=x_min+x_blc*x_inc;x_end=x_min+x_blc*x_inc+Lx-1;if(x_end>=x_max)x_start=x_max-Lx+1;x_end=x_max;endy_start=y_min+y_blc*y_inc;y_end=y_min+y_blc*y_inc+Ly-1;if(y_end>=y_max)y_start=y_max-Ly+1;y_end=y_max;endblk_mean_R=sum(sum(I(y_start:y_end,x_start:x_end,1)))/blk_pix;blk_mean_G
4、=sum(sum(I(y_start:y_end,x_start:x_end,2)))/blk_pix;blk_mean_B=sum(sum(I(y_start:y_end,x_start:x_end,3)))/blk_pix;I_blc(y_start:y_end,x_start:x_end,1)=I(y_start:y_end,x_start:x_end,1)*(mean_R/blk_mean_R);I_blc(y_start:y_end,x_start:x_end,2)=I(y_start:y_end,x_start:x_end,2)*(mean_G/blk_mean_G);I
5、_blc(y_start:y_end,x_start:x_end,3)=I(y_start:y_end,x_start:x_end,3)*(mean_B/blk_mean_B);endendfigure(2)imshow(I_blc);title(['均衡处理(Lx='num2str(Lx)';Ly='num2str(Ly)';x_inc='num2str(x_inc)';y_inc='num2str(y_inc)';)']);%%I_ext=I_blc;%阈值设定ThR1=190;ThG1=128;ThB1=128;x_start1=x_min;x_end1=x_max;y_s
6、tart1=y_min;y_end1=y_max;%处理区域1fory=x_start1:x_end1forx=y_start1:y_end1if(I_blc(x,y,1)>ThR1&&I_blc(x,y,2)>ThG1&&I_blc(x,y,3)>ThB1)I_ext(x,y,1)=0;I_ext(x,y,2)=255;I_ext(x,y,3)=0;endendendfigure(7)imshow(I_ext)%%%去除零散点cnt=0;I_ext2=I_ext;fory=x_start1+1:x_end1-1forx=y_start1+1:y_end1-1if(I_ext(x,y
7、,1)==0&&I_ext(x,y,2)==255&&I_ext(x,y,3)==0)cnt=cnt+1;endif(I_ext(x+1,y,1)==0&&I_ext(x+1,y,2)==255&&I_ext(x+1,y,3)==0)cnt=cnt+1;endif(I_ext(x,y+1,1)==0&&I_ext(x,y+1,2)==255&&I_ext(x,y+1,3)==0)cnt=cnt+1;endif(I_ext(x+1,y+1,1)==0&&I_