2013-11-24 62 views
3

我有Emgu圖像:如何從圖片複製細分?

Image<Bgr,byte> image = new Image<Bgr,byte>("image.jpg"); 

這裏是文件(image.jpg的)的樣子:

enter image description here

裏面紅黃色三角形我想複製到所有像素新圖像稱爲:

Image<Bgr,byte> copiedSegment; 

任何想法如何實現它,如果我有座標的三角形輪廓的所有座標。

預先感謝您。

回答

5

在opencv C++ api中,您可以使用帶有由三角組件組成的遮罩的矩陣副本功能。

Mat image = imread("image.jpg",CV_LOAD_IMAGE_COLOR); 
vector<Point> triangleRoi; 
Mat mask; 

//draw your trianlge on the mask 
cv::fillConvexPoly(mask, triangleRoi, 255); 

Mat copiedSegment; 
image.copyTo(copiedSegment,mask); 

您應該可以在emgu的基礎上編寫一些類似的代碼。

+0

元素,謝謝你的回答。 任何想法如何使不在多邊形內的透明像素? – Michael

+2

爲了使用透明度,我建議你查看OpenCV文檔中的第4個通道。將圖片初始化爲CV_8UC4,併爲第4個通道創建一個帶有零的cv :: Mat,然後將提取的3通道圖像與此「空白」通道合併。 – scap3y

+0

scap,EMGU中Mat的適當類型是什麼?任何想法? – Michael

0
// no we apply filter to get rid of Equalization Noise. 
ImageBilateral = new Image<Gray, Byte>(grayImg.Size); 
CvInvoke.BilateralFilter(grayImg, ImageBilateral, 0, 20.0, 2.0); 
//ImageBilateral.Save(String.Format("C:\\Temp\\BilateralFilter{0}.jpg", counter)); 

retImage = AlignFace(ImageBilateral); 

Point faceCenter = new Point(ImageBilateral.Width/2, (int)Math.Round(ImageBilateral.Height * FACE_ELLIPSE_CY)); 
Size size = new Size((int)Math.Round(ImageBilateral.Width * FACE_ELLIPSE_W), (int)Math.Round(ImageBilateral.Width * FACE_ELLIPSE_H)); 

// Filter out the corners of the face, since we mainly just care about the middle parts. 
// Draw a filled ellipse in the middle of the face-sized image. 
Image<Gray, Byte> mask = new Image<Gray, Byte>(ImageBilateral.Size); 

// draw Ellipse on Mask 
CvInvoke.Ellipse(mask, faceCenter, size, 0, 0, 360, new MCvScalar(255, 255, 255), -1, Emgu.CV.CvEnum.LineType.AntiAlias, 0); 
mask.Save(String.Format("C:\\Temp\\mask{0}.bmp", counter)); 

Image<Gray, Byte> temp1 = ImageBilateral.Copy(mask); 
ImageBilateral.Save(String.Format("C:\\Temp\\ImageBilateral.CopyTo(mask){0}.bmp", counter)); 
temp1.Save(String.Format("C:\\Temp\\temp1{0}.bmp", counter));