二值图像最小外接矩形(正)

实现二值图像最小外接矩形(正)
用于目标识别 散点图画框
在这里插入图片描述

在这里插入图片描述

% 原文链接:https://blog.csdn.net/rosfreshman/article/details/116380981

原文效果如下
在这里插入图片描述
在这里插入图片描述

main.m 旋转角度固定为0° 可以得到平行于坐标轴的矩形

%% 寻找图像最小外接矩形  白色画框
% 先将图像变为二值图
% 计算最小外接矩形(MER)的一种方法是,将物体的边界以每次以一定的角度增量(例如3°)在90°范围内旋转。
% 每旋转一次记录一次其坐标系方向上的外接矩形边界点的最大和最小x、y值。
% 旋转到某一个角度后,外接矩形的面积(或周长)达到最小。取面积最小的外接矩形的参数为主轴意义下的长度和宽度。

clc;clear;close all;

%% 读取rgb原图
%img =  imread('2.png');
%img = rgb2gray(img); %灰度化
%thresh = graythresh(img );  %自动阈值
%img  = im2bw(img ,thresh );       %对图像二值化


%或者直接读取二值图像
img =  imread('2.png');
img =imcomplement(img);%二值图像反相
imgTh = im2uint8(img);
figure(1);
subplot(1,2,1);
imshow(img);title('原图像');

%%%旋转图像求其MER
% for angle = 0 : 1 : 90
for angle = 0 
    imgRotated = double(imrotate(imgTh,angle,'bicubic','loose'));  %求旋转后的图像
    %imshow(uint8(imgRotated));
    [row, col] = size(imgRotated);

    %%%判断最小外接矩形的边界
    for i = 1 : row
        if sum(imgRotated(i, :)) > col
            break;
        end
    end
    yMinTest = i;

    for i = row : -1 : 1
        if sum(imgRotated(i, :)) > col
            break;
        end
    end
    yMaxTest = i;

    for i = 1 : col
        if sum(imgRotated(:, i)) > row
            break;
        end
    end
    xMinTest = i;

    for i = col : -1 : 1
        if sum(imgRotated(:, i)) > row
            break;
        end
    end
    xMaxTest = i;
    %%%判断最小外接矩形的边界
    
    %%%计算面积
    XLU = xMinTest * cosd(angle) - yMinTest * sind(angle);
    YLU = xMinTest * sind(angle) + yMinTest * cosd(angle);

    XLD = xMinTest * cosd(angle) - yMaxTest * sind(angle);
    YLD = xMinTest * sind(angle) + yMaxTest * cosd(angle);

    XRU = xMaxTest * cosd(angle) - yMinTest * sind(angle);
    YRU = xMaxTest * sind(angle) + yMinTest * cosd(angle);

    XRD = xMaxTest * cosd(angle) - yMaxTest * sind(angle);
    YRD = xMaxTest * sind(angle) + yMaxTest * cosd(angle);
        
    l1 = sqrt((XLU - XRU) ^ 2 + (YLU - YRU) ^ 2);
    l2 = sqrt((XLU - XLD) ^ 2 + (YLU - YLD) ^ 2);


    nowSize = l1 * l2;
    %%%保存当前求得的MER
    if angle == 0 || nowSize < typicalSize
        xMin = xMinTest;
        yMin = yMinTest;
        xMax = xMaxTest;
        yMax = yMaxTest;
        typicalSize = nowSize;
        typicalAngle = angle;
        typicalImg = imgRotated;
    end
    %%%保存当前求得的MER
    lastSize = nowSize;
end

%%%重现
XLU = xMin * cosd(typicalAngle) - yMin * sind(typicalAngle);
YLU = xMin * sind(typicalAngle) + yMin * cosd(typicalAngle);

XLD = xMin * cosd(typicalAngle) - yMax * sind(typicalAngle);
YLD = xMin * sind(typicalAngle) + yMax * cosd(typicalAngle);

XRU = xMax * cosd(typicalAngle) - yMin * sind(typicalAngle);
YRU = xMax * sind(typicalAngle) + yMin * cosd(typicalAngle);

XRD = xMax * cosd(typicalAngle) - yMax * sind(typicalAngle);
YRD = xMax * sind(typicalAngle) + yMax * cosd(typicalAngle);
%%%重现

out1 = imrotate(imgTh,typicalAngle,'bicubic','loose');
rectx = [xMin; xMax; xMax; xMin; xMin];
recty = [yMin; yMin; yMax; yMax; yMin];
subplot(1,2,2);
imshow(out1);
% title(['旋转角度为',num2str(typicalAngle),'°']);
title(['最小外接矩形(正)']);
line(rectx(:),recty(:),'color','r');
[wei, hei] = minboxing(rectx(1:end-1),recty(1:end-1)) %显示外接矩形长和宽

在这里插入图片描述
代码只是能用,后面可能会再进一步改写。

  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶叶梓梓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值