![]() |
#2
xiaoduzi2013-07-22 14:05
回复 楼主 xiaoduzi
|
现在已经能够旋转好了,附件3是目前能够实现的情况,附件4是最后处理成的样子,但是怎么把图片提取出来,去掉白纸部分啊,急求!
下面是我的代码,有人说只需要在后面加标记就可以显示了,加标记就是把灰度矩阵周围的全零行去掉吗?我是先手,不是很懂,但是这个很急,还请指教啊
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录

clear all
close all
Img=imread('d.jpg');
figure;imshow(Img);
Im=rgb2gray(Img);
bw=edge(Im,0.01);
figure;imshow(bw);
[H, T, R] = hough(bw);
P = houghpeaks(H, 4, 'threshold', ceil(0.3*max(H(:))));
x=T(P(:,2));y=R(P(:,1));
lines = houghlines(bw, T, R, P, 'FillGap', 20, 'MinLength', 2);%%合并距离小于50的线段,丢弃所有长度小于7的直线段
figure;imshow(bw);hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
%如果要求霍夫变换的效果的话 ,这里就不能单纯的比较线段的长短的方法来确定最长直线,
%要用sort排序的方法,这里简洁了,
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
max_len = 0; % 最大直线长度
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
%highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
figure ;imshow(H, [], 'XData', T, 'YData', R, 'InitialMagnification', 'fit');%fit参数就是把整个图片填充在当前窗口里
colormap('hot')
axis on
axis square
xlabel('\theta'); ylabel('\rho');
axis on; axis normal; title('霍夫变换域', 'FontWeight', 'Bold')
figure;imshow(Img); title('区域标识图像33333', 'FontWeight', 'Bold');
hold on;
% 强调最长的部分
plot(xy_long(:,1), xy_long(:,2), 'LineWidth', 2, 'Color', 'r');
x1 = xy_long(:, 1);
y1 = xy_long(:, 2);
% 求得线段的斜率
K1 = -(y1(2)-y1(1))/(x1(2)-x1(1));
angle = atan(K1)*180/pi;
Img = imrotate(Img, -90-angle, 'bilinear');
bw = imrotate(bw, -90-angle, 'bilinear');
figure; imshow(Img, []); title('原二值图像', 'FontWeight', 'Bold');
figure; imshow(bw, []); title('校正二值图像', 'FontWeight', 'Bold');
SE=strel('rectangle',[4,4]);
bw=imdilate(bw,SE);
bw=bwfill(bw,'holes');
figure;imshow(bw);
bw = bwareaopen(bw, 4000);
figure;imshow(bw);
%标记,回复原图
close all
Img=imread('d.jpg');
figure;imshow(Img);
Im=rgb2gray(Img);
bw=edge(Im,0.01);
figure;imshow(bw);
[H, T, R] = hough(bw);
P = houghpeaks(H, 4, 'threshold', ceil(0.3*max(H(:))));
x=T(P(:,2));y=R(P(:,1));
lines = houghlines(bw, T, R, P, 'FillGap', 20, 'MinLength', 2);%%合并距离小于50的线段,丢弃所有长度小于7的直线段
figure;imshow(bw);hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
%如果要求霍夫变换的效果的话 ,这里就不能单纯的比较线段的长短的方法来确定最长直线,
%要用sort排序的方法,这里简洁了,
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
max_len = 0; % 最大直线长度
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
%highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','blue');
figure ;imshow(H, [], 'XData', T, 'YData', R, 'InitialMagnification', 'fit');%fit参数就是把整个图片填充在当前窗口里
colormap('hot')
axis on
axis square
xlabel('\theta'); ylabel('\rho');
axis on; axis normal; title('霍夫变换域', 'FontWeight', 'Bold')
figure;imshow(Img); title('区域标识图像33333', 'FontWeight', 'Bold');
hold on;
% 强调最长的部分
plot(xy_long(:,1), xy_long(:,2), 'LineWidth', 2, 'Color', 'r');
x1 = xy_long(:, 1);
y1 = xy_long(:, 2);
% 求得线段的斜率
K1 = -(y1(2)-y1(1))/(x1(2)-x1(1));
angle = atan(K1)*180/pi;
Img = imrotate(Img, -90-angle, 'bilinear');
bw = imrotate(bw, -90-angle, 'bilinear');
figure; imshow(Img, []); title('原二值图像', 'FontWeight', 'Bold');
figure; imshow(bw, []); title('校正二值图像', 'FontWeight', 'Bold');
SE=strel('rectangle',[4,4]);
bw=imdilate(bw,SE);
bw=bwfill(bw,'holes');
figure;imshow(bw);
bw = bwareaopen(bw, 4000);
figure;imshow(bw);
%标记,回复原图
[ 本帖最后由 xiaoduzi 于 2013-7-21 21:30 编辑 ]