简体   繁体   English

OpenCV裁剪图像与椭圆

[英]OpenCV crop image with ellipse

I have an image (cv::Mat) with size of 92x112 I want to surround the object in this image with a ellipse then get only this pixels to create another image containing only the object. 我有一个大小为92x112的图像(cv :: Mat)我想用椭圆环绕这个图像中的对象,然后只获得这些像素来创建另一个只包含对象的图像。

I mean, cropping the original image with a ellipse. 我的意思是,用椭圆裁剪原始图像。 It's possible? 这是可能的?

I am trying drawing a ellipse, but the ellipse don't draw complete, with that: 我正在尝试绘制一个椭圆,但是椭圆不会绘制完整的,具有:

ellipse(escalada, Point(92/2,112/2), Size(92/2*0.95,112/2*0.85), 0.0, 90.0, 0.0, Scalar(255,0,0), 3, 8);

and made some test with cvSetImageROI to crop the image, but this method works only with cvRect . 并使用cvSetImageROI进行一些测试以裁剪图像,但此方法仅适用于cvRect

Some idea? 有些想法?

I solve using this: 我用这个解决了:

imagen = imread(nombre_imagen,0); //read image (grayscale)
//Use old C interface 
IplImage *res,*roi;
IplImage src(imagen);
res = cvCreateImage(Size(imagen.rows,imagen.cols),8,1);
roi = cvCreateImage(Size(imagen.rows,imagen.cols),8,1);
cvZero(roi);
cvEllipse(roi,cvPoint(src.width/2,src.height/2),cvSize(src.width/2*0.85,src.height/2*0.95),0.0,0.0,360.0,CV_RGB(255,255,255),-1,8,0);

cvAnd(&src, &src, res, roi);
cvReleaseImage(&roi);

then in res variable i have a image showing the ROI with a ellipse and the rest in black. 然后在res变量我有一个图像显示ROI椭圆形,其余为黑色。

There is no direct support for non-rectangular ROI. 没有直接支持非矩形ROI。
But you can use a mask - see http://docs.opencv.org/doc/tutorials/core/mat-mask-operations/mat-mask-operations.html (not directly circular but original tutorial doesn't exist) 但你可以使用一个掩码 - 请参阅http://docs.opencv.org/doc/tutorials/core/mat-mask-operations/mat-mask-operations.html (不是直接循环但原始教程不存在)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM