Mat outImg(inputImg.size(),inputImg.type());
inputImg(Rect(0, 0, shiftX, height)).copyTo(outImg(Rect(width-shiftX, 0, shiftX, height)));
inputImg(Rect(shiftX, 0, width-shiftX, height)).copyTo(outImg(Rect(0, 0, width-shiftX, height)));
round robin image rotation what is the best way to do in opencv
-
30-07-2022 - |
Вопрос
I want to do a round robin image rotation as shown below.
The x vertical line from the start of image is removed and added to the end of image.
What is the best way to do this in OpenCV, GDI++ and WPF? I need a solution for each of these platform but they could be implemented differently.
I need to implement it in a function with the following signature(for opencv)
Mat CircShift(Mat inputImage, int PixelInXdirectionToShift);
I know how can I do this by manipulating pixels, but I am looking for a solution that do this very fast when pixel manipulation is not that fast.
Решение
Другие советы
For OpenCV take a look at remap.
EDIT: Easy/Quick creation of map (using vector):
//Create vector of what the rows/cols look like
std::vector<int> t_X,t_Y;
for (int i = 0; i < img.cols(); i++) t_X.push_back(i);
for (int i = 0; i < img.rows(); i++) t_Y.push_back(i);
//circular shift t_X vector
std::vector<int>::iterator it;
int zeroPixel = 50; //This x-pixel to bring to 0 (shifting to the left)
it = std::find(t_X.begin(), t_X.end(), zeroPixel);
std::rotate(t_X.begin(), it, t_X.end());
//Create Maps
//Turn vectors in cv::Mat
cv::Mat xRange = cv::Mat(t_X);
cv::Mat yRange = cv::Mat(t_Y);
//Maps
cv::Mat xMap;
cv::Mat yMap;
cv::repeat(xRange.reshape(1,1), yRange.total(), 1, xMap);
cv::repeat(yRange.reshape(1,1).t(), 1, xRange.total(), yMap);
You could also use ROIs
int zeroPixel = 50;
cv::Mat newMat;
cv::Mat rightHalf = img(cv::Rect(0,0,zeroPixel,img.rows()));
cv::Mat leftHalf = img(cv::Rect(0,zeroPixel+1,img.cols()-zeroPixel-1,img.rows());
cv::hconcat(leftHalf , rightHalf , newMat);
Не связан с StackOverflow