gpt4 book ai didi

c# - OpenCV不包含 'GetPixel'的定义

转载 作者:行者123 更新时间:2023-12-02 17:54:59 25 4
gpt4 key购买 nike

尝试使用OpenCv / EmguCV在图像上实现过滤器,但出现错误:

Emgu.CV.Image不包含“GetPixel”的定义

 for (int filterX = 0; filterX < filterWidth; filterX++)
{
for (int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;

**Color imageColor = img.GetPixel(imageX, imageY);**
red += imageColor.R * filter[filterX, filterY];
green += imageColor.G * filter[filterX, filterY];
blue += imageColor.B * filter[filterX, filterY];
}
int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);

result[x, y] = Color.FromArgb(r, g, b);
}
}
}
for (int i = 0; i < w; ++i)
{
for (int j = 0; j < h; ++j)
{
sharpenImage.SetPixel(i, j, result[i, j]);
}
}
}

最佳答案

EMGU使用内置于C#中的结构来保存图像数据。与使用OpenCV进行所有操作相比,这可使代码更有效地工作。尽管EMGU确实将一些OpenCV函数包装在非托管代码中,但是为什么我们要添加opencv.dll,它也试图在C#中保留尽可能多的函数。

EMGU图像结构使用GetPixel方法访问图像数据的方式略有不同,如下所示:

//Colour Image
Bgr my_Bgr = My_Image[0, 0];

//Gray Image
Gray my_Gray = gray_image[0, 0];

您显然将[0,0]更改为图像中相关位置的位置。在EMGU中不建议这样做,因为它的速度不如Bitmap.GetPixel方法慢,但仍然不是最快的方法。

EMGU Image结构可以直接访问Image.Data属性中的图像矩阵。这样可以更快地读取/写入数据。但是,如果设置了图像的ROI,则在通过每个像素循环时,任何小的警告都会大大降低速度。手动将for循环的开始和结束语句设置为所需的ROI设置,然后设置ROI字段要容易得多。原因很简单,任何方法都必须先检查ROI,然后在找到数据之前计算要访问的像素,这会增加一些指令。

可以按以下方式访问Image.Data方法:
//Image<Bgr,Byte>: Bgr = Blue,Green,Red

int Red = My_Image.Data[0,0,2]; //Read to the Red Spectrum
int Green= My_Image.Data[0,0,1]; //Read to the Green Spectrum
int Blue= My_Image.Data[0,0,0]; //Read to the BlueSpectrum

无论如何,您的代码应该看起来像这样:
for (int filterX = 0; filterX < filterWidth; filterX++)
{
for (int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;

red += img.Data[imageY,imageX,2] * filter[filterX, filterY];
green += img.Data[imageY,imageX,1] * filter[filterX, filterY];
blue += img.Data[imageY,imageX,0] * filter[filterX, filterY];
}
int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);

result[x, y] = Color.FromArgb(r, g, b);
}

如果您需要更多帮助,请在我的代码项目文章 Creating Your First EMGU Image Processing Project中介绍访问图像数据的主题,

希望这可以帮助,

干杯,

克里斯

关于c# - OpenCV不包含 'GetPixel'的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8646037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com