Michael
Michael

Reputation: 13626

How to copy segment from the image?

I have Emgu image :

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

Here is how the file(image.jpg) looks like:

enter image description here

All pixels that inside red-yellow triangle I want to copy to the new image called:

Image<Bgr,byte> copiedSegment;

Any idea how to implement it if I have coordinates all coordinates of the triangle contour.

Thank you in advance.

Upvotes: 4

Views: 2096

Answers (2)

Oscar Rangel
Oscar Rangel

Reputation: 1056

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

Upvotes: 0

Element
Element

Reputation: 4051

In the opencv c++ api you can just use the matrix copy function with a mask composed of your triangular components.

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

You should beable to write some similar code in emgu based on this.

Upvotes: 6

Related Questions