MATLAB--数字图像处理 车牌识别之矫正车牌+分割字符

源代码

 Image = imread ('car4.jpg') ;
Image = imresize (Image, [300 450]) ; %调整图像的大小为300*450

gray = 2*Image (:, :, 3) - Image (:, :, 1) - Image (:, :, 2) ;
figure,imshow(gray),title('保留蓝色色域')

l = graythresh (gray) ; %利用Ostu法获取图像的阈值
bw = im2bw (gray, l) ; %根据Ostu法分割车牌图像
figure,imshow(bw),title('二值化后的图像')

bw1 = bwareaopen (bw, 500) ;%删除面积小于500的区域
figure,imshow (bw1) , title ('删除小面积区域后的图像') ;

se = strel ('cube', 15) ;%创建一个边长为15的正方形
bw2 = imclose (bw1, se) ;%闭运算连使整个车牌区域连接在一起
figure,imshow(bw2),title('进行闭运算后的车牌');


rotI = bw2;
 
% Create a binary image
BW = edge(rotI, 'canny'); 
figure,imshow(BW),title("提取边缘后");
se=strel('square',5);
BW=imdilate(BW,se);

% Create the hough transform using the binary image 
[H, T, R] = hough(BW); 
figure, imshow(H, [], 'XData', T, 'YData', R, 'InitialMagnification', 'fit'); 
xlabel('\theta'), ylabel('\rho'); 
axis on, axis normal, hold on;
colormap(gca, hot);
 
% Find peaks in the hough transform of the image
P = houghpeaks(H, 1); 
x = T(P(:,2));
y = R(P(:,1)); 
plot(x, y, 's', 'color', 'blue'); 
 
% Find lines and plot them
lines = houghlines(BW, T, R, P, 'FillGap', 5, 'MinLength', 7); 
figure, imshow(rotI), hold on; 
max_len = 0; 
for k = 1 : length(lines)  % here length(lines)=12
   xy = [lines(k).point1; lines(k).point2];    
   % Determine the endpoints of the longest line segment
   len = norm(lines(k).point1 - lines(k).point2); % distance between point1 and point2
   if ( len > max_len )
      max_len = len; 
      xy_long = xy; 
xy = [lines(k).point1; lines(k).point2]; 
   end
end
 plot(xy(:, 1), xy(:, 2), 'LineWidth', 2, 'Color', 'green');
   
   % Plot beginnings and ends of lines
   plot(xy(1,1), xy(1,2), 'x', 'LineWidth', 2, 'Color', 'yellow');
   plot(xy(2,1), xy(2,2), 'x', 'LineWidth', 2, 'Color', 'red'); 
bw2=imrotate(rotI,x-90);
Image=imrotate(Image,x-90);
figure,imshow(bw2),title('旋转后');

stats = regionprops (bw2, 'BoundingBox', 'Centroid') ; %获取车牌矩形区域, 即二值图中值为1的矩形框
bb = stats (1) .BoundingBox;%获取车牌矩形区域左上角坐标及矩形的长和宽, bb (1) 为矩形框左上角的y坐标, bb (2) 为矩形框左上角的x坐标
Img = Image (floor (bb (2) ) :floor (bb (2) +bb (4) ) ,floor (bb (1) ) :floor (bb (1) +bb (3) ) , :) ;
gray = Img (:, :, 2) ;%突出蓝色车牌的白色字体区域
figure,imshow (gray),title('切割图像 提取蓝色色域后')

l = graythresh (gray) ; %利用Ostu法获取阈值
bw3 = im2bw (gray, l) ; %转换为二值图像
figure,imshow (bw3),title('转换为二值图像')

se = strel ('line', 2, 90) ; %创建一个线条形状
bw4 = imclose (bw3, se) ;%闭运算, 连接各字符之间的缝隙
bw5 = imclearborder (bw4) ; %去除与边界相连的部分
bw6 = bwareaopen (bw5, 30) ; %删除面积小于50的区域
figure,imshow (bw6),title("切割图像二值化");
tem=imdilate(bw6,se);
tem=imdilate(tem,se);
tem=imdilate(tem,se);
tem=imdilate(tem,se);
figure,imshow(tem),title("腐蚀后");
stats = regionprops (tem, 'BoundingBox', 'Centroid') ; %获取各个字的矩形区域坐标
figure,imshow (Img)

for i = 1:length (stats)
bb = stats (i) .BoundingBox;
I = Img (floor (bb (2) ) :floor (bb (2) +bb (4) ) ,floor (bb (1) ) :floor (bb (1) +bb (3) ) , :) ;
str = ['第', num2str(i), '个字'];
figure,imshow (I) ;%展示每个字符
end

你可能感兴趣的:(MATLAB--数字图像处理)