EmguCV学习(二)

本文介绍了使用EmguCV进行图像处理的几个关键步骤,包括图像锐化、特定颜色检测、HSV空间的皮肤检测、直方图显示及其问题解决。详细讨论了直方图计算中的RangeF范围问题,以及如何利用DenseHistogram类进行直方图生成。此外,还展示了读取不同通道图像并生成直方图的功能,以及实现了颜色反转的查找表方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)图像锐化处理


//此部分代码功能是锐化图像,本质上是 扫描图像并访问相邻像素
//sharpened_pixel=5*current-left-right-up-down
byte saturateCast(double inValue)
{
    if (inValue < 0)
        return 0;
    else if (inValue > 255)
        return 255;
    else
        return (byte)inValue;
}
bool sharpen(Mat inImg,Mat outImg)
{
    int nchannel = inImg.NumberOfChannels;
    int nrows = inImg.Rows;
    int ncols = inImg.Cols;
    if(nchannel==1)
    {
        var tempImg=inImg.ToImage<Gray,byte>();
        var tempImg2 = tempImg.Clone();
        for(int i=1;i<nrows-1;++i)
        {
            for(int j=1;j<ncols-1;++j)
            {
                tempImg2.Data[i, j, 0] = saturateCast(5 * tempImg.Data[i, j, 0] - tempImg.Data[i - 1, j, 0] - tempImg.Data[i, j - 1, 0] -
                tempImg2.Data[i + 1, j, 0] - tempImg.Data[i, j + 1, 0]);
            }
        }
        for(int i=0;i<nrows;++i)
        {
            tempImg2.Data[i, 0, 0] = 0;
            tempImg2.Data[i, ncols - 1, 0] = 0;
        }
        for(int j=0;j<ncols;++j)
        {
            tempImg2.Data[0, j, 0] = 0;
            tempImg2.Data[nrows - 1, j, 0] = 0;
        }
        tempImg2.Mat.CopyTo(outImg);
    }
    else if(nchannel==3)
    {
        var tempImg = inImg.ToImage<Bgr, byte>();
        var tempImg2 = tempImg.Clone();
        for(int i=1;i<nrows-1;++i)
        {
            for(int j=1;j<ncols-1;++j)
            {
                for(int channel=0;channel<3;channel++)
                {
                    tempImg2.Data[i, j, channel] = saturateCast(5 * tempImg.Data[i, j, channel] - tempImg.Data[i - 1, j, channel] - tempImg.Data[i, j - 1, channel] -
                        tempImg.Data[i + 1, j, channel] - tempImg.Data[i, j + 1, channel]);
                }
            }
        }
        for(int i=0;i<nrows;++i)
        {
            for(int channel=0;channel<3;channel++)
            {
                tempImg2.Data[i, 0, channel] = 0;
                tempImg2.Data[i, ncols - 1, channel] = 0;
            }
        }
        for(int j=0;j<ncols;++j)
        {
            for(int channel=0;channel<3;channel++)
            {
                tempImg2.Data[0, j, channel] = 0;
                tempImg2.Data[nrows - 1, j, channel] = 0;
            }
        }
        tempImg2.Mat.CopyTo(outImg);
    }else
    {
        return false;
    }
    return true;
}
private void button27_Click(object sender, EventArgs e)
{
    Mat inImage = new Mat();
    Mat outImage = new Mat();
    if (chapter1OFD.ShowDialog() == DialogResult.OK)
        inImage = CvInvoke.Imread(chapter1OFD.FileName, LoadImageType.AnyColor | LoadImageType.AnyDepth);
    if (inImage.IsEmpty)
        return;
    if (!sharpen(inImage, outImage))
        return;
    if (outImage.IsEmpty)
        return;
    imageBox7.Image = outImage;
}
private void button28_Click(object sender, EventArgs e)
{
    if (chapter2Img.IsEmpty)
        return;
    Image<Gray, float> kernelI = new Image<Gray, float>(3, 3, new Gray(0));
    kernelI.Data[1, 1, 0] = (float)5.0;
    kernelI.Data[0, 1, 0] = (float)-1.0;
    kernelI.Data[1, 0, 0] = (float)-1.0;
    kernelI.Data[2, 1, 0] = (float)-1.0;
    kernelI.Data[1, 2, 0] = (float)-1.0;
    if(chapter2Img.NumberOfChannels==3)
    {
        var tempImg = chapter2Img.ToImage<Bgr, byte>();
        Image<Gray,byte>[] results = tempImg.Split();
        CvInvoke.Filter2D(results[0].Mat, results[0].Mat, kernelI.Mat, new Point(-1, -1));
        CvInvoke.Filter2D(results[1].Mat, results[1].Mat, kernelI.Mat, new Point(-1, -1));
        CvInvoke.Filter2D(results[2].Mat, results[2].Mat, kernelI.Mat, new Point(-1, -1));
                 
        for(int i=0;i<tempImg.Rows;i++)
            for(int j=0;j<tempImg.Cols;j++)
            {
                tempImg.Data[i, j, 0] = results[0].Data[i, j, 0];
                tempImg.Data[i, j, 1] = results[1].Data[i, j, 0];
                tempImg.Data[i, j, 2] = results[2].Data[i, j, 0];
            }
        imageBox7.Image = tempImg;
    }
    else if(chapter2Img.NumberOfChannels==1)
    {
        var tempImg = chapter2Img.ToImage<Gray, byte>();
        CvInvoke.Filter2D(tempImg.Mat, tempImg.Mat, kernelI.Mat, new Point(-1, -1));
        imageBox7.Image = tempImg;
    }
}


以上两部分按钮,一个用的是自定义的函数进行锐化处理,一个用到了filter2d函数





(2)检测图片中的颜色(本例中为浅蓝色),并显示检测结果,监测结果为二值图像,白色表示所检测的颜色部分,黑色表示非所检测颜色部分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值