none
Two method parameter (Bitmap) points same image RRS feed

  • Question

  • Hi,

    i have a method which performs something about image processing. I need two inputs. But when i run my code, i see that two images are same. But i choose (i think) two different images. I don't know what is the problem. 

    my main function

    private void sliderKernel_MouseUp(object sender, MouseEventArgs e) {

     Filtreler f1 = new Filtreler();
     f1.Img = new Bitmap(pBox_SOURCE.Image);
     int SablonBoyutu = sliderKernel.Value;
     Bitmap MeanImg = f1.meanfilter(SablonBoyutu, f1.Img); //this method works well.
     pBox_PROCESSED.Image = MeanImg;
     f1.CorrectShade(f1.Img, out MeanImg); // I tried to use MeanImg here
     pBoxShadeCorrection.Image = MeanImg;  

    }

    my methods

     public Bitmap meanfilter(int SablonBoyutu, Bitmap GirisResmi)
            {
                Bitmap CikisResmi = new Bitmap(GirisResmi.Width, GirisResmi.Height);
                int ResimGenisligi = Img.Width;
                int ResimYuksekligi = Img.Height;
                int x, y, i, j, toplamR, toplamG, toplamB, ortalamaR, ortalamaG, ortalamaB;
                int kernelsinir = ((SablonBoyutu - 1) / 2);
                for (x = (SablonBoyutu - 1) / 2; x < ResimGenisligi - (SablonBoyutu - 1) / 2; x++)
                {
                    for (y = (SablonBoyutu - 1) / 2; y < ResimYuksekligi - (SablonBoyutu - 1) / 2; y++)
                    {
                        toplamR = 0;
                        toplamG = 0;
                        toplamB = 0;
                        for (i = -((SablonBoyutu - 1) / 2); i <= (SablonBoyutu - 1) / 2; i++)
                        {
                            for (j = -((SablonBoyutu - 1) / 2); j <= (SablonBoyutu - 1) / 2; j++)
                            {
                                OkunanRenk = Img.GetPixel(x + i, y + j);
                                toplamR = toplamR + OkunanRenk.R;
                                toplamG = toplamG + OkunanRenk.G;
                                toplamB = toplamB + OkunanRenk.B;
                            }
                        }
                        ortalamaR = (toplamR / (SablonBoyutu * SablonBoyutu));
                        ortalamaG = (toplamG / (SablonBoyutu * SablonBoyutu));
                        ortalamaB = (toplamB / (SablonBoyutu * SablonBoyutu));
                       
                        CikisResmi.SetPixel(x, y, Color.FromArgb(ortalamaR, ortalamaG, ortalamaB));
                    }
                }
                return CikisResmi;
            }

    public Bitmap CorrectShade(Bitmap GirisResmi,out Bitmap BulanikResim)
    {
            Color OkunanRenkBulanik;
            int topR, topG, topB,x,y;
            int ResimGenisligi = GirisResmi.Width;
            int ResimYuksekligi = GirisResmi.Height;
            for (x = 0; x < ResimGenisligi; x++)
            {
                for (y = 0; y < ResimYuksekligi; y++)
                {
                    OkunanRenkBulanik = BulanikResim.GetPixel(x, y);
                    OkunanRenk = GirisResmi.GetPixel(x, y);
                    topR = Convert.ToInt16(OkunanRenk.R / OkunanRenkBulanik.R+1); 
                    BulanikResim.SetPixel(x, y, Color.FromArgb(topR, topR, topR));
                }
            }
            return BulanikResim;
    }


    and my property 

    public Bitmap Img { get; set; }
    
    public Color OkunanRenk { get; set; }






    Tuesday, July 30, 2019 5:11 PM

Answers

  • My guess is that pBoxShadeCorrection comes out all black.

    I can't tell what you intended for CorrectShadeBitmap to do, but consider this.  We know that the two bitmaps are going to be very close to each other, since one is just a slightly averaged version of the other.  Remember that Color.R, Color.G, and Color.B are all integers.  That means these computations:

            topR = Convert.ToInt16(OkunanRenk.R / (OkunanRenkBulanik.R+1));
            topG = Convert.ToInt16(OkunanRenk.G / (OkunanRenkBulanik.G+1));
            topB = Convert.ToInt16(OkunanRenk.B / (OkunanRenkBulanik.B+1));

    will all be done as integer division, which truncates.  Since the two colors are so close, the results are almost always going to be either 0 or 1.  (128/129 is zero, for example.  129/128 will be 1.)  And a bitmap filled with RGB(0,0,0) or RGB(1,1,1) is going to appear black.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by MustafaUysal Sunday, August 4, 2019 7:31 PM
    Wednesday, July 31, 2019 7:17 AM

All replies

  • Does f1.meanfilter change the bitmap it is passed?  What you're passing to CorrectShade is the original bitmap from pBox_SOURCE and the bitmap stored in pBox_PROCESSED (which was passed to f1.meanfilter).  Is that what you expect?

    Are you looking for differences just by eye?  Are you sure your filters are making enough of a difference to be visible?

    If you want to put your complete project somewhere and post a link, I'll take a look, but we can't tell from this little snippet what might be going on.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, July 30, 2019 5:44 PM
  • Does f1.meanfilter change the bitmap it is passed?  What you're passing to CorrectShade is the original bitmap from pBox_SOURCE and the bitmap stored in pBox_PROCESSED (which was passed to f1.meanfilter).  Is that what you expect?

    Are you looking for differences just by eye?  Are you sure your filters are making enough of a difference to be visible?

    If you want to put your complete project somewhere and post a link, I'll take a look, but we can't tell from this little snippet what might be going on.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    edited question and put my codes.
    Wednesday, July 31, 2019 5:39 AM
  • My guess is that pBoxShadeCorrection comes out all black.

    I can't tell what you intended for CorrectShadeBitmap to do, but consider this.  We know that the two bitmaps are going to be very close to each other, since one is just a slightly averaged version of the other.  Remember that Color.R, Color.G, and Color.B are all integers.  That means these computations:

            topR = Convert.ToInt16(OkunanRenk.R / (OkunanRenkBulanik.R+1));
            topG = Convert.ToInt16(OkunanRenk.G / (OkunanRenkBulanik.G+1));
            topB = Convert.ToInt16(OkunanRenk.B / (OkunanRenkBulanik.B+1));

    will all be done as integer division, which truncates.  Since the two colors are so close, the results are almost always going to be either 0 or 1.  (128/129 is zero, for example.  129/128 will be 1.)  And a bitmap filled with RGB(0,0,0) or RGB(1,1,1) is going to appear black.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by MustafaUysal Sunday, August 4, 2019 7:31 PM
    Wednesday, July 31, 2019 7:17 AM
  • My guess is that pBoxShadeCorrection comes out all black.

    I can't tell what you intended for CorrectShadeBitmap to do, but consider this.  We know that the two bitmaps are going to be very close to each other, since one is just a slightly averaged version of the other.  Remember that Color.R, Color.G, and Color.B are all integers.  That means these computations:

            topR = Convert.ToInt16(OkunanRenk.R / (OkunanRenkBulanik.R+1));
            topG = Convert.ToInt16(OkunanRenk.G / (OkunanRenkBulanik.G+1));
            topB = Convert.ToInt16(OkunanRenk.B / (OkunanRenkBulanik.B+1));

    will all be done as integer division, which truncates.  Since the two colors are so close, the results are almost always going to be either 0 or 1.  (128/129 is zero, for example.  129/128 will be 1.)  And a bitmap filled with RGB(0,0,0) or RGB(1,1,1) is going to appear black.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    You are right but my problem is not that. I see that two image point same memory location.

    I am triying to get two inputs. Firstly , output of meanfilter (MeanImg) (BulanikResim), secondly original image (f1.img) (GirisResmi). But debugger says my two input are same.


    Wednesday, July 31, 2019 7:40 AM
  • Where in the C# debugger did you see that the point to the same memory location?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, July 31, 2019 9:02 PM
  • Where in the C# debugger did you see that the point to the same memory location?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    https://i.hizliresim.com/Lvn1NZ.jpg


    Thursday, August 1, 2019 8:53 AM
  • That link comes up as "403 Forbidden".

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, August 1, 2019 7:14 PM
  • Hello,

     From looking at the method signatures, all the Bitmap object are passed as

    copy and not reference.  Any internal changes will have no effect unless you

    use the ref keyword. Or the Bitmap object are scoped locally.    Your code

    must give methods access to objects via scope or keywords out and ref.

     

     Hope this helps :)

    • Marked as answer by MustafaUysal Friday, August 2, 2019 6:51 PM
    • Unmarked as answer by MustafaUysal Friday, August 2, 2019 6:52 PM
    Thursday, August 1, 2019 7:44 PM
  • This is simply not true.  It is passing a copy, but it's a copy of the object reference, and that copy points to the exact same object.  It is most certainly NOT making a copy of the bitmap.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, August 1, 2019 9:44 PM
  • Hello,

     From looking at the method signatures, all the Bitmap object are passed as

    copy and not reference.  Any internal changes will have no effect unless you

    use the ref keyword. Or the Bitmap object are scoped locally.    Your code

    must give methods access to objects via scope or keywords out and ref.

     

     Hope this helps :)

    Thank you, i tried to ouse out keyword but i get an error:

    Error	CS0177	The out parameter 'BulanikResim' must be assigned to before control leaves the current method	

    Saturday, August 3, 2019 3:37 PM
  • That link comes up as "403 Forbidden".

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    I am sorry here it is

    https://ibb.co/ZVThX09


    Sunday, August 4, 2019 7:19 AM
  • Do not follow User3DX's advice.  He is wrong.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, August 5, 2019 5:25 AM
  • That image does not match any of the code you have posted so far.  We cannot help you unless you show us the code that is not working, along with the results you got and the results you expected. 

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, August 5, 2019 5:29 AM