图像处理如何应用渐变[-1 | 0 | 1]到RGB图像

时间:2022-11-02 21:18:46

I need to apply gradient operator to RGB bitmap image. It works for 8 bit image but having the difficulty in implementing same for 24 bit image. Here is my code. Can anyone see how to correct the zorizontal gradient operation to RGB image.

我需要将渐变运算符应用于RGB位图图像。它适用于8位图像,但难以实现24位图像。这是我的代码。任何人都可以看到如何纠正RGB图像的zorizo​​ntal梯度操作。

if (iBitPerPixel == 24)  ////RGB 24 bits image
{

    for(int i=0; i<iHeight; i++)
    for(int j=1; j<iWidth-4; j++)
    {
        //pImg_Gradient[i*Wp+j] = pImg[i*Wp+j+1] - pImg[i*Wp+j-1] ;
        int level = pImg[i*Wp+j*3+1] - pImg[i*Wp+j*3-1] ;
        pImg_Gradient[i*Wp+j*3] = level;


    //  pImg_Gradient[i*Wp+j*3] =  level;
    //  pImg_Gradient[i*Wp+j*3+1] = level;
    //  pImg_Gradient[i*Wp+j*3+2]= level;
    }

    for(int i=0; i<iHeight; i++)
        for(int j=0; j<iWidth; j++)
    {
        // Copy the convetred values to original image.
        pImg[i*Wp+j] = (BYTE) pImg_Gradient[i*Wp+j];
    }
        //delete pImg_Gradient;
}

3 个解决方案

#1


1  

Unfortunately, it is not clear how to define a gradient of an RGB image. The best way to go is to transform the image into a color space that separates intensity from color, such as HSV, and compute the gradient of the intensity component. Alternatively, you can compute the gradient of each color channel separately, and then combine the results in some way, such as taking the average.

不幸的是,目前尚不清楚如何定义RGB图像的梯度。最好的方法是将图像转换为将强度与颜色分开的颜色空间,例如HSV,并计算强度分量的梯度。或者,您可以分别计算每个颜色通道的渐变,然后以某种方式组合结果,例如取平均值。

Also see Edge detectors for RGB images?

另请参阅RGB图像的边缘检测器?

#2


0  

In order to calculate the Gradient of an image (Which is a vector) you need to calculate both the horizontal and vertical derivative of the image.

为了计算图像的渐变(这是一个矢量),您需要计算图像的水平和垂直导数。

Since we're dealing with a discrete image we should use Finitie Difference approximations of the derivative.
There are many ways to approximate, many of them are listed on the Wikipedia Pages:

由于我们处理离散图像,我们应该使用导数的Finitie差分近似。有许多近似方法,其中许多都列在*页面上:

http://en.wikipedia.org/wiki/Finite_difference

http://en.wikipedia.org/wiki/Finite_difference

http://en.wikipedia.org/wiki/Finite_difference_method

http://en.wikipedia.org/wiki/Finite_difference_method

http://en.wikipedia.org/wiki/Finite_difference_coefficients

http://en.wikipedia.org/wiki/Finite_difference_coefficients

Basically those are Spatial Coefficients hence you can define a filter using them and just filter the image.
This would be the most efficient way to calculate the gradient.

基本上这些是空间系数,因此您可以使用它们定义过滤器并只过滤图像。这将是计算梯度的最有效方法。

So, all you need is to find a library (Such as Open CV) which supports filtering images and you're done.

因此,您只需要找到一个支持过滤图像的库(例如Open CV),您就完成了。

For color images, usually, you just calculate the Gradient per Color Channel.

对于彩色图像,通常只需计算每个颜色通道的渐变。

Good Luck.

祝你好运。

#3


0  

From your code; you are trying to calculate gradient from RGB but there is nothing to indicate how RGB is stored in your image. A complete guess is that in your image you have BGRBGRBGR...etc.

从你的代码;您正尝试从RGB计算渐变,但没有任何内容可指示RGB在图像中的存储方式。一个完整的猜测是你的图像中有BGRBGRBGR等等。

In that case your code is getting the gradient from the green channel, then storing it in the red of the gradient image. You don't show the gradient image being cleared to 0 - if you don't do this then it will probably be full of junk.

在这种情况下,您的代码从绿色通道获取渐变,然后将其存储在渐变图像的红色中。您不会将渐变图像显示为0 - 如果您不这样做,那么它可能会充满垃圾。

My suggestion is to convert to a greyscale image first; then you can use your original code. Or calculate a gradient for each colour channel.

我的建议是先转换为灰度图像;然后你可以使用你的原始代码。或者为每个颜色通道计算渐变。

#1


1  

Unfortunately, it is not clear how to define a gradient of an RGB image. The best way to go is to transform the image into a color space that separates intensity from color, such as HSV, and compute the gradient of the intensity component. Alternatively, you can compute the gradient of each color channel separately, and then combine the results in some way, such as taking the average.

不幸的是,目前尚不清楚如何定义RGB图像的梯度。最好的方法是将图像转换为将强度与颜色分开的颜色空间,例如HSV,并计算强度分量的梯度。或者,您可以分别计算每个颜色通道的渐变,然后以某种方式组合结果,例如取平均值。

Also see Edge detectors for RGB images?

另请参阅RGB图像的边缘检测器?

#2


0  

In order to calculate the Gradient of an image (Which is a vector) you need to calculate both the horizontal and vertical derivative of the image.

为了计算图像的渐变(这是一个矢量),您需要计算图像的水平和垂直导数。

Since we're dealing with a discrete image we should use Finitie Difference approximations of the derivative.
There are many ways to approximate, many of them are listed on the Wikipedia Pages:

由于我们处理离散图像,我们应该使用导数的Finitie差分近似。有许多近似方法,其中许多都列在*页面上:

http://en.wikipedia.org/wiki/Finite_difference

http://en.wikipedia.org/wiki/Finite_difference

http://en.wikipedia.org/wiki/Finite_difference_method

http://en.wikipedia.org/wiki/Finite_difference_method

http://en.wikipedia.org/wiki/Finite_difference_coefficients

http://en.wikipedia.org/wiki/Finite_difference_coefficients

Basically those are Spatial Coefficients hence you can define a filter using them and just filter the image.
This would be the most efficient way to calculate the gradient.

基本上这些是空间系数,因此您可以使用它们定义过滤器并只过滤图像。这将是计算梯度的最有效方法。

So, all you need is to find a library (Such as Open CV) which supports filtering images and you're done.

因此,您只需要找到一个支持过滤图像的库(例如Open CV),您就完成了。

For color images, usually, you just calculate the Gradient per Color Channel.

对于彩色图像,通常只需计算每个颜色通道的渐变。

Good Luck.

祝你好运。

#3


0  

From your code; you are trying to calculate gradient from RGB but there is nothing to indicate how RGB is stored in your image. A complete guess is that in your image you have BGRBGRBGR...etc.

从你的代码;您正尝试从RGB计算渐变,但没有任何内容可指示RGB在图像中的存储方式。一个完整的猜测是你的图像中有BGRBGRBGR等等。

In that case your code is getting the gradient from the green channel, then storing it in the red of the gradient image. You don't show the gradient image being cleared to 0 - if you don't do this then it will probably be full of junk.

在这种情况下,您的代码从绿色通道获取渐变,然后将其存储在渐变图像的红色中。您不会将渐变图像显示为0 - 如果您不这样做,那么它可能会充满垃圾。

My suggestion is to convert to a greyscale image first; then you can use your original code. Or calculate a gradient for each colour channel.

我的建议是先转换为灰度图像;然后你可以使用你的原始代码。或者为每个颜色通道计算渐变。