2016-08-04 59 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

是的,正是我需要的 –

回答