我在C++ / CLI项目中具有此C++函数(非托管代码)。
BMP Image;
Image.ReadFromFile(filePath);
HsvColor hsvPixel;
RGBApixel startPixel;
for (int i = 0; i < Image.TellWidth(); i++) {
for (int j = 0; j < Image.TellHeight(); j++) {
startPixel = *(Image(i, j));
hsvPixel = RgbToHsv(startPixel);
RGBApixel finalPixel = HsvToRgb(hsvPixel);
*(Image(i, j)) = finalPixel;
}
}
(HsvColor和RGBApixel都是无符号字符组成的结构。Image(i,j)返回一个指向RGBApixel的指针。)
有问题的行是
hsvPixel = RgbToHsv(startPixel);
这条线有两个问题:
我将非常感谢您的帮助。
我也尝试仅执行RgbToHsv方法而不进行任何分配。当我这样做时,循环通常会正常运行,但是有时也会因System.AccessViolationException而崩溃,这仅比对hsvPixel进行分配时少得多。
RGBAPixel的代码:
typedef struct RGBApixel {
ebmpBYTE Blue;
ebmpBYTE Green;
ebmpBYTE Red;
ebmpBYTE Alpha;
} RGBApixel;
ebmpBYTE是类型定义的无符号字符。
两种方法的代码:
RGBApixel HsvToRgb(HsvColor hsv)
{
RGBApixel rgb;
unsigned char region, remainder, p, q, t;
if (hsv.Saturation == 0)
{
rgb.Red = hsv.Value;
rgb.Green = hsv.Value;
rgb.Blue = hsv.Value;
rgb.Alpha = 1;
return rgb;
}
region = hsv.Hue / 43;
remainder = (hsv.Hue - (region * 43)) * 6;
p = (hsv.Value * (255 - hsv.Saturation)) >> 8;
q = (hsv.Value * (255 - ((hsv.Saturation * remainder) >> 8))) >> 8;
t = (hsv.Value * (255 - ((hsv.Saturation * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
rgb.Red = hsv.Value; rgb.Green = t; rgb.Blue = p;
break;
case 1:
rgb.Red = q; rgb.Green = hsv.Value; rgb.Blue = p;
break;
case 2:
rgb.Red = p; rgb.Green = hsv.Value; rgb.Blue = t;
break;
case 3:
rgb.Red = p; rgb.Green = q; rgb.Blue = hsv.Value;
break;
case 4:
rgb.Red = t; rgb.Green = p; rgb.Blue = hsv.Value;
break;
default:
rgb.Red = hsv.Value; rgb.Green = p; rgb.Blue = q;
break;
}
return rgb;
}
HsvColor RgbToHsv(RGBApixel rgb)
{
HsvColor hsv;
unsigned char rgbMin, rgbMax;
rgbMin = rgb.Red < rgb.Green ? (rgb.Red < rgb.Blue ? rgb.Red : rgb.Blue) : (rgb.Green < rgb.Blue ? rgb.Green : rgb.Blue);
rgbMax = rgb.Red > rgb.Green ? (rgb.Red > rgb.Blue ? rgb.Red : rgb.Blue) : (rgb.Green > rgb.Blue ? rgb.Green : rgb.Blue);
hsv.Value = rgbMax;
if (hsv.Value == 0)
{
hsv.Hue = 0;
hsv.Saturation = 0;
return hsv;
}
hsv.Saturation = 255 * long(rgbMax - rgbMin) / hsv.Value;
if (hsv.Saturation == 0)
{
hsv.Hue = 0;
return hsv;
}
if (rgbMax == rgb.Red)
hsv.Hue = 0 + 43 * (rgb.Green - rgb.Blue) / (rgbMax - rgbMin);
else if (rgbMax == rgb.Green)
hsv.Hue = 85 + 43 * (rgb.Blue - rgb.Red) / (rgbMax - rgbMin);
else
hsv.Hue = 171 + 43 * (rgb.Red - rgb.Green) / (rgbMax - rgbMin);
return hsv;
}
Image(i,j)的实现:
RGBApixel* BMP::operator()(int i, int j)
{
using namespace std;
bool Warn = false;
if( i >= Width )
{ i = Width-1; Warn = true; }
if( i < 0 )
{ i = 0; Warn = true; }
if( j >= Height )
{ j = Height-1; Warn = true; }
if( j < 0 )
{ j = 0; Warn = true; }
if( Warn && EasyBMPwarnings )
{
cout << "EasyBMP Warning: Attempted to access non-existent pixel;" << endl
<< " Truncating request to fit in the range [0,"
<< Width-1 << "] x [0," << Height-1 << "]." << endl;
}
return &(Pixels[i][j]);
}
像素声明(来自EasyBMP库):
RGBApixel** Pixels;
编辑:我使用调试器来查看RgbToHsv函数如何执行。循环第一次运行时,startPixel始终相同(应该如此)。但是,当我在Visual Studio中单击“进入”并查看代码如何在函数中执行时,参数(“rgb”)始终与startPixel完全不同!我是调试的新手,所以我可能会错误地解释事物。但是现在我更加困惑了。 Here is a picture.
我还应该在运行时提及代码的结果。它输出图像,但是输出图片只是随机的单色(例如,全是蓝色),而它应该与输入图片相同。
最佳答案
确切的问题是他评论中描述的@HansPassant:
我要解决的问题是删除所有#pragma非托管/#pragma托管行,而是将所有应在本机代码中编译的cpp文件设置为不支持clr,就像他描述的here一样。