Why does my denoised image get darker after using non local means algorithm and why is the noise not reduced...












0















I have implemented the naive version of non local means without integral images.
First image acquisition:



row = 10;
col = 10;

% Patchsize - make sure if code works for different values
patchSize = 4;

% Search window size - make sure if code works for different values
searchWindowSize = 10;

image = imread('r.jpg');
new=imnoise(image,'gaussian');
imshow(new)
ws=floor(searchWindowSize/2);
ps=floor(patchSize/2);
new= padarray(new, [searchWindowSize,searchWindowSize], 'both');
new= padarray(new, [patchSize,patchSize], 'both');


Now, the main non local means function:



unew=zeros(size(new,1),size(new,2));
for i=1+ws+ps:size(new,1)-ws-ps
for j=1+ws+ps:size(new,2)-ws-ps
sear=new(i-ws:i+ws,j-ws:j+ws);
%imshow(new(i-ws:i+ws,j-ws:j+ws))
sear=padarray(sear,[ps,ps],'both');
patch=new(i-ps:i+ps,j-ps:j+ps);
dis=;
ind=1;
sums=0;
for ii=1+ps:size(sear,1)-ps
for jj=1+ps:size(sear,1)-ps
patchn=new(ii-ps:ii+ps,jj-ps:jj+ps);
ssd=(patchn-patch).*2;
psum=0;
for mj=1:patchSize
for gj=1:patchSize
psum= psum+ssd(mj,gj);
end
end
dis(ind)=psum;
sums=sums+exp(-max(dis(ind)*dis(ind)-2*0.1*0.1,0)/0.75*0.75);

ind=ind+1;
end
end
new(i,j)=sums;
end
end


My initial image:



enter image description here



My final image:



enter image description here










share|improve this question





























    0















    I have implemented the naive version of non local means without integral images.
    First image acquisition:



    row = 10;
    col = 10;

    % Patchsize - make sure if code works for different values
    patchSize = 4;

    % Search window size - make sure if code works for different values
    searchWindowSize = 10;

    image = imread('r.jpg');
    new=imnoise(image,'gaussian');
    imshow(new)
    ws=floor(searchWindowSize/2);
    ps=floor(patchSize/2);
    new= padarray(new, [searchWindowSize,searchWindowSize], 'both');
    new= padarray(new, [patchSize,patchSize], 'both');


    Now, the main non local means function:



    unew=zeros(size(new,1),size(new,2));
    for i=1+ws+ps:size(new,1)-ws-ps
    for j=1+ws+ps:size(new,2)-ws-ps
    sear=new(i-ws:i+ws,j-ws:j+ws);
    %imshow(new(i-ws:i+ws,j-ws:j+ws))
    sear=padarray(sear,[ps,ps],'both');
    patch=new(i-ps:i+ps,j-ps:j+ps);
    dis=;
    ind=1;
    sums=0;
    for ii=1+ps:size(sear,1)-ps
    for jj=1+ps:size(sear,1)-ps
    patchn=new(ii-ps:ii+ps,jj-ps:jj+ps);
    ssd=(patchn-patch).*2;
    psum=0;
    for mj=1:patchSize
    for gj=1:patchSize
    psum= psum+ssd(mj,gj);
    end
    end
    dis(ind)=psum;
    sums=sums+exp(-max(dis(ind)*dis(ind)-2*0.1*0.1,0)/0.75*0.75);

    ind=ind+1;
    end
    end
    new(i,j)=sums;
    end
    end


    My initial image:



    enter image description here



    My final image:



    enter image description here










    share|improve this question



























      0












      0








      0








      I have implemented the naive version of non local means without integral images.
      First image acquisition:



      row = 10;
      col = 10;

      % Patchsize - make sure if code works for different values
      patchSize = 4;

      % Search window size - make sure if code works for different values
      searchWindowSize = 10;

      image = imread('r.jpg');
      new=imnoise(image,'gaussian');
      imshow(new)
      ws=floor(searchWindowSize/2);
      ps=floor(patchSize/2);
      new= padarray(new, [searchWindowSize,searchWindowSize], 'both');
      new= padarray(new, [patchSize,patchSize], 'both');


      Now, the main non local means function:



      unew=zeros(size(new,1),size(new,2));
      for i=1+ws+ps:size(new,1)-ws-ps
      for j=1+ws+ps:size(new,2)-ws-ps
      sear=new(i-ws:i+ws,j-ws:j+ws);
      %imshow(new(i-ws:i+ws,j-ws:j+ws))
      sear=padarray(sear,[ps,ps],'both');
      patch=new(i-ps:i+ps,j-ps:j+ps);
      dis=;
      ind=1;
      sums=0;
      for ii=1+ps:size(sear,1)-ps
      for jj=1+ps:size(sear,1)-ps
      patchn=new(ii-ps:ii+ps,jj-ps:jj+ps);
      ssd=(patchn-patch).*2;
      psum=0;
      for mj=1:patchSize
      for gj=1:patchSize
      psum= psum+ssd(mj,gj);
      end
      end
      dis(ind)=psum;
      sums=sums+exp(-max(dis(ind)*dis(ind)-2*0.1*0.1,0)/0.75*0.75);

      ind=ind+1;
      end
      end
      new(i,j)=sums;
      end
      end


      My initial image:



      enter image description here



      My final image:



      enter image description here










      share|improve this question
















      I have implemented the naive version of non local means without integral images.
      First image acquisition:



      row = 10;
      col = 10;

      % Patchsize - make sure if code works for different values
      patchSize = 4;

      % Search window size - make sure if code works for different values
      searchWindowSize = 10;

      image = imread('r.jpg');
      new=imnoise(image,'gaussian');
      imshow(new)
      ws=floor(searchWindowSize/2);
      ps=floor(patchSize/2);
      new= padarray(new, [searchWindowSize,searchWindowSize], 'both');
      new= padarray(new, [patchSize,patchSize], 'both');


      Now, the main non local means function:



      unew=zeros(size(new,1),size(new,2));
      for i=1+ws+ps:size(new,1)-ws-ps
      for j=1+ws+ps:size(new,2)-ws-ps
      sear=new(i-ws:i+ws,j-ws:j+ws);
      %imshow(new(i-ws:i+ws,j-ws:j+ws))
      sear=padarray(sear,[ps,ps],'both');
      patch=new(i-ps:i+ps,j-ps:j+ps);
      dis=;
      ind=1;
      sums=0;
      for ii=1+ps:size(sear,1)-ps
      for jj=1+ps:size(sear,1)-ps
      patchn=new(ii-ps:ii+ps,jj-ps:jj+ps);
      ssd=(patchn-patch).*2;
      psum=0;
      for mj=1:patchSize
      for gj=1:patchSize
      psum= psum+ssd(mj,gj);
      end
      end
      dis(ind)=psum;
      sums=sums+exp(-max(dis(ind)*dis(ind)-2*0.1*0.1,0)/0.75*0.75);

      ind=ind+1;
      end
      end
      new(i,j)=sums;
      end
      end


      My initial image:



      enter image description here



      My final image:



      enter image description here







      matlab image-processing noise-reduction






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 '18 at 14:33









      Cris Luengo

      19.8k52249




      19.8k52249










      asked Nov 18 '18 at 12:00









      Ujjayanta BhaumikUjjayanta Bhaumik

      346




      346
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There are many issues with the code. Here is a non-exhaustive list:




          • You don’t do anything with color, it looks like your code is written for a grey-value image. But you put a color image through it. Consequently, only the red channel is modified.


          • You write into the same image you read from. You cannot do this, as you’re using a combination of input and output pixels to generate the next output pixel. Instead, read from one image, write to another one.


          • A weighted mean is computed by summing value*weight, and then dividing by the sum of the weights at the end. You don’t do this division.


          • You crop your search area from your image, then pad it. This makes no sense. You should crop a larger region from your image.


          • You keep an array dis, you write to it, but you don’t make use of it. That is, you write a new element, then only read this new element; you never go back and look at the older values. Why does this array exist?


          • Your inner two loops can be written as psum = sum(ssd(:)).


          • ssd is twice the difference. You probably want to look at the square difference, or the absolute difference.







          share|improve this answer
























          • ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

            – Ujjayanta Bhaumik
            Nov 18 '18 at 23:07













          • I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

            – Ujjayanta Bhaumik
            Nov 19 '18 at 6:29






          • 1





            @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

            – Cris Luengo
            Nov 19 '18 at 6:48











          • Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

            – Cris Luengo
            Nov 19 '18 at 6:51











          • Thank you so much. Could you suggest me a good source for non local means using integral images too?

            – Ujjayanta Bhaumik
            Nov 19 '18 at 9:46











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360631%2fwhy-does-my-denoised-image-get-darker-after-using-non-local-means-algorithm-and%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There are many issues with the code. Here is a non-exhaustive list:




          • You don’t do anything with color, it looks like your code is written for a grey-value image. But you put a color image through it. Consequently, only the red channel is modified.


          • You write into the same image you read from. You cannot do this, as you’re using a combination of input and output pixels to generate the next output pixel. Instead, read from one image, write to another one.


          • A weighted mean is computed by summing value*weight, and then dividing by the sum of the weights at the end. You don’t do this division.


          • You crop your search area from your image, then pad it. This makes no sense. You should crop a larger region from your image.


          • You keep an array dis, you write to it, but you don’t make use of it. That is, you write a new element, then only read this new element; you never go back and look at the older values. Why does this array exist?


          • Your inner two loops can be written as psum = sum(ssd(:)).


          • ssd is twice the difference. You probably want to look at the square difference, or the absolute difference.







          share|improve this answer
























          • ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

            – Ujjayanta Bhaumik
            Nov 18 '18 at 23:07













          • I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

            – Ujjayanta Bhaumik
            Nov 19 '18 at 6:29






          • 1





            @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

            – Cris Luengo
            Nov 19 '18 at 6:48











          • Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

            – Cris Luengo
            Nov 19 '18 at 6:51











          • Thank you so much. Could you suggest me a good source for non local means using integral images too?

            – Ujjayanta Bhaumik
            Nov 19 '18 at 9:46
















          1














          There are many issues with the code. Here is a non-exhaustive list:




          • You don’t do anything with color, it looks like your code is written for a grey-value image. But you put a color image through it. Consequently, only the red channel is modified.


          • You write into the same image you read from. You cannot do this, as you’re using a combination of input and output pixels to generate the next output pixel. Instead, read from one image, write to another one.


          • A weighted mean is computed by summing value*weight, and then dividing by the sum of the weights at the end. You don’t do this division.


          • You crop your search area from your image, then pad it. This makes no sense. You should crop a larger region from your image.


          • You keep an array dis, you write to it, but you don’t make use of it. That is, you write a new element, then only read this new element; you never go back and look at the older values. Why does this array exist?


          • Your inner two loops can be written as psum = sum(ssd(:)).


          • ssd is twice the difference. You probably want to look at the square difference, or the absolute difference.







          share|improve this answer
























          • ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

            – Ujjayanta Bhaumik
            Nov 18 '18 at 23:07













          • I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

            – Ujjayanta Bhaumik
            Nov 19 '18 at 6:29






          • 1





            @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

            – Cris Luengo
            Nov 19 '18 at 6:48











          • Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

            – Cris Luengo
            Nov 19 '18 at 6:51











          • Thank you so much. Could you suggest me a good source for non local means using integral images too?

            – Ujjayanta Bhaumik
            Nov 19 '18 at 9:46














          1












          1








          1







          There are many issues with the code. Here is a non-exhaustive list:




          • You don’t do anything with color, it looks like your code is written for a grey-value image. But you put a color image through it. Consequently, only the red channel is modified.


          • You write into the same image you read from. You cannot do this, as you’re using a combination of input and output pixels to generate the next output pixel. Instead, read from one image, write to another one.


          • A weighted mean is computed by summing value*weight, and then dividing by the sum of the weights at the end. You don’t do this division.


          • You crop your search area from your image, then pad it. This makes no sense. You should crop a larger region from your image.


          • You keep an array dis, you write to it, but you don’t make use of it. That is, you write a new element, then only read this new element; you never go back and look at the older values. Why does this array exist?


          • Your inner two loops can be written as psum = sum(ssd(:)).


          • ssd is twice the difference. You probably want to look at the square difference, or the absolute difference.







          share|improve this answer













          There are many issues with the code. Here is a non-exhaustive list:




          • You don’t do anything with color, it looks like your code is written for a grey-value image. But you put a color image through it. Consequently, only the red channel is modified.


          • You write into the same image you read from. You cannot do this, as you’re using a combination of input and output pixels to generate the next output pixel. Instead, read from one image, write to another one.


          • A weighted mean is computed by summing value*weight, and then dividing by the sum of the weights at the end. You don’t do this division.


          • You crop your search area from your image, then pad it. This makes no sense. You should crop a larger region from your image.


          • You keep an array dis, you write to it, but you don’t make use of it. That is, you write a new element, then only read this new element; you never go back and look at the older values. Why does this array exist?


          • Your inner two loops can be written as psum = sum(ssd(:)).


          • ssd is twice the difference. You probably want to look at the square difference, or the absolute difference.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 14:47









          Cris LuengoCris Luengo

          19.8k52249




          19.8k52249













          • ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

            – Ujjayanta Bhaumik
            Nov 18 '18 at 23:07













          • I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

            – Ujjayanta Bhaumik
            Nov 19 '18 at 6:29






          • 1





            @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

            – Cris Luengo
            Nov 19 '18 at 6:48











          • Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

            – Cris Luengo
            Nov 19 '18 at 6:51











          • Thank you so much. Could you suggest me a good source for non local means using integral images too?

            – Ujjayanta Bhaumik
            Nov 19 '18 at 9:46



















          • ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

            – Ujjayanta Bhaumik
            Nov 18 '18 at 23:07













          • I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

            – Ujjayanta Bhaumik
            Nov 19 '18 at 6:29






          • 1





            @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

            – Cris Luengo
            Nov 19 '18 at 6:48











          • Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

            – Cris Luengo
            Nov 19 '18 at 6:51











          • Thank you so much. Could you suggest me a good source for non local means using integral images too?

            – Ujjayanta Bhaumik
            Nov 19 '18 at 9:46

















          ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

          – Ujjayanta Bhaumik
          Nov 18 '18 at 23:07







          ideone.com/P1Kqbg I fixed the first two points. Could you help me fix the third point(weighted mean)?

          – Ujjayanta Bhaumik
          Nov 18 '18 at 23:07















          I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

          – Ujjayanta Bhaumik
          Nov 19 '18 at 6:29





          I implemented the weighing too. Could you please suggest if my code is correct?: ideone.com/7497Ht

          – Ujjayanta Bhaumik
          Nov 19 '18 at 6:29




          1




          1





          @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

          – Cris Luengo
          Nov 19 '18 at 6:48





          @UjjayantaBhaumik: Instead of rcn(i,j)=rcn(i,j)-sums/sums2, I think you need to do just rcn(i,j)=sums/sums2. But you now have new and unew that are not used, so you're reading from the unpadded input image instead of the padded new. And your patchn doesn't take into account i and j. There's a lot of work to be done here... Also, please, don't copy-paste the same code 3 times, write a function and call it 3 times. It's a bit more work upfront, but a lot less work down the line.

          – Cris Luengo
          Nov 19 '18 at 6:48













          Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

          – Cris Luengo
          Nov 19 '18 at 6:51





          Not that this is necessarily good code, but you could compare your implementation to the one by the authors of the original non-local means paper: mathworks.com/matlabcentral/fileexchange/…

          – Cris Luengo
          Nov 19 '18 at 6:51













          Thank you so much. Could you suggest me a good source for non local means using integral images too?

          – Ujjayanta Bhaumik
          Nov 19 '18 at 9:46





          Thank you so much. Could you suggest me a good source for non local means using integral images too?

          – Ujjayanta Bhaumik
          Nov 19 '18 at 9:46


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53360631%2fwhy-does-my-denoised-image-get-darker-after-using-non-local-means-algorithm-and%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)