Art*_*ras 0 arrays image-processing go computer-vision
最近,我一直对机器学习更感兴趣,机器学习图像,但要做到这一点,我需要能够处理图像.我想更全面地了解图像处理库如何工作,所以我决定创建自己的库来阅读我能理解的图像.但是,在读取图像的SIZE时,我似乎遇到了一个问题,因为当我尝试编译时会弹出这个错误:
./imageProcessing.go:33:11: non-constant array bound Size
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
package main
import (
// "fmt"
// "os"
)
// This function reads a dimension of an image you would use it like readImageDimension("IMAGENAME.PNG", "HEIGHT")
func readImageDimension(path string, which string) int{
var dimensionyes int
if(which == "" || which == " "){
panic ("You have not entered which dimension you want to have.")
} else if (which == "Height" || which == "HEIGHT" || which == "height" || which == "h" || which =="H"){
//TODO: Insert code for reading the Height of the image
return dimensionyes
} else if (which == "Width" || which == "WIDTH" || which == "width" || which == "w" || which =="W"){
//TODO: Insert code for reading the Width of the image
return dimensionyes
} else {
panic("Dimension type not recognized.")
}
}
func addImage(path string, image string, Height int, Width int){
var Size int
Size = Width * Height
var Pix [Size][3]int
}
func main() {
}
Run Code Online (Sandbox Code Playgroud)
我刚刚开始用Go编程,所以如果这个问题听起来不错,我很抱歉
因为Go是一种静态类型语言,这意味着需要在编译时知道变量类型.
Go中的数组是固定大小的:一旦在Go中创建数组,以后就无法更改其大小.这在某种程度上是指数组的长度是数组类型的一部分(这意味着类型[2]int并且[3]int是2种不同的类型).
变量的值通常在编译时是未知的,因此使用它作为数组长度,在编译时不会知道该类型,因此不允许.
阅读相关问题:如何在go中找到阵列的大小
如果您在编译时不知道大小,请使用切片而不是数组(还有其他原因也可以使用切片).
例如这段代码:
func addImage(path string, image string, Height int, Width int){
var Size int
Size = Width * Height
var Pix [Size][3]int
// use Pix
}
Run Code Online (Sandbox Code Playgroud)
可以转换为创建和使用这样的切片:
func addImage(path string, image string, Height int, Width int){
var Size int
Size = Width * Height
var Pix = make([][3]int, Size)
// use Pix
}
Run Code Online (Sandbox Code Playgroud)