天天看點

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');