首页 > 解决方案 > 不兼容的类型:RGBColor [][] 无法转换为 Double [][] -Error

问题描述

我需要一些帮助来解决让我发疯的事情。关于代码的一些信息-

  1. 像素是红、绿、蓝3种颜色。
  2. RGBColor[][] 表示一个像素数组。
  3. RGBImage[][] 表示一个 RGBColor 数组。RGBImage[][] 数组的每个单元格中都有 RGBColor 对象。
  4. convertToGrayscale() 是一种将每个像素转换为其灰度值的方法。
  5. 还有一个 RGBImage 类的 toString() 方法可以打印出带有所有值的图像 - RGBColor 对象数组。

问题- 我希望 toGrayscaleArray() 方法使用 convertToGrayscale() 方法。所以我构建了一个 for 循环,将数组的每个单元格转换为灰度。但是当我尝试返回现在已转换的 _rgbimage 时,我得到编译器错误 -不兼容的类型:RGBColor [][] 无法转换为 Double [][]

任何帮助将非常感激。

    /** returns grayscale representation of the image
    * @return grayscale representation of the image
    */
    public double [][] toGrayscaleArray()
    {
    int row = _rgbimage.length;
    int col = _rgbimage[0].length;

    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
        {
            _rgbimage[i][j].convertToGrayscale();

        } 
    return _rgbimage; //THE COMPILER ERROR IS HERE
}

toString() - 该方法已经过测试并且可以正常工作。

/** creates a string of matrix which represents the image, in the following format:
 * (red,green,blue) (red,green,blue) (red,green,blue)
 * (red,green,blue) (red,green,blue) (red,green,blue)
 * (red,green,blue) (red,green,blue) (red,green,blue)
 * @return toString
 */
public String toString ()
{
    int rows = _rgbimage.length; 
    int cols = _rgbimage[0].length;
    String imageMatrix = new String("");
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            if (j < cols-1) //checks if it's the last cell on the row
                imageMatrix += _rgbimage[i][j] + " "; //if not last cell - add space
            else 
                imageMatrix += _rgbimage[i][j]; //if last cell - don't add space             
        }
        imageMatrix += "\n";
    }
    return imageMatrix;
    }

标签: javaarraysdoubletostringrgbcolor

解决方案


推荐阅读