Assign different colors to different biological cells in an image
I need to assign different colors to different biological cells in an image.
To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.
Edit: It is biological cells. I have found something similar online:
Source: http://brainiac2.mit.edu/isbi_challenge/
python image opencv image-processing cv2
add a comment |
I need to assign different colors to different biological cells in an image.
To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.
Edit: It is biological cells. I have found something similar online:
Source: http://brainiac2.mit.edu/isbi_challenge/
python image opencv image-processing cv2
Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54
It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44
1
Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40
add a comment |
I need to assign different colors to different biological cells in an image.
To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.
Edit: It is biological cells. I have found something similar online:
Source: http://brainiac2.mit.edu/isbi_challenge/
python image opencv image-processing cv2
I need to assign different colors to different biological cells in an image.
To be more specific, the image is only black and white (so a matrix of 0 and 255 only). The content of the cells (excluding boundaries) is represented by white color, whereas cell boundaries are represented by black color. Each cell is enclosed by some cell boundaries or image edges. I hope to assign different colors to different cells, such that I can immediately tell which cells I am currently at by simply looking at the value of its entry.
Edit: It is biological cells. I have found something similar online:
Source: http://brainiac2.mit.edu/isbi_challenge/
python image opencv image-processing cv2
python image opencv image-processing cv2
edited Nov 19 '18 at 19:52


Dan Mašek
8,84932546
8,84932546
asked Nov 19 '18 at 14:59
NeverBeNeverBe
3417
3417
Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54
It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44
1
Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40
add a comment |
Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54
It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44
1
Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40
Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54
Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54
It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44
It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44
1
1
Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40
Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40
add a comment |
1 Answer
1
active
oldest
votes
A simple approach comes to mind:
Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.
Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling
cv2.threshold
with the flagRETR_CCOMP
:
At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.
N.B.: "Components" are the white parts of image, "holes" are the black parts.
Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.
Sample Code:
import cv2
import numpy as np
img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)
cv2.imwrite('cells_colour.png', output)
Result:
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
add a comment |
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%2f53377311%2fassign-different-colors-to-different-biological-cells-in-an-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A simple approach comes to mind:
Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.
Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling
cv2.threshold
with the flagRETR_CCOMP
:
At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.
N.B.: "Components" are the white parts of image, "holes" are the black parts.
Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.
Sample Code:
import cv2
import numpy as np
img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)
cv2.imwrite('cells_colour.png', output)
Result:
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
add a comment |
A simple approach comes to mind:
Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.
Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling
cv2.threshold
with the flagRETR_CCOMP
:
At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.
N.B.: "Components" are the white parts of image, "holes" are the black parts.
Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.
Sample Code:
import cv2
import numpy as np
img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)
cv2.imwrite('cells_colour.png', output)
Result:
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
add a comment |
A simple approach comes to mind:
Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.
Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling
cv2.threshold
with the flagRETR_CCOMP
:
At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.
N.B.: "Components" are the white parts of image, "holes" are the black parts.
Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.
Sample Code:
import cv2
import numpy as np
img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)
cv2.imwrite('cells_colour.png', output)
Result:
A simple approach comes to mind:
Threshold the image to binarize it, since the one you provided contains more than just 0s and 255s.
Find all the contours and organize them into a two-level hierarchy. This can be accomplished by calling
cv2.threshold
with the flagRETR_CCOMP
:
At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level.
N.B.: "Components" are the white parts of image, "holes" are the black parts.
Iterate over contours. For each contour of a component (since such contours are placed at the top level of the hierarchy, they have no parent contour) draw the contour polygon filled with a random colour.
Sample Code:
import cv2
import numpy as np
img = cv2.imread('cells.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, thresh=128, maxval=255, type=cv2.THRESH_BINARY)[1]
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
output = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
for i, contour in enumerate(contours):
if hierarchy[0][i][3] == -1:
colour = cv2.randu(np.zeros(3, np.uint8), 0, 256)
cv2.drawContours(output, contours, i, colour.tolist(), -1)
cv2.imwrite('cells_colour.png', output)
Result:
answered Nov 19 '18 at 19:36


Dan MašekDan Mašek
8,84932546
8,84932546
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
add a comment |
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Thank you! This is what I am looking for.
– NeverBe
Nov 19 '18 at 19:38
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
Excellent solution!
– Mark Setchell
Nov 20 '18 at 10:01
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53377311%2fassign-different-colors-to-different-biological-cells-in-an-image%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
Are you talking about biological cells? Or Excel spreadsheets? I think a picture would help.
– Mark Setchell
Nov 19 '18 at 17:54
It is biological cells. I have just edited my question.
– NeverBe
Nov 19 '18 at 18:44
1
Besides the solution posted by @Dan Mašek, I have found that skimage.measure.label from skimage package may also work. Perhaps this additional information may help some people in the future. Source: scikit-image.org/docs/dev/api/…
– NeverBe
Nov 19 '18 at 19:40