can't modify the pixel value

asked 2016-03-29 11:05:40 -0600

choukri el gravatar image

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 flag offensive close merge delete

Comments

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)

berak gravatar imageberak ( 2016-03-29 12:29:12 -0600 )edit

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));} } }
choukri el gravatar imagechoukri el ( 2016-03-29 15:23:42 -0600 )edit

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

berak gravatar imageberak ( 2016-03-29 15:27:08 -0600 )edit

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

german_iris gravatar imagegerman_iris ( 2016-03-29 21:16:39 -0600 )edit

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

choukri el gravatar imagechoukri el ( 2016-03-31 05:49:02 -0600 )edit

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

berak gravatar imageberak ( 2016-03-31 05:56:24 -0600 )edit

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

berak gravatar imageberak ( 2016-03-31 05:59:11 -0600 )edit
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?

german_iris gravatar imagegerman_iris ( 2016-03-31 07:17:55 -0600 )edit

^^ right, missed that. thanks for the correction !

berak gravatar imageberak ( 2016-03-31 07:24:49 -0600 )edit

the function works fine, the program crahsesafter this ligne

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

and shows me an error "segmentation fault core dumped'

choukri el gravatar imagechoukri el ( 2016-03-31 13:31:19 -0600 )edit

what's kind of your image? I think you should try img.at<uchar>(i,j) not img.at<float>(i,j).

german_iris gravatar imagegerman_iris ( 2016-03-31 22:33:48 -0600 )edit