C++ 将二维矩阵的值指定给变量

C++ 将二维矩阵的值指定给变量,c++,C++,我有一个3x3矩阵,它没有排序 在对3x3矩阵进行升序或降序排序后,我希望将中值存储在整数变量(int-med)中 由于我的矩阵有奇数个元素,我知道每次中值都是矩阵[1][1],所以我基本上是这样写的: int med = matrix[1][1]; 虽然我在Visual Studio中运行中值应用程序时没有编译时错误,但我得到以下运行时错误: "#2 run time check failure..the stack around variable matrix[][] is corrupt

我有一个3x3矩阵,它没有排序

在对3x3矩阵进行升序或降序排序后,我希望将中值存储在整数变量(
int-med
)中

由于我的矩阵有奇数个元素,我知道每次中值都是
矩阵[1][1]
,所以我基本上是这样写的:

int med = matrix[1][1];
虽然我在Visual Studio中运行中值应用程序时没有编译时错误,但我得到以下运行时错误:

"#2 run time check failure..the stack around variable matrix[][] is corrupted..
代码如下:

#include "stdafx.h"
#include <math.h>
#include <mtip.h>
#define MAX 3

int medianfilter_MIF(HMTIPMOD hMod ,Dataset_byte *in,Dataset_byte **out,int th) 
{
    if(!in)
    {
        mmsg_ReportError("Missing input!");
        return MRV_FAILURE;
    }

    *out = (Dataset_byte*)md_DatasetClone((Dataset*)in, DST_SAME);
    if(!*out)
    {
        mmsg_ReportError("Could not clone image!");
        return MRV_FAILURE;
    }


    BYTE *pin = DATA_PTR(in);
    BYTE *pout = DATA_PTR(*out);
    int d_w = NX(in); 
    int d_h = NY(in);
    int mask2d_int[MAX][MAX];
    int *element;
    int temp;

    for (int i = 1; i < d_w - 1; i++){  
        for (int j = 1; j < d_h - 1; j++){
            int temp = VAL2(in,i,j);
            mmsg_ReportError("the value of temp is: %d ",temp);
            for (int p = i - 1; p < i + 2; p++){
                for (int q = j - 1; q < j + 2; q++){
                    mask2d_int[p][q]=VAL2(in,p,q);
                    mmsg_ReportError("the value of mask2d_int[p][q] : %d ",mask2d_int[p][q]);
                }
            }


    for (element = &mask2d_int[0][0]; element <= &mask2d_int[MAX-1][MAX-1]; element ++) {
                 if (*element > *(element + 1)) {
               *element ^= *(element + 1); /* exclusive or swap */
               *(element + 1) ^= *element;
               *element ^= *(element + 1);
               }
        msg_ReportError("the value of element after swapping is %d ",element);
            }
            int med = mask2d_int[1][1];
            mmsg_ReportError("the value of median is: %d ",med);
            if ((temp - med) < th){
                *pout = temp;
                mmsg_ReportError("the value of *pout before is %d ",*pout);
                }
            else{
                *pout = med;
                mmsg_ReportError("the value of *pout after is %d ",*pout);
                }
            ++pout;
            }
        }
    return MRV_SUCCESS;
}


int medianfilter_MDF(HMTIPMOD hMod)
{
    INPUT_DATASET("Input image", 0, 0, DST_BYTE, CONN_REQUIRED);
    OUTPUT_DATASET("Output image", 0, 0, DST_BYTE, 0);
    CONTROL_INT_SLIDER("threshold:",30,0,255);
    return MRV_SUCCESS;
}

REGISTER_MTIP_MODULE("Median Filter", medianfilter_MDF, medianfilter_MIF, 0, 0, 0);
#包括“stdafx.h”
#包括
#包括
#定义最大值3
int medianfilter_MIF(HMTIPMOD hMod、数据集_字节*in、数据集_字节**out、int th)
{
如果(!in)
{
mmsg_报告错误(“缺少输入!”);
返回MRV_故障;
}
*out=(数据集字节*)md_数据集克隆((数据集*)in,DST_相同);
如果(!*out)
{
mmsg_报告错误(“无法克隆图像!”);
返回MRV_故障;
}
字节*pin=数据_PTR(in);
字节*pout=数据_PTR(*out);
int d_w=NX(in);
int d_h=NY(in);
int mask2d_int[MAX][MAX];
int*元素;
内部温度;
对于(int i=1;i
看一看

for (element = &mask2d_int[0][0]; element <= &mask2d_int[MAX-1][MAX-1]; element ++) {
                                          ^^
                 if (*element > *(element + 1)) {
                                  ^^^^^^^^^^^
for(元素=&mask2d_int[0][0];元素*(元素+1)){
^^^^^^^^^^^

你在
元素时循环,请发布整个函数的代码。问题可能不在这一行。我现在已经添加了整个代码。你能试着找出问题吗..谢谢
for (element = &mask2d_int[0][0]; element < &mask2d_int[MAX-1][MAX-1]; element ++) {
                                          ^