Search code examples
c#opencvparametershistogramemgucv

EMGU CV reassigned image


I try to use two image parameters to control my image in project. The problem is that when I cannot reassign image after applying any public void functions in Emgu CV. This is my code:

public static class Global
{
    public static Image<Gray, byte> xrayPic;
    public static Image<Gray, byte> rootPic;
}

private void takePhotoBtn_Click(object sender, EventArgs e)
{
    Image<Bgr, Byte> ImageSrc = new Image<Bgr, Byte>(_subPath);
    Image<Gray, Byte> GrayImage = ImageSrc.Convert<Gray, byte>();
    Image<Gray, Byte> MedianImage = GrayImage.SmoothMedian(5);

    Global.xrayPic = MedianImage;
    Global.rootPic = MedianImage;

    Global.xrayPic.Save(_subPath);
    imgBox.Image.Dispose();
    imgBox.Image = Global.xrayPic.Bitmap;    
}

private void checkHistogram_CheckedChanged(object sender, EventArgs e)
{
    if(checkHistogram.Checked)
    {
        Image<Gray, byte> tmpPic = Global.xrayPic;
        tmpPic._EqualizeHist();
        // Global.xrayPic._EqualizeHist();
        imgBox.Image.Dispose();
        imgBox.Image = tmpPic.Bitmap;
    }

    if(checkHistogram.Checked == false)
    {
        Global.xrayPic = Global.rootPic;
        imgBox.Image.Dispose();
        imgBox.Image = Global.xrayPic.Bitmap;
    }
}

When I check to the checkbox to apply the function __EqualizeHist(), it applied function automatically to adjust first pic to second pic (like attached image). However, when I uncheck, It does not return to my root_Pic (second pic to first pic) This is the demonstration for my code


Solution

  • The problem is when you copy images like this

    Global.xrayPic = Global.rootPic;
    

    then the reference of Global.rootPic is copied to Global.xrayPic which means both objects will point to the same image in the memory, any changes to either Global.rootPic or Global.xrayPic will result in change in both.

    Solution:

    Use the deep copy of the images like this

    Global.xrayPic = Global.rootPic.Clone();
    

    If you want to copy emgucv image from 1 variable to another it is always a good idea to Clone them.

    I hope this will resolve your problem if you have any other problem them copy here.