如何插入像素坐标而不是像素值?需要它来生成相机的校正重映射

时间:2019-04-21 13:01:03

标签: c++ opencv

传统上,在处理图像时,对像素值进行插值。我的情况是,我需要生成一个重映射(在PC上),然后将其放置在带有摄像头的嵌入式系统中进行进一步处理,这样,任何处理算法都可以直接引用该重映射,而不必事先处理每一帧。就像这样(ofc不是在应用程序中这样写的,只是一个演示):

struct Point { //A pixel on the image
    int x, y
} original, corrected;

//I want every entry in this remap to have a valid value
float remap[width][height][2] = ...; //Contains the new pixel coordinates of each pixel
//I want the processes in the embedded system to be able to do this with every pixel
//But now it is not possible as not every pixel has a corresponding mapX and mapY
corrected.x = remap[original.x][original.y][0]; //mapX
corrected.y = remap[original.x][original.y][1]; //mapY

生成重映射的过程之一是使用OpenCV进行径向变形校正。由于OpenCV使用反向映射dst(x, y) = src(mapX(x, y), mapY(x, y)),因此我再次对其进行反向转换,使得dst(mapX(x, y), mapY(x, y)) = src(x, y)。但随后,某些x, y在更正后没有相应的mapX, mapY

我需要每个x, y都有一个相应的mapX, mapY,嵌入式系统上的进程才能正常工作。如何插入像素坐标而不是像素值?

到目前为止,我仅尝试对周围区域的mapX, mapY值进行平均,但是效果不佳。

for (int i = 0; i < parameters.width * parameters.height; ++i) {
    if (!status[i]) { //If no corresponding mapX, mapY
        double avgX = 0, avgY = 0;
        int count = 0;
        for (int y = -5; y < 5; ++y) { //A square surrounding the pixel being interpolated
            for (int x = -5; x < 5; ++x) {
                int num = std::min(std::max(0, i + y * parameters.width + x), parameters.width * parameters.height);
                if (status[num]) { //If this pixel has a corresponding mapX, mapY
                    avgX += mapX[num];
                    avgY += mapY[num];
                    ++count;
                }
            }
        }
        //If at least 1 pixel in the surrounding area has a corresponding mapX, mapY
        if (avgX != 0 || avgY != 0) {
            avgX /= count;
            avgY /= count;
            mapX[i] = avgX;
            mapY[i] = avgY;
            status[i] = true;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

可能会警告

 int num = std::min(std::max(0, i + y * parameters.width + x), parameters.width * parameters.height);

必须

int num = std::min(std::max(0, i + y * parameters.width + x), parameters.width * parameters.height - 1);

由于for (int i = 0; i < parameters.width * parameters.height; ++i)(我的意思是i < parameters.width * parameters.height而不是i <= parameters.width * parameters.height


仅当(status[num])为true时,

avgX avgY 才被修改, status 的某些条目包含 true < / em>之前要对进行总体