Extracting Cluster Coordinates out of Flood Fill Function
I am trying to find a flood filling function that can give me as output a list with the coordinates (min and max of the x,y axis) of the clusters ('connected'pixels).
I have been given the following implementation but it seems to be the most time consuming part of my algorithm, and I wanna speed it up, as in the end it has to run over some hundreds of images.
def floodfill8(self,coord,i,j,imin,imax,jmin,jmax,area=0):
r"""determining cluster size and coordinates by recursive flood filling
"""
if coord[i,j]==1:
area+=1
imax=np.max([imax,i])
imin=np.min([imin,i-1])
jmax=np.max([jmax,j])
jmin=np.min([jmin,j-1])
coord[i,j]=0
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j-1,imin,imax,jmin,jmax,area)
return area, coord, imin, imax, jmin, jmax
def findcluster(self,coord):
r"""Cluster finding algorithm, finds connected groups of 1's in coord
---------
Input:
coord array of 0's and 1's
---------
Output:
clusters list of clusters with elements [xmin,xmax,ymin,ymax,size]"""
#add fixed boundaries
newcoord=np.zeros((coord.shape[0]+2,coord.shape[1]+2))
newcoord[1:-1,1:-1]=coord
clusters=
for i in np.arange(coord.shape[0]):
for j in np.arange(coord.shape[1]):
if newcoord[i+1,j+1]==1:
size,newcoord,imin,imax,jmin,jmax=self.floodfill8(newcoord,i+1,j+1,imin=i,imax=i,jmin=j,jmax=j)
clusters.append([imin,imax,jmin,jmax,size])
clusters=np.array(clusters)
if len(clusters)>1:
ind=np.argsort(clusters[:,-1])
clusters=clusters[ind]
return clusters
Note that this recursive flood filling is called in a loop over all the pixels in the image.
I really couldn't find anything already implemented, a library or something, and I am still quite a noob in this ,image processing, field, but I believe that it should be something optimized in some library.
I was wondering whether anyone has any information of a flood filling function or the relevant library, that can also give me this output.
The reason I want this output is twofold:
1.So that I can find the position of the clusters and remove them (the rectangle surrounding them) from the picture so that I do background correction.
2.So that draw a (red)-rectangular around those clusters that have the wished contrast and the wished size.
Hope my question is clear! If not I will be happy to get some feedback and explain the unclear parts.
Also if you have any advice in order to speed it up it is most welcome!
Thank you!
output coordinates cluster-computing flood-fill
add a comment |
I am trying to find a flood filling function that can give me as output a list with the coordinates (min and max of the x,y axis) of the clusters ('connected'pixels).
I have been given the following implementation but it seems to be the most time consuming part of my algorithm, and I wanna speed it up, as in the end it has to run over some hundreds of images.
def floodfill8(self,coord,i,j,imin,imax,jmin,jmax,area=0):
r"""determining cluster size and coordinates by recursive flood filling
"""
if coord[i,j]==1:
area+=1
imax=np.max([imax,i])
imin=np.min([imin,i-1])
jmax=np.max([jmax,j])
jmin=np.min([jmin,j-1])
coord[i,j]=0
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j-1,imin,imax,jmin,jmax,area)
return area, coord, imin, imax, jmin, jmax
def findcluster(self,coord):
r"""Cluster finding algorithm, finds connected groups of 1's in coord
---------
Input:
coord array of 0's and 1's
---------
Output:
clusters list of clusters with elements [xmin,xmax,ymin,ymax,size]"""
#add fixed boundaries
newcoord=np.zeros((coord.shape[0]+2,coord.shape[1]+2))
newcoord[1:-1,1:-1]=coord
clusters=
for i in np.arange(coord.shape[0]):
for j in np.arange(coord.shape[1]):
if newcoord[i+1,j+1]==1:
size,newcoord,imin,imax,jmin,jmax=self.floodfill8(newcoord,i+1,j+1,imin=i,imax=i,jmin=j,jmax=j)
clusters.append([imin,imax,jmin,jmax,size])
clusters=np.array(clusters)
if len(clusters)>1:
ind=np.argsort(clusters[:,-1])
clusters=clusters[ind]
return clusters
Note that this recursive flood filling is called in a loop over all the pixels in the image.
I really couldn't find anything already implemented, a library or something, and I am still quite a noob in this ,image processing, field, but I believe that it should be something optimized in some library.
I was wondering whether anyone has any information of a flood filling function or the relevant library, that can also give me this output.
The reason I want this output is twofold:
1.So that I can find the position of the clusters and remove them (the rectangle surrounding them) from the picture so that I do background correction.
2.So that draw a (red)-rectangular around those clusters that have the wished contrast and the wished size.
Hope my question is clear! If not I will be happy to get some feedback and explain the unclear parts.
Also if you have any advice in order to speed it up it is most welcome!
Thank you!
output coordinates cluster-computing flood-fill
add a comment |
I am trying to find a flood filling function that can give me as output a list with the coordinates (min and max of the x,y axis) of the clusters ('connected'pixels).
I have been given the following implementation but it seems to be the most time consuming part of my algorithm, and I wanna speed it up, as in the end it has to run over some hundreds of images.
def floodfill8(self,coord,i,j,imin,imax,jmin,jmax,area=0):
r"""determining cluster size and coordinates by recursive flood filling
"""
if coord[i,j]==1:
area+=1
imax=np.max([imax,i])
imin=np.min([imin,i-1])
jmax=np.max([jmax,j])
jmin=np.min([jmin,j-1])
coord[i,j]=0
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j-1,imin,imax,jmin,jmax,area)
return area, coord, imin, imax, jmin, jmax
def findcluster(self,coord):
r"""Cluster finding algorithm, finds connected groups of 1's in coord
---------
Input:
coord array of 0's and 1's
---------
Output:
clusters list of clusters with elements [xmin,xmax,ymin,ymax,size]"""
#add fixed boundaries
newcoord=np.zeros((coord.shape[0]+2,coord.shape[1]+2))
newcoord[1:-1,1:-1]=coord
clusters=
for i in np.arange(coord.shape[0]):
for j in np.arange(coord.shape[1]):
if newcoord[i+1,j+1]==1:
size,newcoord,imin,imax,jmin,jmax=self.floodfill8(newcoord,i+1,j+1,imin=i,imax=i,jmin=j,jmax=j)
clusters.append([imin,imax,jmin,jmax,size])
clusters=np.array(clusters)
if len(clusters)>1:
ind=np.argsort(clusters[:,-1])
clusters=clusters[ind]
return clusters
Note that this recursive flood filling is called in a loop over all the pixels in the image.
I really couldn't find anything already implemented, a library or something, and I am still quite a noob in this ,image processing, field, but I believe that it should be something optimized in some library.
I was wondering whether anyone has any information of a flood filling function or the relevant library, that can also give me this output.
The reason I want this output is twofold:
1.So that I can find the position of the clusters and remove them (the rectangle surrounding them) from the picture so that I do background correction.
2.So that draw a (red)-rectangular around those clusters that have the wished contrast and the wished size.
Hope my question is clear! If not I will be happy to get some feedback and explain the unclear parts.
Also if you have any advice in order to speed it up it is most welcome!
Thank you!
output coordinates cluster-computing flood-fill
I am trying to find a flood filling function that can give me as output a list with the coordinates (min and max of the x,y axis) of the clusters ('connected'pixels).
I have been given the following implementation but it seems to be the most time consuming part of my algorithm, and I wanna speed it up, as in the end it has to run over some hundreds of images.
def floodfill8(self,coord,i,j,imin,imax,jmin,jmax,area=0):
r"""determining cluster size and coordinates by recursive flood filling
"""
if coord[i,j]==1:
area+=1
imax=np.max([imax,i])
imin=np.min([imin,i-1])
jmax=np.max([jmax,j])
jmin=np.min([jmin,j-1])
coord[i,j]=0
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i+1,j-1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j+1,imin,imax,jmin,jmax,area)
area,coord,imin,imax,jmin,jmax=self.floodfill8(coord,i-1,j-1,imin,imax,jmin,jmax,area)
return area, coord, imin, imax, jmin, jmax
def findcluster(self,coord):
r"""Cluster finding algorithm, finds connected groups of 1's in coord
---------
Input:
coord array of 0's and 1's
---------
Output:
clusters list of clusters with elements [xmin,xmax,ymin,ymax,size]"""
#add fixed boundaries
newcoord=np.zeros((coord.shape[0]+2,coord.shape[1]+2))
newcoord[1:-1,1:-1]=coord
clusters=
for i in np.arange(coord.shape[0]):
for j in np.arange(coord.shape[1]):
if newcoord[i+1,j+1]==1:
size,newcoord,imin,imax,jmin,jmax=self.floodfill8(newcoord,i+1,j+1,imin=i,imax=i,jmin=j,jmax=j)
clusters.append([imin,imax,jmin,jmax,size])
clusters=np.array(clusters)
if len(clusters)>1:
ind=np.argsort(clusters[:,-1])
clusters=clusters[ind]
return clusters
Note that this recursive flood filling is called in a loop over all the pixels in the image.
I really couldn't find anything already implemented, a library or something, and I am still quite a noob in this ,image processing, field, but I believe that it should be something optimized in some library.
I was wondering whether anyone has any information of a flood filling function or the relevant library, that can also give me this output.
The reason I want this output is twofold:
1.So that I can find the position of the clusters and remove them (the rectangle surrounding them) from the picture so that I do background correction.
2.So that draw a (red)-rectangular around those clusters that have the wished contrast and the wished size.
Hope my question is clear! If not I will be happy to get some feedback and explain the unclear parts.
Also if you have any advice in order to speed it up it is most welcome!
Thank you!
output coordinates cluster-computing flood-fill
output coordinates cluster-computing flood-fill
asked Nov 20 '18 at 20:14
LefterisLefteris
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400838%2fextracting-cluster-coordinates-out-of-flood-fill-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400838%2fextracting-cluster-coordinates-out-of-flood-fill-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown