我有一张黑白车牌图片。
图片如下:
现在我想给每个数字的背景上色,以便进一步裁剪车牌上的数字。
效果如下:
非常感谢您的帮助。
图片如下:
![enter image description here](https://istack.dev59.com/arVI6.webp)
效果如下:
![enter image description here](https://istack.dev59.com/Bsipn.webp)
生成盒子的一种简单方法是通过在每一列上求和你的图像,并查找总和下降到某个阈值以下的位置(即白色像素在该列中下降到给定数量以下的位置)。这将为您提供盒子应该放置的列索引。这些盒子的宽度可能太窄(即数字的小部分可能会露出侧面),因此您可以通过卷积指数向量与一个由1组成的小向量并查找结果大于零的值来膨胀边缘。以下是使用上述图像的示例:
rawImage = imread('license_plate.jpg'); %# Load the image
maxValue = double(max(rawImage(:))); %# Find the maximum pixel value
N = 35; %# Threshold number of white pixels
boxIndex = sum(rawImage) < N*maxValue; %# Find columns with fewer white pixels
boxImage = rawImage; %# Initialize the box image
boxImage(:,boxIndex) = 0; %# Set the indexed columns to 0 (black)
dilatedIndex = conv(double(boxIndex),ones(1,5),'same') > 0; %# Dilate the index
dilatedImage = rawImage; %# Initialize the dilated box image
dilatedImage(:,dilatedIndex) = 0; %# Set the indexed columns to 0 (black)
%# Display the results:
subplot(3,1,1);
imshow(rawImage);
title('Raw image');
subplot(3,1,2);
imshow(boxImage);
title('Boxes placed over numbers');
subplot(3,1,3);
imshow(dilatedImage);
title('Dilated boxes placed over numbers');
注意: 上面所做的阈值处理考虑到黑白图像可能是双精度类型(具有0或1的值)、逻辑类型(也具有0或1的值)或无符号8位整数类型(具有0或255的值)。你只需要将N
设置为用作识别包含数字部分的列的白色像素数量的阈值即可。
sum(rawImage)
的图形中相对随意地选择了它。你可以有很多自动选择的方法:例如图像行数的某个固定百分比(例如N = 0.875*size(rawImage,1);
),最小和最大列总和之间距离的一部分(例如s = sum(rawImage); N = round((min(s)+0.75*(max(s)-min(s)))/maxValue);
)等等。 - gnovice假设您有围绕字母的框 - 这给您整体角度
将图像折叠成1d(最好先旋转它,使边界框水平)
然后在此1d签名中查找字母之间的间隙,从而给出数字的位置。 如果您知道板的数字数量和格式,则会有所帮助。