Skip to content

高丽藏图片matlab投影切图方法 #2

@xianchun

Description

@xianchun

f = imread('gl_40_76.jpg'); %读原图文件
%f = imread('dh224_b.jpg');
figure(1);imshow(f); %显示原图
g = rgb2gray(f) %彩色图转换为灰度图
figure(2); imshow(g); %显示灰度图

level = graythresh(g); %设定灰度阈值
bw = ~im2bw(g,level); %灰度图转换为二值图
figure(4); imshow(bw); %显示二值图

s = size(bw); %计算矩阵大小
%求图像的列像素累加和
for i = 1:s(2)
gx(i) = 0;
for j = 1:s(1)
gx(i) = gx(i)+ bw(j,i);
end
end
x = 1:s(2);
%line(x, gx,'Color','b'); %显示求和曲线

windowSize = 15; %滤波窗宽
gy = filter(ones(1,windowSize)/windowSize,1,gx); %对曲线进行滑动平均滤波
gy = filter(ones(1,windowSize)/windowSize,1,gy); %再次滤波
gmax = find(diff(sign(diff(gy)))== -2)+1; %求极大值
%line(x, gy,'Color','r'); %绘滤波后的曲线
smax = size(gmax); %极大值点数
ly = 1:s(1) %绘切割竖线
for k = 1:smax(2)
if(k == 1) %左边第一列字两边都划线
lq(1) = gmax(k)-4*windowSize; %左边第一条竖线水平坐标
line(lq(1), ly,'Color','y'); %绘第一条竖线
lq(2) = gmax(k) + windowSize; %第二条竖线的水平坐标
line(lq(2), ly,'Color','y'); %绘第二条竖线
end

lq(k+1) = gmax(k) + windowSize;                        %其余竖线的水平坐标
line(lq(k+1), ly,'Color','y');                                     %绘其余的竖线

end

for k = 1:gmax+1 %列字条水平像素累加
for i = 1:s(1)
py(i) = 0;
for j = lq(k):lq(k+1)
py(i) = py(i)+ bw(i,j);
end
end
windowSize = 23; %滤波窗宽
py = filter(ones(1,windowSize)/windowSize,1,py); %对投影曲线滑动滤波
fpy = filter(ones(1,windowSize)/windowSize,1,py); %二次滤波
pmax = find(diff(sign(diff(fpy)))==- 2)+1; %求极大值
psmax = size(pmax); %极值点数

lx = lq(k):lq(k+1);
for m = 1:psmax(2)
    hq(m) = pmax(m);                                                     %横切线的竖直坐标
    line(lx, hq(m), 'Color','r');                                                    %绘横切割线
    hold on                                                                               %保持绘图
end   

end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions