Find 4 specific angle pixels and use them with a warp perspective.

I play with OpenCV, and I want to know how you will create a simple version of a perspective conversion program. I have a parallelogram image, and each of its angles consists of a pixel with a certain color, which is nowhere else in the image. I want to iterate over all the pixels and find these 4 pixels. Then I want to use them as corner points in a new image to distort the perspective of the original image. In the end, I had to zoom in.

Point2f src[4]; //Is this the right datatype to use here?
int lineNumber=0;
//iterating through the pixels
for(int y = 0; y < image.rows; y++)
{
    for(int x = 0; x < image.cols; x++)
    {
        Vec3b colour = image.at<Vec3b>(Point(x, y));
    if(color.val[1]==245 && color.val[2]==111 && color.val[0]==10) { 
        src[lineNumber]=this pixel // something like Point2f(x,y) I guess
        lineNumber++;
    }
    }
}
/* I also need to get the dst points for getPerspectiveTransform 
and afterwards warpPerspective, how do I get those? Take the other 
points, check the biggest distance somehow and use it as the maxlength to calculate 
the rest? */

OpenCV ? ( , " " ). , "" . - , ?

- :

enter image description here

+4
1

, :

enter image description here

, png-, , . , jpeg, , , .

.

- .

getPerspectiveTransform warpPerspective, .

:

enter image description here

:

#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread("path_to_image");

    // Create a black output image
    Mat3b out(300,300,Vec3b(0,0,0));

    // The color of your markers, in order
    vector<Scalar> colors{ Scalar(0, 0, 255), Scalar(0, 255, 0), Scalar(255, 0, 0), Scalar(0, 255, 255) }; // red, green, blue, yellow

    vector<Point2f> src_vertices(colors.size());
    vector<Point2f> dst_vertices = { Point2f(0, 0), Point2f(0, out.rows - 1), Point2f(out.cols - 1, out.rows - 1), Point2f(out.cols - 1, 0) };

    for (int idx_color = 0; idx_color < colors.size(); ++idx_color)
    {
        // Detect color
        Mat1b mask;
        inRange(img, colors[idx_color], colors[idx_color], mask);

        // Find connected components
        vector<vector<Point>> contours;
        findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

        // Find largest
        int idx_largest = distance(contours.begin(), max_element(contours.begin(), contours.end(), [](const vector<Point>& lhs, const vector<Point>& rhs) {
            return lhs.size() < rhs.size();
        }));

        // Find centroid of largest component
        Moments m = moments(contours[idx_largest]);
        Point2f center(m.m10 / m.m00, m.m01 / m.m00);

        // Found marker center, add to source vertices
        src_vertices[idx_color] = center;
    }

    // Find transformation
    Mat M = getPerspectiveTransform(src_vertices, dst_vertices);

    // Apply transformation
    warpPerspective(img, out, M, out.size());

    imshow("Image", img);
    imshow("Warped", out);
    waitKey();

    return 0;
}
+3

Source: https://habr.com/ru/post/1617154/


All Articles