c++ - assigning the value of a 2d matrix to an variable -
i have 3x3 matrix not sorted.
after sorting 3x3 matrix in ascending or descending order want store median value in integer variable (int med
).
as matrix having odd number of elements know every time median value matrix[1][1]
, have written this:
int med = matrix[1][1];
though no compile time error in visual studio while running application median, got below run time error:
"#2 run time check failure..the stack around variable matrix[][] corrupted..
here code:
#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; (int = 1; < d_w - 1; i++){ (int j = 1; j < d_h - 1; j++){ int temp = val2(in,i,j); mmsg_reporterror("the value of temp is: %d ",temp); (int p = - 1; p < + 2; p++){ (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]); } } (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 %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 %d ",*pout); } else{ *pout = med; mmsg_reporterror("the value of *pout after %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);
take @
for (element = &mask2d_int[0][0]; element <= &mask2d_int[max-1][max-1]; element ++) { ^^ if (*element > *(element + 1)) { ^^^^^^^^^^^
you loop while element <= &mask2d_int[max-1][max-1]
means element runs , including last int, compare "next" int (which doesn't exist), , possibly swap.
the bytes after multidimensional array contain "magic value" put compiler (which generating debug build). swapping last int in array bytes after array trashes value, causing error seeing.
changing loop to
for (element = &mask2d_int[0][0]; element < &mask2d_int[max-1][max-1]; element ++) { ^
should trick.
if want working - i've said - notice reason sort matrix median, , you've tagged question "c++" means there easier ways of doing if interested.
Comments
Post a Comment