Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Bitmap blur problem [modified]

Bitmap blur problem [modified]

Scheduled Pinned Locked Moved Graphics
helpgraphicsdata-structures
32 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Naturality

    GAHH!!! Another grey and black screen! Here's the current file.[^]

    "Sir, I protest. I am NOT a merry man!"

    T Offline
    T Offline
    Tim Craig
    wrote on last edited by
    #14

    Ok, a few things for you to think about while I'm trying to figure out your pixel indexing scheme. You read the second file, the output file, into memory. Why? You simply want to calculate the new image and write it? You never free the memory you allocated for the two image arrays. Won't kill you in this instance but bad practice. It doesn't look like you close the output file before you exit. I think Windows will do this for you but I never trust Windows to do anything I can do explicitly. It saves big teeth marks in your ass if it doesn't actually do what it's advertised to do. I'll get back to you on the rest of it.

    If you don't have the data, you're just another asshole with an opinion.

    1 Reply Last reply
    0
    • N Naturality

      GAHH!!! Another grey and black screen! Here's the current file.[^]

      "Sir, I protest. I am NOT a merry man!"

      T Offline
      T Offline
      Tim Craig
      wrote on last edited by
      #15

      Ok, courtesy of Wikipedia I've brushed up on Gaussian blurring. :cool: You're trying to replace each pixel, well at last those not on an edge with a weighted average of itself and the 8 pixels surrounding it. You have your weights in the array and are multiplying the pixel by the weight and diving by the sum of the weights (16). Two things. First and most glaring is why do you divide the blurred values by "size", 9 on your case? That's going to make things dark as you just cut each pixel value to 1/9 of its value, something less than 23 maximum out of the possible 255 if I did that right. Second, you'd be more accurate to do the division after you've fully accumulated the 9 weighted pixel values. The way you have it, you get a little truncation for each of the 9. If you wait until the end, you only get the truncation once. So get rid of the division by 16 in the loop and replace "size" by 16 at the end. Now to figure out how you're getting the 9 pixels to average. All I know for sure is that I'm 99.9% sure what you're doing isn't right. :suss:

      If you don't have the data, you're just another asshole with an opinion.

      N 1 Reply Last reply
      0
      • T Tim Craig

        Ok, courtesy of Wikipedia I've brushed up on Gaussian blurring. :cool: You're trying to replace each pixel, well at last those not on an edge with a weighted average of itself and the 8 pixels surrounding it. You have your weights in the array and are multiplying the pixel by the weight and diving by the sum of the weights (16). Two things. First and most glaring is why do you divide the blurred values by "size", 9 on your case? That's going to make things dark as you just cut each pixel value to 1/9 of its value, something less than 23 maximum out of the possible 255 if I did that right. Second, you'd be more accurate to do the division after you've fully accumulated the 9 weighted pixel values. The way you have it, you get a little truncation for each of the 9. If you wait until the end, you only get the truncation once. So get rid of the division by 16 in the loop and replace "size" by 16 at the end. Now to figure out how you're getting the 9 pixels to average. All I know for sure is that I'm 99.9% sure what you're doing isn't right. :suss:

        If you don't have the data, you're just another asshole with an opinion.

        N Offline
        N Offline
        Naturality
        wrote on last edited by
        #16

        Well, this those changes, it now comes out red and black instead of grey and black. I'm Sure I'm doing it wrong too. :sigh:

        "Sir, I protest. I am NOT a merry man!"

        C 1 Reply Last reply
        0
        • N Naturality

          Well, this those changes, it now comes out red and black instead of grey and black. I'm Sure I'm doing it wrong too. :sigh:

          "Sir, I protest. I am NOT a merry man!"

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #17

          If the colors are changing, then what you're doing wrong is your pixel lookup, the colors are bleeding between red, green and blue. Did you read my article which has a guassian blur filter in it ?

          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          N 1 Reply Last reply
          0
          • C Christian Graus

            If the colors are changing, then what you're doing wrong is your pixel lookup, the colors are bleeding between red, green and blue. Did you read my article which has a guassian blur filter in it ?

            Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            N Offline
            N Offline
            Naturality
            wrote on last edited by
            #18

            Yes, I did. However, I don't see how my pixel lookup is wrong. :S

            "Sir, I protest. I am NOT a merry man!"

            T 1 Reply Last reply
            0
            • N Naturality

              Yes, I did. However, I don't see how my pixel lookup is wrong. :S

              "Sir, I protest. I am NOT a merry man!"

              T Offline
              T Offline
              Tim Craig
              wrote on last edited by
              #19

              Ok, for starters I believe you're scanning the input image correctly now, x and y cover the correct pixels. However, you want to grab 3 pixels from the previous row right above the target pixel, three from the current row including the two on either side of the target, and the from the next row. So xp and yp have to take the values, [-1, 0, 1]. You start with 0 and your test of xp < 4 actually makes xp go from 0, 1, 2, 3. So you're getting the wrong pixel correlated. Also, after you call avg(), you should only be setting 1 pixel in the output image, the one at x, y. So the second set of for loops need to vanish.

              If you don't have the data, you're just another asshole with an opinion.

              N 1 Reply Last reply
              0
              • T Tim Craig

                Ok, for starters I believe you're scanning the input image correctly now, x and y cover the correct pixels. However, you want to grab 3 pixels from the previous row right above the target pixel, three from the current row including the two on either side of the target, and the from the next row. So xp and yp have to take the values, [-1, 0, 1]. You start with 0 and your test of xp < 4 actually makes xp go from 0, 1, 2, 3. So you're getting the wrong pixel correlated. Also, after you call avg(), you should only be setting 1 pixel in the output image, the one at x, y. So the second set of for loops need to vanish.

                If you don't have the data, you're just another asshole with an opinion.

                N Offline
                N Offline
                Naturality
                wrote on last edited by
                #20

                Thanks for the advice :D. But now, the output is just red and black lines :doh: Here's the updated code:

                unsigned int yp=0, xp=0;

                for(int y = 1; y < bmp.biHeight-1; y++){
                for(int x = 1; x < bmp.biWidth-1; x++){
                for(int i = 0; i < 9; i++){
                {
                get_pixel(x+xp,y+yp,blank[i],image);
                if(xp<2)
                xp++;
                else{
                xp = -1;
                yp++;}
                }
                }
                avg(blank, Merge, 9, Matrix);
                set_pixel(x,y,Merge,image2);
                clear_pixel(Merge);
                yp=-1, xp=-1;

                }}
                

                "Sir, I protest. I am NOT a merry man!"

                C T 3 Replies Last reply
                0
                • N Naturality

                  Thanks for the advice :D. But now, the output is just red and black lines :doh: Here's the updated code:

                  unsigned int yp=0, xp=0;

                  for(int y = 1; y < bmp.biHeight-1; y++){
                  for(int x = 1; x < bmp.biWidth-1; x++){
                  for(int i = 0; i < 9; i++){
                  {
                  get_pixel(x+xp,y+yp,blank[i],image);
                  if(xp<2)
                  xp++;
                  else{
                  xp = -1;
                  yp++;}
                  }
                  }
                  avg(blank, Merge, 9, Matrix);
                  set_pixel(x,y,Merge,image2);
                  clear_pixel(Merge);
                  yp=-1, xp=-1;

                  }}
                  

                  "Sir, I protest. I am NOT a merry man!"

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #21

                  Well, this is sure messy. Apart from anything else, all these function calls are going to slow your code down. avg works on a reference, it doesn't return a value ? yp and xp are zero at the start of the first loop, every other time they start at -1. I'm sure that's not the only issue, but it's an indication of the sort of problems you probably have.

                  Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                  1 Reply Last reply
                  0
                  • N Naturality

                    Thanks for the advice :D. But now, the output is just red and black lines :doh: Here's the updated code:

                    unsigned int yp=0, xp=0;

                    for(int y = 1; y < bmp.biHeight-1; y++){
                    for(int x = 1; x < bmp.biWidth-1; x++){
                    for(int i = 0; i < 9; i++){
                    {
                    get_pixel(x+xp,y+yp,blank[i],image);
                    if(xp<2)
                    xp++;
                    else{
                    xp = -1;
                    yp++;}
                    }
                    }
                    avg(blank, Merge, 9, Matrix);
                    set_pixel(x,y,Merge,image2);
                    clear_pixel(Merge);
                    yp=-1, xp=-1;

                    }}
                    

                    "Sir, I protest. I am NOT a merry man!"

                    T Offline
                    T Offline
                    Tim Craig
                    wrote on last edited by
                    #22

                    Christian is right about the initialization of xp and yp. And the xp<2 test is wrong. It's short, count it out in your fingers. :) He's right about it being very messy and inefficient. But right now it's more important for you to understand the algorithm.

                    If you don't have the data, you're just another asshole with an opinion.

                    1 Reply Last reply
                    0
                    • N Naturality

                      Thanks for the advice :D. But now, the output is just red and black lines :doh: Here's the updated code:

                      unsigned int yp=0, xp=0;

                      for(int y = 1; y < bmp.biHeight-1; y++){
                      for(int x = 1; x < bmp.biWidth-1; x++){
                      for(int i = 0; i < 9; i++){
                      {
                      get_pixel(x+xp,y+yp,blank[i],image);
                      if(xp<2)
                      xp++;
                      else{
                      xp = -1;
                      yp++;}
                      }
                      }
                      avg(blank, Merge, 9, Matrix);
                      set_pixel(x,y,Merge,image2);
                      clear_pixel(Merge);
                      yp=-1, xp=-1;

                      }}
                      

                      "Sir, I protest. I am NOT a merry man!"

                      T Offline
                      T Offline
                      Tim Craig
                      wrote on last edited by
                      #23

                      Oh, one other question. You apparently assume that the pixels are RGB triples. You never check the bitmap info to make sure it matches. Were you told this is the structure of the image?

                      If you don't have the data, you're just another asshole with an opinion.

                      C 1 Reply Last reply
                      0
                      • T Tim Craig

                        Oh, one other question. You apparently assume that the pixels are RGB triples. You never check the bitmap info to make sure it matches. Were you told this is the structure of the image?

                        If you don't have the data, you're just another asshole with an opinion.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #24

                        Yeah, I spotted that, too. But, I assume it is, if it were 32 bit, he'd be complaining about pixels not processed, I reckon.

                        Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                        N 2 Replies Last reply
                        0
                        • C Christian Graus

                          Yeah, I spotted that, too. But, I assume it is, if it were 32 bit, he'd be complaining about pixels not processed, I reckon.

                          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                          N Offline
                          N Offline
                          Naturality
                          wrote on last edited by
                          #25

                          I spotted the initialisation of xp and yp straight after posting. But it had no effect. :( I wasn't aware you could check if it was an RGBTRIPLE. Ignorance, I guess. How would I go about this? However, the other program I am using to create the random noise input image seems to work fine with the same values in the header BITMAPINFOHEADER and BITMAPFILEHEADERs. Oh, and, rather ironically, I used so many function calls to try and clean the for loops up. :^)

                          "Sir, I protest. I am NOT a merry man!"

                          T 1 Reply Last reply
                          0
                          • N Naturality

                            I spotted the initialisation of xp and yp straight after posting. But it had no effect. :( I wasn't aware you could check if it was an RGBTRIPLE. Ignorance, I guess. How would I go about this? However, the other program I am using to create the random noise input image seems to work fine with the same values in the header BITMAPINFOHEADER and BITMAPFILEHEADERs. Oh, and, rather ironically, I used so many function calls to try and clean the for loops up. :^)

                            "Sir, I protest. I am NOT a merry man!"

                            T Offline
                            T Offline
                            Tim Craig
                            wrote on last edited by
                            #26

                            Have you tried stepping through your code in the debugger to see if things actually work like you're expecting them to? I would have been doing that about 14 messages ago. :doh:

                            If you don't have the data, you're just another asshole with an opinion.

                            N 1 Reply Last reply
                            0
                            • T Tim Craig

                              Have you tried stepping through your code in the debugger to see if things actually work like you're expecting them to? I would have been doing that about 14 messages ago. :doh:

                              If you don't have the data, you're just another asshole with an opinion.

                              N Offline
                              N Offline
                              Naturality
                              wrote on last edited by
                              #27

                              Hmm.... xp is set to 4294967295 and does not change with each loop... y, however, does as is wanted.

                              "Sir, I protest. I am NOT a merry man!"

                              N 1 Reply Last reply
                              0
                              • N Naturality

                                Hmm.... xp is set to 4294967295 and does not change with each loop... y, however, does as is wanted.

                                "Sir, I protest. I am NOT a merry man!"

                                N Offline
                                N Offline
                                Naturality
                                wrote on last edited by
                                #28

                                Okay, fixed that. Still get the same output, though.

                                "Sir, I protest. I am NOT a merry man!"

                                1 Reply Last reply
                                0
                                • C Christian Graus

                                  Yeah, I spotted that, too. But, I assume it is, if it were 32 bit, he'd be complaining about pixels not processed, I reckon.

                                  Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                                  N Offline
                                  N Offline
                                  Naturality
                                  wrote on last edited by
                                  #29

                                  Hmm.... I tried recreating the file using the other program(which uses the same values in bmp and bfh) using the CREATE_NEW definition in the CreateFile() function. When I do this, the resulting image cannot be drawn by windows picture and fax viewer. This may be the problem...

                                  "Sir, I protest. I am NOT a merry man!"

                                  T 2 Replies Last reply
                                  0
                                  • N Naturality

                                    Hmm.... I tried recreating the file using the other program(which uses the same values in bmp and bfh) using the CREATE_NEW definition in the CreateFile() function. When I do this, the resulting image cannot be drawn by windows picture and fax viewer. This may be the problem...

                                    "Sir, I protest. I am NOT a merry man!"

                                    T Offline
                                    T Offline
                                    Tim Craig
                                    wrote on last edited by
                                    #30

                                    Sounds like a clue to me. :suss: I'm not really up on that kind of file I/O. I rarely store things these days.

                                    If you don't have the data, you're just another asshole with an opinion.

                                    1 Reply Last reply
                                    0
                                    • N Naturality

                                      Hmm.... I tried recreating the file using the other program(which uses the same values in bmp and bfh) using the CREATE_NEW definition in the CreateFile() function. When I do this, the resulting image cannot be drawn by windows picture and fax viewer. This may be the problem...

                                      "Sir, I protest. I am NOT a merry man!"

                                      T Offline
                                      T Offline
                                      Tim Craig
                                      wrote on last edited by
                                      #31

                                      You've gone quiet. Did you ever get it to work? Inquiring minds want to know.

                                      If you don't have the data, you're just another asshole with an opinion.

                                      N 1 Reply Last reply
                                      0
                                      • T Tim Craig

                                        You've gone quiet. Did you ever get it to work? Inquiring minds want to know.

                                        If you don't have the data, you're just another asshole with an opinion.

                                        N Offline
                                        N Offline
                                        Naturality
                                        wrote on last edited by
                                        #32

                                        Unfortunately I haven't got it to work yet. I shall keep at it, however.

                                        "Sir, I protest. I am NOT a merry man!"

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups