clear;close all RGB = imread('E:\6.jpg'); [r,c]=size(RGB); f=RGB(round(0.3*r):r,:,:); figure(1), subplot(2,3,2),imshow(f),title('原始RGB'); %将RGB转换为YCbCr g = rgb2ycbcr(f); %YCbCr変換 Y =g(:,:,1); %Y成分 Cb = g(:,:,2); %Cb成分 Cr=g(:,:,3); %Cr成分 figure(1);subplot(2,3,3);imshow(f);title('原始图像'); subplot(2,3,4);imshow(Y);title('Y成分'); subplot(2,3,5);imshow(Cb);title('Cb成分'); subplot(2,3,6);imshow(Cr);title('Cr成分'); %三通道灰度直方图 [m,n,p]=size(Cb); %对于二维数组来说,它的size是指它的行数和列数;三维数组是指它的各个维的维度,可以理解为m行,n列和p层,即一共有m*n*p个元素 a=zeros(1,256); %a为一个1行,256列的全0矩阵 b=zeros(1,256); c=zeros(1,256); for i=1:m %从行开始 for j=1:n %每一行的列 a(f(i,j,1)+1)=a(f(i,j,1)+1)+1; %f(i,j,1)是Y通道的值,这个值有0-255的取值,a数组正好是容量256的数组,数组的下标范围是1开始的,所以要加1 end end for i=1:m for j=1:n b(f(i,j,2)+1)=b(f(i,j,2)+1)+1;%G通道 end end for i=1:m for j=1:n c(f(i,j,3)+1)=c(f(i,j,3)+1)+1;%B通道 end end %显示各通道灰度值 figure(2);subplot(2,2,1);bar(a); title('Y分量的灰度直方图') xlabel('灰度值') ylabel('像素数') figure(2); subplot(2,2,2); bar(b); title('Cb分量的灰度直方图') xlabel('灰度值') ylabel('像素数') subplot(2,2,3);bar(c); title('Cr分量的灰度直方图') xlabel('灰度值') ylabel('像素数') figure(3),subplot(2,2,2),imshow(g),title('g'), threshold=im2bw(Y,150/255); subplot(2,2,1),imshow(threshold),title('g二值化'); erodeElement = strel('square', 2) ; T = imerode(threshold,erodeElement); T=imfill(T,'holes'); subplot(2,2,3),imshow(T),title('形态学处理'); I=threshold-T; figure(4), imshow(I);title('形态学边缘提取'); figure (6) imshow(I); hold on %直线检测与绘制 %得到霍夫空间 [H1,T1,R1] = hough(I,'RhoResolution',2,'Theta',-90:0.5:89); %求极值点 Peaks=houghpeaks(H1,10); %得到线段信息 lines=houghlines(I,T1,R1,Peaks); %绘制线段 for k=1:length(lines) xy=[lines(k).point1;lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',3); end hold off b=[lines.theta;lines.rho] figure(7),uitable('Data',b);