0
我有一個3x3的矩陣,它沒有排序。將2d矩陣的值賦給變量
按照升序或降序對3x3矩陣進行排序後,我希望將中值存儲在整型變量中(int med
)。
由於我的基質爲奇數的元素,我知道每一次的中位值將是matrix[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);
請發佈整個功能的代碼。這個問題可能不在這條線上。 – 2011-02-18 13:32:55