# can't modify the pixel value

Hi everyone,

I want to smooth an image with opencv/c++,here's my code

newImgMax.at<float>(k,l) = (sum(imgMax, k, l, wmax) / (wmax * wmax));

my function sum works fine,I can edit the result of the function but I can't assign it to a pixel,the function sum returns a float and when I try to assign it to a float variable(not a pixel) I can't too.Any help please ?

edit retag close merge delete

proposal: describe, what you're trying to achieve, not where you got stuck. maybe you can show us some general formula, how your smoothing is supposed to work, -- then we can help you getting this into code.

(i.e. acccessing your image with at<float>() is for sure the wrong way)

I'm implementing the FABEMD(Fast and Adaptive Bidimensional Empirical Mode Decomposition Using Order-Statistics Filter Based Envelope Estimation) Method,it consists in decomposing an image into many BIMFs ,after generating the envelopes(upper(imgMax) and lower(imgMin)) I have to apply an averaging smooth for each one ,newImgMax represents the upper envelope after smoothing and it will be used to calculate the first BIMF :

for (int k = 0; k < input.size().width; k++) {
for (int l = 0; l < input.size().height; l++) {
if( (k >= 0 && k < input.size().width) && (l >= 0  && l < input.size().height) ) {
newImgMax.at<float>(k,l) = (sum(imgMax, k, l, wmax) / (wmax * wmax));} } }


please, append that to you original question ? (and use the "10101" button , to format code properly)

I am very curious about what you say. could you show us your code more.

I'm implementing the function "sum" which add the pixels of an image here's the code

float sum(cv::Mat img, int startedX, int startedY, int w) { float res = 0.0f;
for (int i = startedX - ((w - 1) / 2); i < startedX + ((w + 1) / 2); i++) {
for (int j = startedY - ((w - 1) / 2) ; j < startedY + ((w + 1) / 2); j++)         {
if ((i >= 0 && i < img.size().width) && (j >= 0  && j < img.size().height)) {
res =res + img.at<float>(i,j);
}  }
return res;}


in the main function I can call and edit the return of the function without any problem

 std::cout << "result= " <<sum(imgMax, k, l, wmax)<< std::endl;


but I CAN'T assign it to a float variable

float tmp =0.0f ; tmp=sum(imgMax, k, l, wmax);

my program crashes afterthis lign

rows / cols problem. you need: image.at<type>(row,col)

also, e.g. for startedX=0 and startedY=0 you code goes out of bounds. please run a debug build, so it throws proper asserts

1

I dont think so. He added another code if ((i >= 0 && i < img.size().width) && (j >= 0 && j < img.size().height)) to keep the pairs(i,j) in the bounds. Can you sure that the img's type is float?

the function works fine, the program crahsesafter this ligne

 **tmp=sum(imgMax, k, l, wmax);**


and shows me an error "segmentation fault core dumped'