Ask Your Question
0

How to get smooth image after transforming by changing pixel position using opencv?

asked 2017-04-14 04:59:42 -0500

dhaval gravatar image

updated 2017-04-14 09:18:29 -0500

I am transforming image using opencv in following way. but, I am getting distorted image. any way to get the smoothly transformed image(as per the attached last - goalImage) ?

I have tried using warpPerspective, warpAffine. it is producing smooth transformation but, transformation that is needed is not been possible to get based on calculating just 4 points.

I have used blur, GaussianBlur, medianBlur, bilateralFilter on dstImage but, still not getting favorable result.

Mat_<Vec3b> srcImage = imread("C:/images/0.jpg", CV_LOAD_IMAGE_COLOR);
Mat_<Vec3b> dstImage = srcImage.clone();
for (int i = 0; i < dstImage.rows; i++)
    {
        for (int j = 0; j < dstImage.cols; j++)
        {
            //calculating rnew and thetanew -- from i and j
            ynew = rnew * sin(thetanew);

            dstImage(i, j)[0] = srcImage(i, ynew)[0];
            dstImage(i, j)[1] = srcImage(i, ynew)[1];
            dstImage(i, j)[2] = srcImage(i, ynew)[2];

            //I have tried following way also, but result is same
            //dstImage(i, j) = getColorSubpix(eiffel, cv::Point2f(i, ynew));

        }
    }

cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt)
{
    assert(!img.empty());
    assert(img.channels() == 3);

    int x = (int)pt.x;
    int y = (int)pt.y;

    int x0 = cv::borderInterpolate(x,   img.cols, cv::BORDER_REFLECT_101);
    int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
    int y0 = cv::borderInterpolate(y,   img.rows, cv::BORDER_REFLECT_101);
    int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);

    float a = pt.x - (float)x;
    float c = pt.y - (float)y;

    uchar b = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[0] * a) * (1.f - c)
                           + (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
    uchar g = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[1] * a) * (1.f - c)
                           + (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
    uchar r = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[2] * a) * (1.f - c)
                           + (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);

    return cv::Vec3b(b, g, r);
}

srcImage : My source image

dstImage :My output image

Instead of broken lines in above image, I want to produce smooth lines like below image

goalImage : Kind of smoothness I want to achieve after transformation

edit retag flag offensive close merge delete

Comments

1

added some karma so that you can update post and upload images!

StevenPuttemans gravatar imageStevenPuttemans ( 2017-04-14 07:58:30 -0500 )edit

@StevenPuttemans : Thanks !

dhaval gravatar imagedhaval ( 2017-04-14 09:15:38 -0500 )edit
1

Your approach seems good, you just need to debug it to verify the interpolation function.

Try to experiment with synthetical data (2x2 pixel image) and some well-defined points: (0,0); (0.5,0.5); (0,0.5) etc.

kbarni gravatar imagekbarni ( 2017-04-14 10:31:25 -0500 )edit
1

@kbarni : yup, it seems I need to go title bit more close to the problem and small image will help to debug.. thanks.

dhaval gravatar imagedhaval ( 2017-04-14 11:32:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-04-15 02:57:38 -0500

berak gravatar image

updated 2017-04-15 03:36:50 -0500

you should use remap to model arbitrary distortions. it will also handle interpolation and borders for free.

Mat img = imread("img/grid.jpg");
int width  = img.cols;
int height = img.rows;
// setup distortion mapping:
Mat_<Vec2f> prjMap(height, width);
for (int x=0; x<width; x++) {
    for (int y=0; y<height; y++) {
        float u = x + sin(float(y)/100)*15; // some funny formula ..
        float v = y + cos(float(x)/100)*20;
        prjMap(y, x) = Vec2f(u,v);
    }
}

// apply distortion to image:
Mat projected;
remap(img, projected, prjMap, cv::Mat(), INTER_LINEAR, BORDER_CONSTANT); // play with flags !
imshow("P", projected);
waitKey();

image description

edit flag offensive delete link more

Comments

@berak : thanks for your expert input. this is what I was looking for.

dhaval gravatar imagedhaval ( 2017-04-16 23:32:55 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-14 04:59:42 -0500

Seen: 201 times

Last updated: Apr 15 '17