# \`component\_centroids\` did not recognize \`Matrix{Int32}\`

**URL:** <https://discourse.julialang.org/t/component-centroids-did-not-recognize-matrix-int32/101067>\
**Category:** General Usage\
**Tags:** images, image-processing\
**Created:** [July 2, 2023, 2:16am UTC](https://discourse.julialang.org/t/component-centroids-did-not-recognize-matrix-int32/101067 "2023-07-02T02:16:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Page](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_page/32/51064_2.png) [@Adam\_Page](https://discourse.julialang.org/u/Adam_Page)\
**Post date:** [July 2, 2023, 2:16am UTC](https://discourse.julialang.org/t/component-centroids-did-not-recognize-matrix-int32/101067/1 "2023-07-02T02:16:48Z")

</div>

I’m new to Julia. I was trying to execute `center = component_centroids(cq)[2:end]` where `cq` is of type `Matrix{Int32}`, but got the following error:

```julia
MethodError: no method matching component_centroids(::Matrix{Int32})

Closest candidates are:
  component_centroids(!Matched::AbstractArray{Int64, N}) where N
   @ ImageMorphology C:\Users\whoever\.julia\packages\ImageMorphology\1lEjs\src\connected.jl:247

```

So I have to write `center = component_centroids(Int64.(cq))[2:end]` instead. However, I looked into the source code of `component_centroids` and found no sign that `Int64` is necessary. The source code is provided below for convenience:

```julia
"`component_centroids(labeled_array)` -> an array of centroids for each label, including the background label 0"
function component_centroids(img::AbstractArray{Int,N}) where {N}
    len = length(0:maximum(img))
    n = fill(zero(CartesianIndex{N}), len)
    counts = fill(0, len)
    @inbounds for I in CartesianIndices(size(img))
        v = img[I] + 1
        n[v] += I
        counts[v] += 1
    end
    return map(v -> n[v].I ./ counts[v], 1:len)
end

```

I’m not sure if this is a bug or there’s something I misunderstood.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 2, 2023, 3:17am UTC](https://discourse.julialang.org/t/component-centroids-did-not-recognize-matrix-int32/101067/2 "2023-07-02T03:17:18Z")

</div>

> [@Adam\_Page](#):
>
> `function component_centroids(img::AbstractArray{Int,N}) where {N}`

This should be

```julia
function component_centroids(img::AbstractArray{<:Integer,N}) where {N}

```

if you want to accept any type of integer array here. See [Julia FAQ - Troubleshooting “method not matched”: parametric type invariance and `MethodError`s](https://docs.julialang.org/en/v1/manual/faq/#Troubleshooting-%22method-not-matched%22:-parametric-type-invariance-and-MethodErrors), and note that `Int` is a concrete type (a synonym for `Int64` on 64-bit systems) whereas `Integer` is the abstract integer supertype.
