1

I have 2 (aerial) images, taken from a slightly different angle:

image 1: enter image description here

image2: enter image description here

I need to rescale image1 in horizontal direction to align it to image2. Without any modification, both images placed next to each other look like this:

enter image description here

This is the desired result: (made with photoshop) enter image description here

In Photoshop, I took the right half of image1 and scaled it down horizontally a little bit. I did the same for the left half of image1, where I had to scale slightly more.

I would like to know how I can accomplish this using openCV - by using Hough Line Transform. I already started drawing hough lines, but I have no idea how to do the transform to make the hough lines match:

enter image description here

Here's my C++ code (called from objective-c):

cv::Mat image1 = [im1 CVMat3];
cv::Mat gray_image1;
// Convert to Grayscale
cvtColor( image1, gray_image1, CV_RGB2GRAY );

cv::Mat dst1, cdst;
Canny(image1, dst1, 40, 90, 3);

double minLineLength = 0;
double maxLineGap = 10;

std::vector<cv::Vec2f> lines;
// detect lines
cv::HoughLines(dst1, lines, 1, CV_PI/180, 90, minLineLength,maxLineGap );


for( size_t i = 0; i < lines.size(); i++ ) {
    float rho = lines[i][0], theta = lines[i][1];
    if( theta>CV_PI/180*70 && theta<CV_PI/180*110) {

        cv::Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000*(-b));
        pt1.y = cvRound(y0 + 1000*(a));
        pt2.x = cvRound(x0 - 1000*(-b));
        pt2.y = cvRound(y0 - 1000*(a));

        line( image1, pt1, pt2, cvScalar(10,100,255), 3, CV_AA);
    }
}

Some help would be really appreciated :-). Thanks in advance.

gasparuff
  • 2,295
  • 29
  • 48
  • Are you using Python, Java or C++?... and do you have to use Hough Lines? It may be possible to use image stitching: http://www.pyimagesearch.com/2016/01/11/opencv-panorama-stitching/ – rayryeng Feb 10 '17 at 16:35
  • I have already tried using the panaroma stitching, but I have more than 2 images (around 7) and the distortion gets bigger on every image (10° angle step), which seems to make panorama stitching impossible. Panorama stitching always cropped my images and the results were horrible. I need the images to stay (almost) the same size they are. I'm not sure if using Hough Lines will really solve my problem, do you have any other idea? :-) – gasparuff Feb 10 '17 at 16:52
  • Have you looked at the `Stitcher` class? It is designed to handle multiple images... and given that there seems to be just a vertical shift, no cropping vertically should happen: http://stackoverflow.com/questions/16284126/opencv-stitcher-class-with-overlapping-stationary-cameras... but if you are opposed to stitching, let me think about that for a while :) – rayryeng Feb 10 '17 at 16:58
  • If there is a scaling involved, we need to fit an affine transform between the images. If you are open to choices in using additional libraries alongwith OpenCV, please do take a look at **monomodal** [`registration`](https://itk.org/Doxygen/html/classitk_1_1ImageRegistrationMethodv4.html) algorithm using [`GradientDescentOptimizer`](https://itk.org/Doxygen/html/classitk_1_1RegularStepGradientDescentOptimizerv4.html) and [`MeanSquaresMetric`](https://itk.org/Doxygen/html/classitk_1_1MeanSquaresImageToImageMetricv4.html) implemented by ITK library. – sgarizvi Feb 10 '17 at 20:08

0 Answers0