C 二维数组算法的帮助

C 二维数组算法的帮助,c,arrays,algorithm,multidimensional-array,C,Arrays,Algorithm,Multidimensional Array,我正在开发一个程序,以最好的方式将两幅图像组合在一起。假设我有两个2D数组,其中包含构成这两个图像的“像素”结构 我已经编写了一个算法,该算法根据“x”和“y”确定将两个图像中较大的图像连接到一起的位置,其中x是较大图像的2D数组的列,y是行。它们应该连接的点封装在“匹配”结构中,如下所示: typedef struct { int overlap; // used in algorithm to determine where to combine int x;

我正在开发一个程序,以最好的方式将两幅图像组合在一起。假设我有两个2D数组,其中包含构成这两个图像的“像素”结构

我已经编写了一个算法,该算法根据“x”和“y”确定将两个图像中较大的图像连接到一起的位置,其中x是较大图像的2D数组的列,y是行。它们应该连接的点封装在“匹配”结构中,如下所示:

typedef struct {
    int overlap;       // used in algorithm to determine where to combine
    int x;             // column of larger image
    int y;             // row of larger image
    int overlapWidth;  // width of the overlap between the two images
    int overlapHeight; // height of the overlap between the two images
} match;
请注意,相对于较大的图像,匹配点可以是负数(x、y或两者)

重叠中包含的像素被平均化,两个图像之外的任何像素(但仍在新维度内)都是白色的

示例

 +---+---+ image1
 |   |   |
 +---+---+---+ image2
 |   | X |   |
 +---+---+---+
     |   |   |    
     +---+---+
FOREACH row in new_height
    FOREACH col in new_width
        IF larger_image AND smaller_image do not contain (row,col)
            add white pixel
        ELSE IF overlap contains (row,col)
            add average of pixels
        ELSE IF only larger_image contains (row,col)
            add larger_image pixel
        ELSE
            add smaller_image pixel
X
表示这两个图像的匹配点,因此其状态如下所示:

typedef struct {
    int overlap;       // used in algorithm to determine where to combine
    int x;             // column of larger image
    int y;             // row of larger image
    int overlapWidth;  // width of the overlap between the two images
    int overlapHeight; // height of the overlap between the two images
} match;
x=1
y=1
重叠宽度=1
重叠高度=1

本例的新图像为3x3

那么,假设我有将两幅图像连接在一起的“匹配”,计算出的结果图像的新宽度和高度,以及基于这些新尺寸为结果图像分配的2D数组,我想知道如何将适当的像素放入新图像中。下面是一些伪代码,表示我正在尝试做的事情

伪代码

 +---+---+ image1
 |   |   |
 +---+---+---+ image2
 |   | X |   |
 +---+---+---+
     |   |   |    
     +---+---+
FOREACH row in new_height
    FOREACH col in new_width
        IF larger_image AND smaller_image do not contain (row,col)
            add white pixel
        ELSE IF overlap contains (row,col)
            add average of pixels
        ELSE IF only larger_image contains (row,col)
            add larger_image pixel
        ELSE
            add smaller_image pixel

我很难想出一个方法来做到这一点。我不一定在寻找代码,我只是在寻找如何做我所说的事情的想法。如果您有任何想法,我们将不胜感激。

您的伪代码看起来是正确的。你只是在把它翻译成C语言时遇到了麻烦吗

首先,伪代码使用单词“add”,这意味着像素将被放入某个新数组中,但它没有指定该数组的来源。请允许我稍微扩展一下您的伪代码:

Create a new_image
FOREACH row in new_height
    FOREACH col in new_width
        IF larger_image AND smaller_image do not contain (row,col)
            in new_image, set (row,col) to white pixel
        ELSE IF overlap contains (row,col)
            in new_image, set (row,col) to average of pixels
        ELSE IF only larger_image contains (row,col)
            in new_image, set (row,col) to larger_image pixel
        ELSE
            in new_image, set (row,col) to smaller_image pixel
现在的问题是“不包含(行、列)”。图像不包含特定坐标意味着什么?这意味着(行,列)坐标位于源图像的空间之外。我认为如果您编写一个函数,在新的图像空间中获取一个图像(大或小)和一个
match
struct,以及一个(行、列)坐标,并从源图像返回相应的像素,或者指示该像素不在该图像中,这会有所帮助。(在C语言中,可以让函数返回int,如果有像素,则返回1;如果没有像素,则返回0;如果返回1,则返回一个指向像素的指针的参数。)

现在,您可以询问此函数大图像和小图像是否包含(行,列),如果包含,还可以从每个图像中获取像素,并应用四种不同的情况。

要检查哪些图像属于
(行,列)
,可以使用此算法。我需要有大小图像的大小;因此,需要额外的参数

INPUT: row, col, match, big, small
if match.x < 0 then
    // offset of big image, in case small one is to the left
    offsetx <- -match.x
if match.y < 0 then
    // offset of big image, in case small one is above
    offsety <- -match.y
if offsetx <= col and col < offsetx + big.width then
    if offsety <= row and row < offsety + big.height then
        hitBig <- true
        coordBig <- (col - offsetx, row - offsety)
if offsetx + match.x <= col and col < offsetx + match.x + small.width then
    if offsety + match.y <= row and row < offsety + match.y + small.height then
        hitSmall <- true
        coordSmall <- (col - offsetx - match.x, row - offsety - match.y)
return hitBig, coordBig, hitSmall, coordSmall
输入:行、列、匹配、大、小
如果match.x<0,则
//大图像的偏移量,如果小图像在左侧
抵销