歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV 金字塔圖像分割

OpenCV 金字塔圖像分割

日期:2017/3/1 9:34:41   编辑:Linux編程

OpenCV中有封裝好的cvPyrSegmentation函數,參數解釋如下:

PyrSegmentation
用金字塔實現圖像分割

void cvPyrSegmentation( IplImage* src, IplImage* dst,
CvMemStorage* storage, CvSeq** comp,
int level, double threshold1, double threshold2 );
src
輸入圖像.
dst
輸出圖像.
storage
Storage: 存儲連通部件的序列結果
comp
分割部件的輸出序列指針 components.
level
建立金字塔的最大層數
threshold1
建立連接的錯誤阈值
threshold2
分割簇的錯誤阈值
函數 cvPyrSegmentation 實現了金字塔方法的圖像分割。金字塔建立到 level 指定的最大層數。如果 p(c(a),c(b))<threshold1,則在層 i 的象素點 a 和它的相鄰層的父親象素 b 之間的連接被建立起來,

定義好連接部件後,它們被加入到某些簇中。如果p(c(A),c(B))<threshold2,則任何兩個分割 A 和 B 屬於同一簇。

如果輸入圖像只有一個通道,那麼

p(c1,c2)=|c1-c2|.
如果輸入圖像有單個通道(紅、綠、蘭),那幺

p(c1,c2)=0,3·(c1r-c2r)+0,59·(c1g-c2g)+0,11·(c1b-c2b) .
每一個簇可以有多個連接部件。圖像 src 和 dst 應該是 8-比特、單通道 或 3-通道圖像,且大小一樣

Threshold1,Threshold2的解讀:

看到別人博客裡有人在問這兩個參數的區別,其實就如上面黃色底紋文字所說,

金字塔分割先通過p(c(a),c(b))<threshold1在像素a,b...中找連通域,也就是所謂的連接部件A,B...

第二步通過p(c(A),c(B))<threshold2判斷兩個聯通與是否屬於同一個簇,簇即使我們最後得到的分割結果

例程:

void ON_SEGMENT(int a)
{
cvPyrSegmentation(images0, images1, storage, &comp,
level, threshold1+1, threshold2+1);
cvShowImage("Segmentation", images1);
}

void CCVMFCView::OnPyrSegmentation()
{
images[0] = cvCloneImage(workImg);
cvFlip(images[0]);

cvNamedWindow("Segmentation", 1);

storage = cvCreateMemStorage ( block_size );

images[0]->width &= -(1<<level);
images[0]->height &= -(1<<level);

images0 = cvCloneImage( images[0] );
images1 = cvCloneImage( images[0] );
// 對彩色圖像進行分割
l = 1;
threshold1 =255;
threshold2 =30;


sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation",
&threshold1, 255, ON_SEGMENT);
sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",
&threshold2, 255,ON_SEGMENT);

ON_SEGMENT(1);

cvWaitKey(0);
cvDestroyWindow( "Segmentation" );
cvFlip(images1);

images1->width = workImg->width;
images1->height = workImg->height;

cvReleaseMemStorage(&storage );
cvReleaseImage(&images[0]);
cvReleaseImage(&images0);

m_dibFlag=imageReplace(images1,&workImg);
m_ImageType=1;
Invalidate();
}

金字塔圖像分割結果:

--------------------------------------分割線 --------------------------------------

Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm

[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm

--------------------------------------分割線 --------------------------------------

OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved