天天看点

MATLAB中求图像中某一区域的质心

在《MATLAB图像处理宝典》,秦襄培 编。384页,有 一个regionprops函数,这个实现对二值图的多种描述。

以下内容http://blog.163.com/[email protected]/blog/static/3533324620119188314745/

MATLAB中求图像中某一区域的质心  

clear;clc;close all

%%读入图像

I_gray=imread('130.bmp');

level=graythresh(I_gray);  %%求二值化的阈值

[height,width]=size(I_gray);

bw=im2bw(I_gray,level);    %%二值化图像

figure(1),imshow(bw);      %%显示二值化图像

[L,num]=bwlabel(bw,8);     %%标注二进制图像中已连接的部分

plot_x=zeros(1,1);         %%用于记录质心位置的坐标

plot_y=zeros(1,1);

%%求质心

sum_x=0;sum_y=0;area=0;

[height,width]=size(bw);

for i=1:height

    for j=1:width

        if L(i,j)==1

            sum_x=sum_x+i;

            sum_y=sum_y+j;

            area=area+1;

        end

    end

end

%%质心坐标

plot_x(1)=fix(sum_x/area);

plot_y(1)=fix(sum_y/area);

figure(2);imshow(bw);

%%标记质心点

hold on

plot(plot_y(1) ,plot_x(1), '*')

原图:

MATLAB中求图像中某一区域的质心

二值化图像:

MATLAB中求图像中某一区域的质心

 标记区域质心的图像:

MATLAB中求图像中某一区域的质心

 以下内容  http://blog.sina.com.cn/s/blog_8ad7bc620101ac1d.html

matlab求图像最小外接矩形

 malab里面已经有一个现成minboundrect()函数来求最小外接矩形。我们可以用命令type minboundrect来查看minboundrect()函数的内容。

close all;

clc;

I=imread('d:\image.jpg');%此处为图像所放路径和图片名字。

bw=im2bw(I);          %将图像二值化

[r c]=find(bw==0);   

[rectx,recty,area,perimeter] = minboundrect(c,r,'a'); % 'a'是按面积算的最小矩形,如果按边长用'p'。

figure

imshow(bw);

line(rectx(:),recty(:),'color','r');