How to get connected components label in a binary image? - image-processing

I've a binary image where removing green dot gets me separate line segments. I've tried using label_components() function from Julia but it labels only verticall joined pixels as one label.
I'm using
using Images
img=load("current_img.jpg")
img[findall(img.==RGB(0.0,0.1,0.0))].=0 # this makes green pixels same as background, i.e. black
labels = label_components(img)
I'm expecteing all lines which are disjoint to be given a unique label
(as was a funciton in connected component labeling in matlab, but i can't find something similar in julia)

Since you updated the question and added more details to make it clear, I decided to post the answer. Note that this answer utilizes some of the functions that I wrote here; so, if you didn't find documentation for any of the following functions, I refer you to the previous answer. I operated on several examples and brought the results in the continue.
Let's begin with an image similar to the one you brought in the question and perform the entire operation from the scratch. for this, I drew the following:
I want to perform a segmentation process on it and labelize each segment and highlight the segments using the achieved labels.
Let's define the functions:
using Images
using ImageBinarization
function check_adjacent(
loc::CartesianIndex{2},
all_locs::Vector{CartesianIndex{2}}
)
conditions = [
loc - CartesianIndex(0,1) ∈ all_locs,
loc + CartesianIndex(0,1) ∈ all_locs,
loc - CartesianIndex(1,0) ∈ all_locs,
loc + CartesianIndex(1,0) ∈ all_locs,
loc - CartesianIndex(1,1) ∈ all_locs,
loc + CartesianIndex(1,1) ∈ all_locs,
loc - CartesianIndex(1,-1) ∈ all_locs,
loc + CartesianIndex(1,-1) ∈ all_locs
]
return sum(conditions)
end;
function find_the_contour_branches(img::BitMatrix)
img_matrix = convert(Array{Float64}, img)
not_black = findall(!=(0.0), img_matrix)
contours_branches = Vector{CartesianIndex{2}}()
for nb∈not_black
t = check_adjacent(nb, not_black)
(t==1 || t==3) && push!(contours_branches, nb)
end
return contours_branches
end;
"""
HighlightSegments(img::BitMatrix, labels::Matrix{Int64})
Highlight the segments of the image with random colors.
# Arguments
- `img::BitMatrix`: The image to be highlighted.
- `labels::Matrix{Int64}`: The labels of each segment.
# Returns
- `img_matrix::Matrix{RGB}`: A matrix of RGB values.
"""
function HighlightSegments(img::BitMatrix, labels::Matrix{Int64})
colors = [
# Create Random Colors for each label
RGB(rand(), rand(), rand()) for label in 1:maximum(labels)
]
img_matrix = convert(Matrix{RGB}, img)
for seg∈1:maximum(labels)
img_matrix[labels .== seg] .= colors[seg]
end
return img_matrix
end;
"""
find_labels(img_path::String)
Assign a label for each segment.
# Arguments
- `img_path::String`: The path of the image.
# Returns
- `thinned::BitMatrix`: BitMatrix of the thinned image.
- `labels::Matrix{Int64}`: A matrix that contains the labels of each segment.
- `highlighted::Matrix{RGB}`: A matrix of RGB values.
"""
function find_labels(img_path::String)
img::Matrix{RGB} = load(img_path)
gimg = Gray.(img)
bin::BitMatrix = binarize(gimg, UnimodalRosin()) .> 0.5
thinned = thinning(bin)
contours = find_the_contour_branches(thinned)
thinned[contours] .= 0
labels = label_components(thinned, trues(3,3))
highlighted = HighlightSegments(thinned, labels)
return thinned, labels, highlighted
end;
The main function in the above is find_labels which returns
The thinned matrix.
The labels of each segment.
The highlighted image (Matrix, actually).
First, I load the image, and binarize the Gray scaled image. Then, I perform the thinning operation on the binarized image. After that, I find the contours and the branches using the find_the_contour_branches function. Then, I turn the color of contours and branches to black in the thinned image; this gives me neat segments. After that, I labelize the segments using the label_components function. Finally, I highlight the segments using the HighlightSegments function for the sake of visualization (this is the bonus :)).
Let's try it on the image I drew above:
result = find_labels("nU3LE.png")
# you can get the labels Matrix using `result[2]`
# and the highlighted image using `result[3]`
# Also, it's possible to save the highlighted image using:
save("nU3LE_highlighted.png", result[3])
The result is as follows:
Also, I performed the same thing on another image:
julia> result = find_labels("circle.png")
julia> result[2]
14×16 Matrix{Int64}:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0
0 1 1 0 0 0 3 3 0 0 0 5 5 5 0 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
As you can see, the labels are pretty clear. Now let's see the results of performing the procedure in some examples in one glance:
Original Image
Labeled Image

Related

Conditionally Assign Value to Dask Dataframe using Apply

I am trying to iterate through a Dask dataframe and compare the values in one of its columns to a column in another Dask dataframe with the same name. If the columns match I would like to update the value is the target Dask dataframe. The code below runs, but the values are not updated to '1' where I expected, or anywhere. I am new to Dask and suspect I am missing some crucial step or am not understanding the framework.
def populateSymptomsDDF(row):
for vac in row['vac_codes']:
if vac in symptoms_ddf.columns:
symptoms_ddf[vac] = symptoms_ddf[vac].where(symptoms_ddf['dog'] == row['dog'], 1)
with ProgressBar():
x = vac_ddf.apply(lambda x: populateSymptomsDDF(x), meta=('int64'), axis=1)
x.compute(scheduler='processes')
symptoms_ddf.compute()
Head of icd_ddf:
dog vac_codes
0 1 [G35, E11.40, R53.1, Z79.899, I87.2]
1 2 [G35, R53.83, G47.00]
2 3 [G35, G95.9, R53.83, F41.9]
3 4 [G35, N53.9, E55.9, Z74.09]
4 5 [G35, M51.26, R53.1, M47.816, R25.2, G82.50, R...
Head of symptoms_ddf (before running code):
dog W19 W10 W05.0 V00.811 R53.83 R53.8 R53.1 R47.9 R47.89 ... G81.12 G81.11 G81.10 G50.0 G31.84 F52.8 F52.31 F52.22 F52.0 F03
0 1 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
1 2 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 4 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
4 5 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
Thank you for any insights you can provide!
Dask dataframes don't have the same in-place behavior as pandas. Generally every operation should be a bulk parallel operation. Otherwise there isn't much reason to use Dask.
Also, iterating through dataframes will generally be quite slow. This is also true with Pandas.
Fortunately, I think that you're maybe just looking for a join or merge operation. I would encourage you to look up the documentation for Pandas merge
https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html

Possibility of only dealing with specific region of binary image

Recently I study the image processing.
When I go through the problem of filling the hole, it confuses me (I assume that the people able to answer the question is familiar with the step of doing this so I skip to the problem):
Let's say if I have a binary image like this:
0 0 0 0 0 0 0
0 0 1 1 0 0 0
0 1 0 0 1 0 0
0 1 0 0 1 0 0
0 0 1 0 1 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
0 1 0 0 0 1 0
0 1 1 1 1 0 0
0 0 0 0 0 0 0
And the book says to start form the region that is inside of the hole and perform the dilation operation and set the bound in case it fills the whole image.
I have no problem understanding the whole process, but if I try to code it, how can I only deal with a specific region (in the hole for this case)? Or the actual implement would be different method ?
If you can assume that the object with holes does not touch the border of the image, you can create an intermediate image where you call flood fill (with value e.g. 2) on the top left pixel. Any remaining '0' pixels have to be inside the contour. Take the position of the first encountered remaining '0' pixel and flood fill it in the original image.

Logistic Regression prediction faults

I have been trying to solve this problem of titanic survived problem. Where i splitted x to be the passengers and y to be the survived. But the problem is i couldn't able to get the y_pred (ie) prediction results. As it is 0 for all the values. I get 0 value as prediction. It would be helpful for me if anyone can solve it. As it is my first classifier problem as a beginner
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('C:/Users/Umer/train.csv')
x = df['PassengerId'].values.reshape(-1,1)
y = df['Survived']
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,
random_state = 0)
from sklearn.preprocessing import StandardScaler
sc_x = StandardScaler()
x_train = sc_x.fit_transform(x_train)
x_test = sc_x.transform(x_test)
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(x_train,y_train)
#predicting the test set results
y_pred = classifier.predict(x_test)
I couldn't reproduce the same result, in fact, I copied-pasted your code and did not get them all zeros as you described the issue as, instead I got:
[0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0]
Nevertheless, there are a few things I noticed in your approach that you may want to know about:
The default separator in Pandas read_csv is , , so if your dataset variables separated by a tab (same like the one I have) , you then should specify the separator like this:
df = pd.read_csv('titanic.csv', sep='\t')
PassengerId has no useful information that your model may learn from in order to predict the Survived people, it's just a continuous number that increments with each new passenger. Generally speaking, in classification, you need to avail of all features that make your model learns from (unless of course there are redundant features that add no information to the model) especially in your dataset, it's a multivariate dataset.
There is no point of scaling the PassengerId, because features scaling is usually used when features highly vary in magnitudes, units and range (e.g. 5kg and 5000gms) and in your case, as I mentioned, it's just an incremental integer which has no real information to the model.
One last thing, you should get your data as type float for StandardScaler to avoid warnings like the follow:
DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
So you do convert like this from the beginning:
x = df['PassengerId'].values.astype(float).reshape(-1,1)
Finally if you're still getting the same result, then please add a link to your dataset.
Update
After providing the dataset, it turns out that the result you're getting is correct, that's again because of reason number 2 I mentioned above (that is PassengerId provides no useful information to the model so it cannot predict correctly!)
You can test it yourself via comparing the log loss before and after adding more features from the dataset:
from sklearn.metrics import log_loss
df = pd.read_csv('train.csv', sep=',')
x = df['PassengerId'].values.reshape(-1,1)
y = df['Survived']
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,
random_state = 0)
classifier = LogisticRegression()
classifier.fit(x_train,y_train)
y_pred_train = classifier.predict(x_train)
# calculate and print the loss function using only the PassengerId
print(log_loss(y_train, y_pred_train))
#predicting the test set results
y_pred = classifier.predict(x_test)
print(y_pred)
Output
13.33982681120802
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0]
Now by using many "supposedly-useful" information:
from sklearn.metrics import log_loss
df = pd.read_csv('train.csv', sep=',')
# denote the words female and male as 0 and 1
df['Sex'].replace(['female','male'], [0,1], inplace=True)
# try three features that you think they are informative to the model
# so it can learn from them
x = df[['Fare', 'Pclass', 'Sex']].values.reshape(-1,3)
y = df['Survived']
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,
random_state = 0)
classifier = LogisticRegression()
classifier.fit(x_train,y_train)
y_pred_train = classifier.predict(x_train)
# calculate and print the loss function with the above 3 features
print(log_loss(y_train, y_pred_train))
#predicting the test set results
y_pred = classifier.predict(x_test)
print(y_pred)
Output
7.238735137632405
[0 0 0 1 1 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0
0 1 1 0 0 0 0 0 1 0 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 0
1 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 0 1
1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0
0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1
1]
In Conclusion:
As you can see, the loss gave better value (lesser than before) and the prediction is now more reasonable!

Feature Reduction

How do I reduce feature dimension ? My feature looks like :
1(Class Number) 10_10_1(File name) 0 0 0 0 0 0 0 0 0.564971751 23.16384181 25.98870056 19.20903955 16.10169492 13.27683616 1.694915254 0 0 0 0 0 0 0 3.95480226 11.5819209 20.33898305 60.4519774 3.672316384 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.107344633 62.99435028 33.89830508 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.412429379 66.66666667 31.92090395 0 0 0 0 0 0 0 0 0 0 0 0 0 0.564971751 22.59887006 26.83615819 46.89265537 3.107344633 0 0 0 0 0 0 0 0 0 0 0 0 0 0.564971751 16.38418079 28.53107345 50.84745763 3.672316384 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 90.6779661 9.322033898 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.847457627 90.11299435 9.039548023 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17.79661017 81.3559322 0.847457627 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27.11864407 72.88135593 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.564971751 37.85310734 61.29943503 0.282485876 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.412429379 50.84745763 47.45762712 0.282485876 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24.57627119 75.42372881 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17.23163842 82.20338983 0.564971751 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 29.37853107 70.62146893 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 55.64971751 44.35028249 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64.40677966 35.59322034 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 67.79661017 32.20338983 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 66.66666667 33.33333333 0 0 0 0 0 0 0 0 0 0 0 0 1 3 2 6 7 5 4 8 9 10 11 12 13 14 15 16 17 18 14.81834463 3.818489078 3.292123621 2.219541777 2.740791003 1.160544518 2.820053602 1.006906813 0.090413195 2.246638594 0.269778302 2.183126126 2.239168249 0.781498607 2.229795302 0.743329919 1.293839141 0.783068011 1.104421291 0.770312707 0.697659061 1.082266169 0.408339745 1.073922207 0.999148017 0.602195061 1.247286588 0.712143548 0.867327913 0.603063537 0.474115683 0.596387106 0.370847522 0.54900076 0.35930586 0.580272233 0.397060362 0.535337691
After filename, feature values are given.
If your feature is unsupervised, you can use PCA.
import numpy as np
from sklearn.decomposition import PCA
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA(n_components=2)
pca.fit(X)
PCA(copy=True, n_components=2, whiten=False)
print(pca.explained_variance_ratio_)
If it is supervised, you can use LDA
import numpy as np
from sklearn.lda import LDA
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
y = np.array([1, 1, 1, 2, 2, 2])
clf = LDA()
clf.fit(X, y)
For reducing the amount of features handed to models, besides others, feature reduction or feature deletion/selection can be used. Some frequently used feature reduction approaches have already been pointed out here, like principal component analysis (PCA), linear discriminant analysis (LDA), or partial least square regression (PLSR). Those essentially project the original data into a subspace, which aims for representing the information in less features. In particular, PCA thereby tries to maximize the preserved variance in the original data (unsupervised), while LDA tries to minimize intra-class-variance and maximize inter-class-distance (supervised, for classification), and PLSR tries to maximize the preserved variance in the original data and maximize the correlation with the target variable (supervised, regression).
Additionally, classic feature selection can be employed for reducing the amount of features. Those don't project data into a subspace but select "useful" features straight from the existing set of features. Usually those approaches are divided in feature filters and feature wrappers, where filters decide on which features to use by looking only at the features and the target variable (e.g. try to minimize inter-feature-correlation while maximizing the feature-target-correlation). In contrast, feature wrappers additionally consider the model that uses the selected features - so they directly optimize the model performance instead. Usually, filters are computationally cheaper than wrappers - but similar to using PCA, feature filters don't necessarily need to improve subsequent model performance, as they don't know what to optimize for.
Edit: as you are working with image data, feature filters and wrappers might not be optimal if used alone - they likely require image preprocessing and/or downsizing before being employed.
If you are using R, I'd recommend using the caret package, which provides all of the above already embedded into the model model training and evaluation process, which is quite important (cf. here for some details on their filters/wrappers). Here's a small snippet for usage of the approaches above:
library(caret)
# PCA with preserving 95% variance in original data
modelPca <- train(x = iris[,1:4], iris[,5], preProcess=c('center', 'scale', 'pca'), trControl=trainControl(preProcOptions=list(thresh=0.95)), method='svmLinear', tuneGrid=expand.grid(C=3**(-3:3)))
# LDA with selection of dimensions
modelLda2 <- train(x = iris[,1:4], y = iris[,5], method='lda2', tuneGrid=expand.grid(dimen=1:4))
# PLSR with selection of dimensions
modelPls <- train(x = iris[,1:3], y = iris[,4], method='pls', tuneGrid=expand.grid(ncomp=1:3))
# feature wrapper: (backwards) recursive feature elimination (there exist more...)
modelRfe <- rfe(x = iris[,1:4], y = iris[,5], sizes = 1:4, rfeControl = rfeControl())
# feature filter: univariate filtering
modelSbf <- sbf(x = iris[,1:4], y = iris[,5], sbfControl = sbfControl())

Why is my convolution result shifted when using FFT

I'm implementing Convolutions using Radix-2 Cooley-Tukey FFT/FFT-inverse, and my output is correct but shifted upon completion.
My solution is to zero-pad both input size and kernel size to 2^m for smallest possible m, tranforming both input and kernel using FFT, then multiply the two element-wise and transform the result back using FFT-inverse.
As an example on the resulting problem:
0 1 2 3 0 0 0 0
4 5 6 7 0 0 0 0
8 9 10 11 0 0 0 0
12 13 14 15 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
with identity kernel
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
becomes
0 0 0 0 0 0 0 0
0 0 1 2 3 0 0 0
0 4 5 6 7 0 0 0
0 8 9 10 11 0 0 0
0 12 13 14 15 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
It seems any sizes of inputs and kernels produces the same shift (1 row and 1 col), but I could be wrong. I've performed the same computations using the online calculator at this link! and get same results, so it's probably me missing some fundamental knowledge. My available litterature has not helped. So my question, why does this happen?
So I ended up finding the answer why this happens myself. The answered is given through the definition of the convolution and the indexing that happens there. So by definition the convolution of s and k is given by
(s*k)(x) = sum(s(k)k(x-k),k=-inf,inf)
The center of the kernel is not "known" by this formula, and thus an abstraction we make. Define c as the center of the convolution. When x-k = c in the sum, s(k) is s(x-c). So the sum containing the interesting product s(x-c)k(c) ends up at index x. In other words, shifted to the right by c.
FFT fast convolution does a circular convolution. If you zero pad so that both the data and kernel are circularly centered around (0,0) in the same size NxN arrays, the result will also stay centered. Otherwise any offsets will add.

Resources