clc;
clear;
close all;
%蓝色:红色分量0到80 绿色分量20到105 蓝色分量70到170
%蓝色/红色>1 蓝色/绿色>1 红色/绿色<1
%白色标准差小于15 三种颜色均大于100
[fn,pn,fi]=uigetfile('*.*','选择图片');%fn为返回的文件名;pn为返回的文件的路径名;fi为选择的文件类型
I=imread([pn fn]);%figure,imshow(I);title('原始图像');%显示原始图像
tic
G=fspecial('gaussian',5);
I=imfilter(I,G,'same');
Ib=I;
imshow(I);
I2=rgb2hsv(I);%I(:,:,1)值从 0 到 1,对应于颜色在颜色圈上的位置。随着色调从 0 增加到 1
%颜色从红色过渡到橙色、黄色、绿色、青色、蓝色、品红色,最后又回到红色。
%I(:,:,2)色调的量或距离中性色的量。0 表示中性色,1 表示最大饱和度。
%I(:,:,3)特定颜色的红色、绿色和蓝色分量的最大明度
[a,b]=find((I2(:,:,1)<0.75)&(I2(:,:,1)>0.59)&(I2(:,:,2)>0.5)&I2(:,:,3)>0.2);%0.56-0.78
% I(a,b,:)=0;
cz=size(a);
for i=1:cz(1)
I(a(i),b(i),:)=0;
end
Ic=Ib-I;
figure,imshow(Ic);
% [m,n,~]=size(I2);
% z=[];z2=[];
% for i=1:m
% for j=1:n
% if I2(i,j,1)<0.75&&I2(i,j,1)>0.59&&I2(i,j,2)>0.4&&I2(i,j,3)>0.2
% z=[z,i];z2=[z2,j];
% end
% end
% end
% cz=size(z);
% for i=1:cz(2)
% I(z(i),z2(i),:)=0;
% end
% Ic=Ib-I;
% figure,imshow(Ic);
I3=rgb2gray(Ic);
figure,imshow(I3);
I3=imadjust(I3,[0.05,0.1],[0,1]);
figure,imshow(I3);
se=strel('square',18);
I3=imclose(I3,se);
I4=im2bw(I3);
figure,imshow(I4);
% I42=imclearborder(I4,8);
% figure('name','ceshi'),imshow(I42);
[lm,on]=bwlabel(I4,8);
status=regionprops(lm,'area','image');
for i=1:on
if status(i).Area>=100&&size(status(i).Image,1)>5&&size(status(i).Image,2)>5
lmx=lm;
lmx(lmx~=i)=0;
[r,c]=find(lmx==i); %找出矩阵中非零元素所在行和列,并存在r,c中
[rectx,recty,area,perimeter,ckb] = minboundzsj(c,r,'a');
if ckb>3&&ckb<5
rect=[rectx,recty];
if recty(2)==recty(1)||rectx(2)==rectx(1)
degree=0;
else
if norm(rect(2,:)-rect(1,:))>=norm(rect(3,:)-rect(2,:))
degree=-rad2deg(atan((recty(2)-recty(1))/(rectx(2)-rectx(1))));
else
degree=-(rad2deg(atan((recty(2)-recty(1))/(rectx(2)-rectx(1))))-90);
end
end
figure,imshow(Ib);
hold on
% scatter(rectx(3),recty(3))
% scatter(rectx(1),recty(1))
% scatter(rectx(1),1);
% scatter(1356.6,1)
line(rectx,recty);
end
end
end
bw=imrotate(Ib,-degree,'bilinear','loose');
figure,imshow(bw);title('倾斜校正');
[om, on, o] = size(I);
new_m = ceil(abs(om*cosd(degree)) + abs(on*sind(degree)));%ceil朝无穷大方向取整
new_n = ceil(abs(on*cosd(degree)) + abs(om*sind(degree)));
m1 = [1 0 0; 0 -1 0; -0.5*on 0.5*om 1];
m2 = [cosd(degree) -sind(degree) 0; sind(degree) cosd(degree) 0; 0 0 1];
m3 = [1 0 0; 0 -1 0; 0.5*new_n 0.5*new_m 1];
rect=[rect,ones(5,1)];
xrect=rect*m1*m2*m3;
figure,imshow(bw);
hold on
line(xrect(:,1),xrect(:,2));
xrect2=eval(vpa(xrect,6));
xrectx=unique(xrect2(:,1));
xrecty=unique(xrect2(:,2));
width=abs(xrectx(2)-xrectx(1));
height=abs(xrecty(2)-xrecty(1));
Icp=imcrop(bw,[min(xrect2(:,1)) min(xrect2(:,2)) width height]);
figure,imshow(Icp);
% figure,imshow(bw);
% hold on
% scatter(min(xrect2(:,1)),max(xrect2(:,2)))
% [om, on, ~] = size(I);
% new_m = ceil(abs(om*cosd(degree)) + abs(on*sind(degree)));%ceil朝无穷大方向取整
% new_n = ceil(abs(on*cosd(degree)) + abs(om*sind(degree)));
% rect=[rect,ones(5,1)];
% rm1 = [1 0 0; 0 -1 0; -0.5*new_n 0.5*new_m 1];
% rm2 = [cosd(degree) sind(degree) 0; -sind(degree) cosd(degree) 0; 0 0 1];
% rm3 = [1 0 0; 0 -1 0; 0.5*on 0.5*om 1];
% for i = 1:new_n
% for j = 1:new_m
% % rotated image's coordinates to no-rotation image's coordinates
% old_coordinate = [i j 1]*rm1*rm2*rm3;
% col = round(old_coordinate(1));
% row = round(old_coordinate(2));
% % prevent border overflow
% if row < 1 || col < 1 || row > om || col > on
% new_img_nnp(j, i) = 0;
% new_img_lp(j, i) = 0;
% else
% % nearest neighbor interpolation
% new_img_nnp(j, i, 1) = I(row, col, 1);
% new_img_nnp(j, i, 2) = I(row, col, 2);
% new_img_nnp(j, i, 3) = I(row, col, 3);
%
% % bilinear interpolation
% % left = floor(col);
% % right = ceil(col);
% % top = floor(row);
% % bottom = ceil(row);
% %
% % a = col - left;
% % b = row - top;
% % new_img_lp(j, i, 1) = (1-a)*(1-b)*I(top, left, 1) + a*(1-b)*I(top, right, 1) + ...
% % (1-a)*b*I(bottom, left, 1) + a*b*I(bottom, right, 1);
% % new_img_lp(j, i, 2) = (1-a)*(1-b)*I(top, left, 2) + a*(1-b)*I(top, right, 2) + ...
% % (1-a)*b*I(bottom, left, 2) + a*b*I(bottom, right, 2);
% % new_img_lp(j, i, 3) = (1-a)*(1-b)*I(top, left, 3) + a*(1-b)*I(top, right, 3) + ...
% % (1-a)*b*I(bottom, left, 2) + a*b*I(bottom, right, 3);
% end
% end
% end
% figure, imshow(new_img_nnp/255), title('nearest neighbor interpolation');
% % figure, imshow(new_img_lp/255), title('bilinear interpolation');
% csx=[2 4 3 1 2];csy=[1 3 4 2 1];
% figure,line(csx,csy)
% status=regionprops(lm,'area','boundingbox','image');
% figure,imshow(I4);
% xlabel('最小外接矩形');
% fhtjz=[];
% for i=1:n
% mianji=status(i).BoundingBox(3)*status(i).BoundingBox(4);
% if mianji>=100
% % rectangle('position',status(i).BoundingBox,'edgecolor','r');
% fhtjz=[fhtjz,i];
% end
% end
% cx=size(fhtjz,2);
% i=1;
% for i=1:cx
% Icl=im2double(status(i).Image);
% Icl2=padarray(Icl,[round(size(Icl,1)/2) round(size(Icl,2)/2)],'both');
% [r,c]=find(Icl2==1); %找出矩阵中非零元素所在行和列,并存在r,c中
% [rectx,recty,area,perimeter] = minboundzsj(c,r,'a');
% figure,imshow(Icl2);
% hold on
% line(rectx,recty);
% end
% status=regionprops(l,'BoundingBox');%返回一个长度4的数组,分别表示矩形的左上角点坐标和矩形的长宽
% figure,imshow(I4);
% xlabel('最小外接矩形');
% for i=1:n
% rectangle('position',status(i).BoundingBox,'edgecolor','r');
% end
% fkcbl=[];
% for i=1:n
% kcbl(i)=status(i).BoundingBox(3)/status(i).BoundingBox(4);%宽长比例
% if kcbl(i)>3&&kcbl(i)<4
% fkcbl=[fkcbl,i];
% end
% end
% cx=size(fkcbl,2);
% figure,imshow(I4);
% xlabel('合适的最小外接矩形');
% for i=1:cx
% rectangle('position',status(fkcbl(i)).BoundingBox,'edgecolor','r');
% end
% [r,cv]=find(I4==1); %找出矩阵中非零元素所在行和列,并存在r,cv中
% [rectx,recty,area,perimeter] = minboundzsj(cv,r,'a');
% figure,imshow(I4);
% hold on
% line(rectx,recty);
% I4=edge(I3,'sobel','horizontal');
% BW = edge(I,'sobel',thresh,direction) %根据所指定的敏感度阈值thresh,在所指定的方向direction上,用Sobel算子进行边缘检测。
% Direction可取的字符串值为horizontal(水平方向)、vertical(垂直方向)或both(两个方向)
% figure,imshow(I4);
toc
Lp.zip_车牌定位
版权申诉
178 浏览量
2022-09-20
10:28:16
上传
评论
收藏 8.97MB ZIP 举报
周楷雯
- 粉丝: 80
- 资源: 1万+
最新资源
- Elasticsearch实战
- Screenshot_2024-06-05-21-20-09-259_net.csdn.csdnplus.jpg
- miflash_unlock.zip
- stream.x64.x-none.rarstream.x64.x-none.rarstream.x64.x-none.rars
- MAVEN 教程和详细讲解
- 目标检测高空拍摄道路小车轿车检测数据集601张VOC+YOLO格式.zip
- 智慧旅游 大屏模板 静态模板
- JSP+sql网络远程作业处理系统
- 2024年全国一卷高考数学等3个文件(1).zip
- 实战自学python如何成为大佬(目录):https://blog.csdn.net/weixin-67859959/artic
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈