I am trying to cluster different regions of an image using Fuzzy C-means clustering with ImageSegmentation.jl
using ImageSegmentation, Images
fl = load("flower.jpg")
fuzzy_fl = fuzzy_cmeans(fl,3,2)
It gives an error like this:
MethodError: no method matching fuzzy_cmeans(::Base.ReshapedArray{Float64,2,ImageCore.ChannelView{Float64,3,Array{ColorTypes.RGB4{Float64},2}},Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64},Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}})
Closest candidates are:
fuzzy_cmeans(::Array{T<:Real,2}, ::Int64, ::Real; maxiter, tol, dist_metric, display) where T<:Real at /Users/asharma19/.julia/v0.6/Clustering/src/fuzzycmeans.jl:58
fuzzy_cmeans(::AbstractArray{T<:ColorTypes.Colorant,N}, ::Any...; kwargs...) where {T<:ColorTypes.Colorant, N} at /Users/asharma19/.julia/v0.6/ImageSegmentation/src/clustering.jl:12
1) How should I input an image to this function?
2) Also, if it works fine, as the function does not return a SegmentedImage array, how should I display a segmented/clustered image after that?
The code works fine for me. You are probably taking a slice of the image manipulating it (e.g. views, transposes) before feeding it into the fuzzy_cmeans function. Something different than the code example you posted.
The function fuzzy_cmeans does not accept Array{ColorTypes.RGB4{FixedPointNumbers.Normed{UInt8,8}},2}.
The images need to be Array{ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}},2} to be input in the function.
Although, it should not happen as the parameter type of the function is Colorant{T,N} which is the parent of AbstractRGB{T}.
The images usually are RGB4 (have some padding involved) so you need to convert them before using. (can use convert function of julia)
Related
Suppose I want to write a dynamic function that gets an object subtype of AbstractMatrix and shuffles the values along a specified dimension. Surely there can be various approaches and ways to do this, but suppose the following way:
import Random.shuffle
function shuffle(data::AbstractMatrix; dims=1)
n = size(data, dims)
shuffled_idx = shuffle(1:n)
data[shuffled_idx, :] #This line is wrong. It's not dynamic
A wrong way is to use several (actually indefinite) if-else statements like if dims==1 do... if dims==2 do. But it isn't the way to do these kinds of things. I could write data::AbstractArray then the input could have various dimensions. So this came to my mind that this can be possible if I can do something like getindex(data, [idxs]; dims). But I checked for the dims keyword argument (or even positional one) in the dispatches of getindex, but there isn't such a definition. So how can I get values by specified indexes and along a dim?
You are looking for selectdim:
help?> selectdim
search: selectdim
selectdim(A, d::Integer, i)
Return a view of all the data of A where the index for dimension d equals i.
Equivalent to view(A,:,:,...,i,:,:,...) where i is in position d.
Here's a code example:
function myshuffle(data::AbstractMatrix; dim=1)
inds = shuffle(axes(data, dim))
return selectdim(data, dim, inds)
end
Make sure not to use 1:n as indices for AbstractArrays, as they may have non-standard indices. Use axes instead.
BTW, selectdim apparently returns a view, so you may or may not need to use collect on it.
I am trying to learn Julia and I read this article about the quick success of Julia. In the last page of the article the author works a small example showing the benefits of multiple dispatch. They define a custom class Spect and define a plot() function for it. Then for an object sqw of type Spect they can call plot(sqw) without having to edit the original plot function. Moreover, this definition also affects similar plotting functions so that you can also call scatter(sqw) without problems. My issue is that author does not show the code, so I do not understand how can you achieve this. I am specially interested in the fact that just defining plot() for this new class is enough to also call other functions like scatter() without defining them for the new class.
Can someone write a small example of this like that of the article so that I can understand how all of this is achieved? Thank you in advance.
Cross posting my answer from Discourse:
It’s a shame the article doesn’t link to the code. Here’s my rough reproduction attempt. My version uses the dct and idct so I’m not getting the nice harmonics, but I think it shows the ideas pretty well.
using RecipesBase, FFTW
struct Spect
points :: AbstractRange
weights :: Vector{Float64}
end
function Spect(f::Function, min, max, n)
points = range(min, max, n)
Spect(points, dct(f.(points)))
end
#recipe function f(S::Spect)
S.points, idct(S.weights)
end
These definitions are enough for
using Plots
squarewave(x) = iseven(floor(x)) ? 1.0 : 0.0
sqw = Spect(squarewave, 0, 5, 20);
plot(sqw)
scatter(sqw)
and
Is this bad practice? It seems like a lot could go wrong here.*
I am setting the argument of an outer function to be a global variable for a function defined inside it. I am just doing this to work around some existing code.
f = function(a,b){h = function(c){print(b);b+c}}
myh = f(1,2)
myh(7)
#[1] 2
#[1] 9
*On the other hand, it's perfectly acceptable to write something like
h = function(c){print(7);7+c}
Creating a function that creates functions (or a function factory) is a totally acceptable code practice. See https://adv-r.hadley.nz/function-factories.html for more details on certain parts of the technical implementation in R.
It is most often used if you need to create functions at runtime or you need to create a lot of similar funcions.
The function factory you have created could be considered similar to a function factory that would create different sized counters that told the user how much the amount was incremented by.
It is important to keep track of the functions you create this way however.
Let me know if you'd like more clarification on anything.
(One possible bad practise in the function you have created though is an unused argument a).
I read in several places that pipes in Julia only work with functions that take only one argument. This is not true, since I can do the following:
function power(a, b = 2) a^b end
3 |> power
> 9
and it works fine.
However, I but can't completely get my head around the pipe. E.g. why is this not working?? :
3 |> power()
> MethodError: no method matching power()
What I would actually like to do is using a pipe and define additional arguments, e.g. keyword arguments so that it is actually clear which argument to pass when piping (namely the only positional one):
function power(a; b = 2) a^b end
3 |> power(b = 3)
Is there any way to do something like this?
I know I could do a work-around with the Pipe package, but to honest it feels kind of clunky to write #pipe at the start of half of the lines.
In R the magritrr package has convincing logic (in my opinion): it passes what's left of the pipe by default as the first argument to the function on the right - I'm looking for something similar.
power as defined in the first snippet has two methods. One with one argument, one with two. So the point about |> working only with one-argument methods still holds.
The kind of thing you want to do is called "partial application", and very common in functional languages. You can always write
3 |> (a -> power(a, 3))
but that gets clunky quickly. Other language have syntax like power(%1, 3) to denote that lambda. There's discussion to add something similar to Julia, but it's difficult to get right. Pipe is exactly the macro-based fix for it.
If you have control over the defined method, you can also implement methods with an interface that return partially applied versions as you like -- many predicates in Base do this already, e.g., ==(1). There's also the option of Base.Fix2(power, 3), but that's not really an improvement, if you ask me (apart from maybe being nicer to the compiler).
And note that magrittrs pipes are also "macro"-based. The difference is that argument passing in R is way more complicated, and you can't see from outside whether an argument is used as a value or as an expression (essentially, R passes a thunk containing the expression and a pointer to the parent environment, and automatically evaluates and caches it if you use it as a value; see substitute)
I have a function f(x,y) whose outcome is random (I take mean from 20 random numbers depending on x and y). I see no way to modify this function to make it symbolic.
And when I run
x,y = var('x,y')
d = plot_vector_field((f(x),x), (x,0,1), (y,0,1))
it says it can't cast symbolic expression to real or rationa number. In fact it stops when I write:
a=matrix(RR,1,N)
a[0]=x
What is the way to change this variable to real numbers in the beginning, compute f(x) and draw a vector field? Or just draw a lot of arrows with slope (f(x),x)?
I can create something sort of like yours, though with no errors. At least it doesn't do what you want.
def f(m,n):
return m*randint(100,200)-n*randint(100,200)
var('x,y')
plot_vector_field((f(x,y),f(y,x)),(x,0,1),(y,0,1))
The reason is because Python functions immediately evaluate - in this case, f(x,y) was 161*x - 114*y, though that will change with each invocation.
My suspicion is that your problem is similar, the immediate evaluation of the Python function once and for all. Instead, try lambda functions. They are annoying but very useful in this case.
var('x,y')
plot_vector_field((lambda x,y: f(x,y), lambda x,y: f(y,x)),(x,0,1),(y,0,1))
Wow, I now I have to find an excuse to show off this picture, cool stuff. I hope your error ends up being very similar.