2016-08-04 35 views
2

所以我们的想法是拍摄一张矩形图像,并从中圈出一个圆圈。我想出了一个简单的算法,它从源图像中提取像素并将它们逐行排列成圆形,但问题在于结果太失真。有没有任何算法可以做到这一点,而不会丢失这么多的数据?在给定图像中在OpenCV中绘制一个圆环

下面的代码:

//reading source and destination images 
Mat src = imread("srcImg.jpg", 1); 
Mat dst = imread("dstImg.jpg", 1); 

int srcH = src.rows; int srcW = src.cols; 
int dstH = dst.rows; int dstW = src.cols; 

//convert chamber radius to pixels 
double alpha; 
int r = 250; 
double k = 210/(500 * PI); 

//take pixels from source and arrange them into circles 
for (int i = srcH-1; i > 0; i--) { 
    for (int j = 1; j <= srcW; j++) { 
     alpha = (double) (2 * PI * (r * k+i))/j; 
     int x_new = abs((int) (dstW/2 - (r * k + i) * cos(alpha)) - 200); 
     int y_new = abs((int) (dstH/2 - (3.5*(r * k + i) * sin(alpha)))+1000); 
     dst.at<uchar>(x_new, y_new) = src.at<uchar>(srcH-i, srcW-j); 
    } 
} 

//make dst image grey and show all images 
Mat dstGray; 
cvtColor(dst, dstGray, CV_RGB2GRAY); 
imshow("Source", src); 
imshow("Result", dstGray); 

waitKey(); 

一个结果如下图所示: result

+0

所以你需要什么,似乎类似于液化具有较大影响半径,您想要将矩形图像封装在给定半径的圆形光盘中?我对吗 ? – ZdaR

+0

是的,正是我需要的 –

回答