2013-11-24 60 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));